当前位置:网站首页>[C language] question set of X

[C language] question set of X

2022-07-07 16:28:00 InfoQ

write in front

​​Hello, Hello, everyone  En, Learn together , Your smile (●'◡'●)
Welcome to → give the thumbs-up  +  Collection ️ +  Leaving a message.
️ We are not on the stage of our choice , Acting is not the script we chose
summary : I hope after you read it , It can help you , Insufficient, please correct ! Learn and communicate together
2021  Blog star of the year, Internet of things and embedded development  TOP5~2021  Blog star  Top100~ Alibaba cloud experts  ^  Star Blogger ~ Nuggets ⇿InfoQ  Creator ~ Zhou Bang  77» General list  1479

Question 46 → Create a custom function , So as to achieve strcat() The function of

​​​​​​​

First of all, when realizing this topic , We need to know first  strcat()  A basic information of function .
Let's see first  strcat()  The function declaration of the function is as follows ↓

char *strcat(char *dest, const char *src)
Append a copy of the source string to the destination string .
dest
 →  Point to target array , The array contains a  C  character string , And enough to hold the appended string .
src
 →  Point to the string to append , The string does not override the target string .
This function returns a string pointing to the final destination  
dest
  The pointer to .
hold  
src
  The string pointed to is appended to  
dest
  The end of the string that you are pointing to .
That's all  strcat()  A basic information of function , I believe that when we know this, we can solve this problem well *⌒(*^-゜)v THX!!


Question 47 → seek  1! + 2! + 3! ... +n!; Don't think about spillovers

This can actually be used  for  The loop can be done . This topic is actually an easy one , But the comparison needs to examine the ability of logical thinking .
1! + 2! + 3! ... +n!  The assumption is 4 that →1+1×2+1×2×3+1×2×3×4.
remember : The problem is that there is no need to consider whether the program results in stack overflow .
In fact, if you encounter this kind of problem, you can refer to a method of substitution method to solve this kind of problem, which will be much better .

Question 48 → Create a custom function , Implement string functions strcpy()

Do this to create a string function to achieve its functions , First, we must understand the function of the custom function we created , This is conducive to our better realization .
strcpy()  The function is declared as follows
 ↓

char *strcpy(char *dest, const char *src)
hold  
src
  The string you point to is copied to  
dest.
Note that if the target array  
dest
  Not big enough , And the length of the source string is too long , It may cause buffer overflow . therefore ,
dest
  Be big enough , So that we can be  
src
  Put it away .
dest
 
  Point to the target array used to store the copied content .
src
 
  The string to copy .
This function returns a string pointing to the final destination  
dest
  The pointer to .
strcpy()
  In the original character to ensure that you have  
src
  The space size of existing characters is also called subscript .
Be careful : The pointer type of the return value here can be  
void
  It can also be  **char ***

Question 49 → Calculated at n How many binary complements are there in the parameters of 1

The subject : Compare and examine your logical thinking ability, which is actually a use of operators .( The key to solving the problem is )
Calculated at n How many binary complements are there in the parameters of 1, That is, when we input numbers when we input them , It can calculate the number of complements among us 1. Let's take a number of input integer types as an example , In doing this problem, we must know the original code 、 Inverse code 、 What exactly is complement (^∀^●)ノシ
Original code →
Directly convert the number into binary according to the positive or negative form .
Inverse code →
Leave your sign bit unchanged , That is, the highest position remains unchanged , Secondly, reverse the order of bits , Get the inverse .
Complement code →
The complement is   Inverse code +1  You can get the complement , Be careful : The premise is to reverse the code , On the basis of +1.

Question 50 → Design an algorithm , Find input A and B The least common multiple of

I entered two numbers in the previous topic , Find their greatest common divisor . So in this exercise, we will design an algorithm , Find input A and B The least common multiple of . Before that, we need to know what is the least common multiple , In this way, we can have a train of thought for solving problems (@^0^)
The multiples of two or more integers are called their common multiples , In addition to 0 The smallest common multiple outside is called the smallest common multiple of these integers . Integers a,b The least common multiple of is [a,b], alike ,a,b,c The least common multiple of is [a,b,c], The least common multiple of multiple integers has the same notation .
The concept corresponding to the least common multiple is the greatest common divisor ,a,b The greatest common divisor of is (a,b). On the least common multiple and the greatest common divisor , We have this theorem :(a,b)x[a,b]=ab(a,b All are integers ).
Let's use a diagram to show ↓
null

Question 46 の Code

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
char *My_strcat(char *dest, const char *src)
{
 assert(dest && src != NULL);// Assertion
 char *ret = dest;
 while (*dest != '\0')//'\0' Of ASCLL The code value is 0
 {
 dest++;
 }
 //dest Pointing to '\0'
 while (*dest++ = *src++)
 {
 ;
 }
 /* amount to
 while (*src != '\0')
 {
 *dest++ = *src++;
 }*/
 return ret;
}
int main(void)
{
 char arr1[20] = &quot;hello C&quot;;
 char arr2[20] = &quot;yuyan&quot;;
 printf(&quot;%s\n&quot;, My_strcat(arr1, arr2));
 return 0;
}

The operation results are as follows ↓
hello Cyuyan


Question 47 の Code

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main(void)
{
 // Substitution method  1*1 + 1*2 + 1*2*3 + 1*2*3*4 -  Suppose you enter a number :4
 int i = 0;
 int j = 0;
 int num = 0;
 int sum = 0;
 printf(&quot; Please enter a number ->:&quot;);
 scanf(&quot;%d&quot;, &num);
 for (i = 1; i <= num; i++)
 {
 int ret = 1;// Be careful ->ret
 for (j = 1; j <= i; j++)
 {
 ret = j * ret;// The sum of each class
 }
 sum = ret + sum;// The sum of the
 }
 printf(&quot;sum = %d\n&quot;, sum);
 return 0;
}

The operation results are as follows ↓
Please enter a number ->:4
sum = 33


Question 48 の Code

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
void my_strcpy(char* str1, char* str2)
{
 assert(str1 && str2 != NULL);// Assertion !

 // Put the string str2 Assign a value to str1, encounter '\0' end .
 while (*str2 != '\0')
 {
 *str1++ = *str2++;
 }
}
int main(void)
{
 char str[20] = { 0 };
 char p[20] = { 0 };
 printf(&quot; Please enter the string ->:&quot;);
 scanf(&quot;%s&quot;, str);
 my_strcpy(p, str);
 printf(&quot;ret = %s\n&quot;,p);
 return 0;
}

Running results
Please enter the string ->:C Language yyds!
ptr = C Language yyds!


Question 49 の Code

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int function(int n)
{
 int count = 0;
 int i = 0;
 for (i = 0; i < 32; i++)
 {
 //  hypothesis n = 3
 // 0011 >> 0 - 0011 & 1111 +1
 // 0011 >> 1 - 0001 & 1111 +2
 // 0001 >> 2 - 0000 & 1111 count = 2
 if (((n >> i) & 1) == 1)
 { 
 count++;
 }
 }
 return count;
}
int main(void)
{
 int n = 0;
 printf(&quot; Please enter a number :&quot;);
 scanf(&quot;%d&quot;, &n);
 int ret = function(n);
 printf(&quot;ret = %d\n&quot;, ret);

 return 0;
}

Running results
hypothesis → Please enter a number :3
ret = 2


Question 50 の Code

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
typedef unsigned long int u_lint;
int main(void)
{
 int i = 1;
 u_lint a = 0;
 u_lint b = 0;
 printf(&quot; Please enter two numbers ->:&quot;);
 scanf(&quot;%d %d&quot;, &a, &b);
 while (i)
 {
 if (a*i % b == 0)
 {
 printf(&quot; Minimum common multiple :%d\n&quot;, a*i);
 break;
 }
 i++;// Be careful →i++ The location of
 }
 return 0;
}

Running results
Possible input results → Please enter two numbers ->:90 6
Minimum common multiple :90
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071413162693.html