Analysis of good questions in C language (1)

multiple choice 1

执行下面程序,正确的输出是( )
int x = 5, y = 7;
void swap()
{
    
    
	int z;
	z = x;
	x = y;
	y = z;
} int main()
{
    
    
	int x = 3, y = 8;
	swap();
	printf("%d,%d\n",x, y);
	return 0;
}
A: 5,7    B: 7,5    C: 3,8    D: 8,3

[Answer] C
[Analysis] First of all, we can see that x and y have two different values, which are x=5 and 3, y=7 and 8, and the range of x=5 and y=7 is larger than that of x=3 and The range of y=8 is larger (that is, the scope is larger), and the printf function has a proximity principle , that is, it outputs the nearest x and y to it, and the swap function exchanges x=5 and y=7 (because swap The function has no proximity principle, and the scope of x=5 and y=7 is larger )

multiple choice 2

以下不正确的定义语句是( )
A double x[5] = {2.0, 4.0, 6.0, 8.0, 10.0};
B char c2[] = {'\x10', '\xa', '\8'};
C char c1[] = {'1','2','3','4','5'};
D int y[5+3]={0, 1, 3, 5, 7, 9};

[Answer] B
[Analysis] \8 means 8 in octal, and the number in octal is represented as 0~7, so \8 means error

multiple choice 3

若给定条件表达式 (M)?(a++):(a--) ,则其中表达式 M ()
A: 和(M==0)等价 B: 和(M==1)等价 C: 和(M!=0)等价 D: 和(M!=1)等价

[Answer] C
[Analysis] (M)?(a++): (a–) means to judge whether the expression M is true (M!=0) (because it is an expression, it is true as long as it is not 0), If true then a++, otherwise a–

multiple choice 4

有如下定义语句,则正确的输入语句是【多选】( )
int b;
char c[10];
A: scanf("%d%s",&b,&c); B: scanf("%d%s",&b,c);
C: scanf("%d%s",b,c); D: scanf("%d%s",b,&c);

[Answer] AB
[Analysis] Some people here may think that there should be & after scanf. In fact, & is only for storing data addresses, and & can be omitted for arrays.
Let's take an example

#include<stdio.h>
void jack( int *arr)
{
    
    
	for (int i = 0; i <= 5; i++)
	{
    
    
		arr[i] = i;
	}
}
int main()
{
    
    
	int arr[6] = {
    
     0 };
	jack(arr);
	for (int i = 0; i <= 5; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
	return 0;
}

The result of this code running is shown in the figure
insert image description here
and after adding &

#include<stdio.h>
void jack( int *arr)
{
    
    
	for (int i = 0; i <= 5; i++)
	{
    
    
		arr[i] = i;
	}
}
int main()
{
    
    
	int arr[6] = {
    
     0 };
	jack(&arr);
	for (int i = 0; i <= 5; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
	return 0;
}

insert image description here
We can see that the result has not changed, so & can be added or not.

Programming question one

根据输入的日期,计算是这一年的第几天。输入保证年份为4位数且日期合法。
输入描述:输入一行,每行空格分割,分别是年,月,日。
输出描述:输出是这一年的第几天
示例:
输入:2012 12 31 输入:1982 3 4
输出:366 输出:63

Niuke.com topic link
[topic analysis]
1: Since the number of days in each month is not necessarily the same, it is very difficult to solve this problem if we don’t use an array (I tried it), so we need to define an array, with each The number of days in a month is an element, which is input into the array in order, and because the number of days in February of a leap year is different from that of a non-leap year, we need to define two arrays to represent leap years and non-leap years respectively.
2: Because the last day we input is not necessarily the last day of this month, we need to calculate one month less, and then add the number of days in the previous month, plus the number of days entered, to get the final result

【Code】

#include <stdio.h>
int main()
{
    
    
	int year, month, day,sum=0;
	scanf("%d %d %d", &year, &month, &day);
	int run[12] = {
    
     31,29,31,30,31,30,31,31,30,31,30,31 };
	int notrun[12] = {
    
     31,28,31,30,31,30,31,31,30,31,30,31 };
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
	{
    
    
		for (int i = 0; i <= month - 2; i++)
		{
    
    
			sum = sum+run[i];
		}
		sum += day;
	}
	else
	{
    
    
		for (int i = 0; i <= month - 2; i++)
		{
    
    
			sum =sum+ notrun[i];
		}
		sum += day;
	}
	printf("%d", sum);
	return 0;
}

Guess you like

Origin blog.csdn.net/2301_79178723/article/details/132316817