当前位置:网站首页>Basic questions (I)
Basic questions (I)
2022-06-30 06:32:00 【Yuan_ o_】
1、 Analyze the following functions
#include <stdio.h>
int main()
{
int a, b, c;
a = 5;
c = ++a;// First ++, Reuse a (c=6,a=6)
b = ++c, c++, ++a, a++;// Be careful (b = ++c) It's an expression (c=8,b=7,a=8)
b += a++ + c;//+= The priority is very low , So first count the second a=9,b=23
printf("a = %d b = %d c = %d\n:", a, b, c);
return 0;
}
The output is :
2、
Write a function to return the parameter binary 1 The number of .
such as : 15 0000 1111 4 individual 1
// Method 1 :
// Version of a defects , Negative numbers cannot be calculated
// Version 2 Just one change , Change the formal parameter to unsigned type
#include <stdio.h>
int stat1(int n)// Version of a
int stat1(unsigned int n)// Version 2
{
int count = 0;
while (n)
{
if ((n % 2) == 1)
{
count++;
}
n /= 2;
}
return count;
}
int main()
{
int num = 0;
scanf("%d", &num);
int n = stat1(num);
printf("%d\n", n);
return 0;
}
// Method 2
#include <stdio.h>
int stat1(int n)
{
int i = 0;
int count = 0;
for (i = 0; i < 32; i++)
{
if (((n >> i) & 1) == 1)
{
count++;
}
}
return count;
}
int main()
{
int num = 0;
scanf("%d", &num);
int n = stat1(num);
printf("%d\n", n);
return 0;
}
// Method 3
#include <stdio.h>
int stat1(int n)
{
int count = 0;
while(n)
{
n = n & (n - 1);
count++;
}
return count;
}
int main()
{
int num = 0;
scanf("%d", &num);
int n = stat1(num);
printf("%d\n", n);
return 0;
}
3、 Programming to realize :
Two int(32 position ) Integers m and n In the binary representation of , How many bits (bit) Different ?
Input example :
1999 2299
Output example :7
// Method 1 :
#include <stdio.h>
int count_diff_bit(int m, int n)
{
int count = 0;
int i = 0;
for (i = 0; i < 32; i++)
{
if (((m >> i) & 1) != ((n >> i) & 1))
{
count++;
}
}
return count;
}
int main()
{
int m = 0;
int n = 0;
scanf("%d %d", &m, &n);
int ret = count_diff_bit(m, n);
printf("%d\n", ret);
return 0;
}
// Method 2 :
#include <stdio.h>
int count_diff_bit(int m, int n)
{
int count = 0;
int ret = m ^ n;// ^ XOR operator Same as 0, Dissimilarity is 1
while (ret)
{
ret = ret & (ret - 1);
count++;
}
return count;
}
int main()
{
int m = 0;
int n = 0;
scanf("%d %d", &m, &n);
int ret = count_diff_bit(m, n);
printf("%d\n", ret);
return 0;
}
4、 Get all even and odd bits in an integer binary sequence , Print out the binary sequence separately
//10
//0000000000000000000000000001010
// Specify the last as the first
#include <stdio.h>
int main()
{
int i = 0;
int num = 0;
scanf("%d", &num);
// Get odd digit numbers
for (i = 30; i >= 0; i -= 2)
{
printf("%d ",(num >> i) & 1);
}
printf("\n");
// Get even digits
for (i = 31; i >= 1; i -= 2)
{
printf("%d ", (num >> i) & 1);
}
return 0;
}
5、 Analyze the following code
#include <stdio.h>
int i;// Global variables
// Global variables , Static variables are placed in the static area
// Global variables , Static variables are not initialized , The default value is 0
// local variable , Put it in the stack area , Not initialized , The default value is random
int main()
{
i--;//-1
//sizeof The result of this operator calculation is size_t Type of , It's an unsigned integer
if (i > sizeof(i))// When they compare , There's an integer boost , Convert to unsigned integer , here -1 Is a very positive number
{
printf(">\n");
}
else
{
printf("<\n");
}
return 0;
}
Global variables , When no initial value is given , The compiler initializes it to... By default 0.
i The initial value of 0,i– result -1,i For shaping ,sizeof(i) seek i The type size is 4, According to this analysis , The results should be printed <, however sizeof The return value type of is actually an unsigned integer , Therefore, the compiler will automatically set the left i Automatically convert to unsigned data ,-1 The corresponding unsigned integer is a very large number , exceed 4 perhaps 8, So it should actually be printed >
6、
Input description : Multi group input , An integer (2~20), Represents the number of rows output , It also means composition “x” The length of the backslash and the backslash of .
Output description : Enter... For each line , For output “*” Composed of X Shape patterns .
#include <stdio.h>
int main()
{
int n = 0;
while(scanf("%d", &n) == 1)
{
int j = 0;
int i = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
printf("*");
else if (i + j == n - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
return 0;
}
7、
Input description :
Multi group input , There are two integers in a row , Represents year and month respectively , Space off .
Output description :
Enter... For each group , Output as a line , An integer , It means how many days there are in this month of the year .
Example :
Input :2008 2
Output :29
// Method 1
#include <stdio.h>
int main()
{
int year = 0;
while (scanf("%d", &year) == 1)
{
int n = 0;
scanf("%d", &n);
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
if (n == 2)
printf("29");
else if (n == 4 || n == 6 || n == 9)
printf("30");
else
printf("31");
}
else
{
if (n == 2)
printf("28");
else if (n == 2 || n == 4 || n == 6 || n == 9)
printf("30");
else
printf("31");
}
}
return 0;
}
// Method 2
#include <stdio.h>
int is_leap_year(int y)// Determine if it's a leap year
{
return(((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0));
}
int main()
{
int y = 0;// year
int m = 0;// month
int d = 0;// Days
int data[13] = {
0,31,28,31,30,31,30,31,31,30,31,30,31 };// Array start complement 0, Make the array subscript correspond to the month
while (scanf("%d%d", &y, &m) == 2)//scanf Function received several values , Will return a few
{
int d = data[m];
if ((is_leap_year(y) == 1) && (m == 2))// To determine whether it is a leap year 2 month
{
d++;
}
printf("%d\n", d);
}
return 0;
}
边栏推荐
- Suggestion: use tools:overrideLibrary
- 神经网络入门
- 46. 全排列-dfs双百代码
- Summary of 2 billion redis data migration
- Thread safe solutions, communication between threads (classic examples of producers and consumers)
- Develop stylelint rules from zero (plug-ins)
- Multithreading advanced level
- [wechat applet: single or multiple styles, background color, rounded corners]
- Use of observer mode and status mode in actual work
- ES6解构赋值
猜你喜欢
Connect to remote server
Base64 explanation: playing with pictures Base64 encoding
RSA and AES
力扣------替换空格
Initial love with mqtt
Pycharm shortcut key
Judge whether H5 is in wechat environment or enterprise wechat environment at both ends
Never forget the original intention, and be lazy if you can: C # operate word files
Feisheng: Based on the Chinese word breaker ik-2 ways to build custom hot word separators Showcase & pit arrangement Showtime
Multithreading advanced level
随机推荐
Vscode configuration proxy
Spin official tutorial
Why does the verification code not refresh when clicked
Common mistakes daily practice 01
Suggestion: use tools:overrideLibrary
Picture.....
关于Glide加载图片模糊不清楚
MySQL summary
To: k210 realizes face recognition (code interpretation attached)
1.5 - 逻辑运算
Use and principle of completionservice (source code analysis)
ES6数组遍历与ES5数组遍历
Multithreading advanced level
INI analyse les documents d'apprentissage
Gazebo model modification
Common NPM install errors
Application of redis client list in practice
Is it safe to open an account online? Can you open an account to speculate on the Internet?
Record a problem tracking of excessive load
ES6 arrow function