当前位置:网站首页>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;
}
边栏推荐
- Practice summary of Prometheus project in amu Laboratory
- Loading class `com. mysql. jdbc. Driver‘. This is deprecated. The new driver class is `com. mysql. cj. jdb
- Control method of UAV formation
- Thread safe solutions, communication between threads (classic examples of producers and consumers)
- ES6箭头函数
- [untitled]
- Gazebo model modification
- To: k210 realizes face recognition (code interpretation attached)
- A complete performance test process
- List in set (2)
猜你喜欢

1.4 - 定点数与浮点数
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

与MQTT的初定情缘

Loading class `com. mysql. jdbc. Driver‘. This is deprecated. The new driver class is `com. mysql. cj. jdb

我做功能测试这么多年的心得

1.3 - 码制

File Transfer Protocol,FTP文件共享服务器

gazebo/set_ model_ State topic driving UAV model through posture

Initial love with mqtt

Common NPM install errors
随机推荐
Traitement d'images 7 - amélioration d'images
Application of redis client list in practice
Multithreading advanced level
Gazebo installation, uninstall and upgrade
判断h5在两端是在微信环境还是企业微信环境
880. 索引处的解码字符串
Docker is equipped with the latest MySQL image
Rhcsa day 1
c# - C#用fo-dicom对CT图像的PixelData进行处理和转换
不忘初心,能偷懒就偷懒:C#操作Word文件
ES6 extended operator (...)
ROS multi machine
How does Altium designer hide some temporarily unnecessary classes, such as GND
ini解析學習文檔
New project folder based on PIO plug-in in vscode -- Interpretation
High performance distributed execution framework ray
从底层结构开始学习FPGA----RAM IP核及关键参数介绍
Never forget the original intention, and be lazy if you can: C # operate word files
Variable parameters of go
Rhcsa day 3