Analysis of good questions in C language (3)

multiple choice one

以下程序段的输出结果是()
#include<stdio.h>
int main()
{
    
    
char s[] = "\\123456\123456\t";
printf("%d\n", strlen(s));
return 0;
}
A: 12      B: 13      C: 16     D: 以上都不对

[Answer] A
[Analysis] This question involves escape characters, \ is a kind of escape character, and \ is to modify (second \) with (first \) so that (second \) does not represent Escaping means (a bit of a twist here)
so \\ is one character and 123456 is 6 characters.
For the following \123456\t, \123 represents a character ( \ddd: ddd represents a 1-3 digit octal number ), while 456 represents 3 characters, and \t represents a character.
Next is the meaning of strlen, strlen is to calculate the length of the string, and will not stop until it meets \0 , so after the previous analysis, strlen (s)=1+6+1+3+1=12
bold style

multiple choice two

若有以下程序,则运行后的输出结果是()
#include <stdio.h>
#define N 2
#define M N + 1
#define NUM (M + 1) * M / 2
int main()
{
    
    
printf("%d\n", NUM);
return 0;
}
A: 4       B: 8       C: 9      D: 6

[Answer] B
[Analysis] This question is to replace variables. According to the above definition, we know that
N=2
M=N+1
NUM=(M+1)*M/2
, just bring the equation into the third expression come to conclusion.
It is worth noting that many people calculated the result as 6, because they calculated NUM=(2+1+1)(2+1)/2=6 (in fact, I also calculated this question ) but in
fact The correct algorithm is:
NUM=(2+1+1) *2+1/2=8.5
This is because the second M of (M+1)*M does not have (), so you cannot add () without authorization during calculation
.
The last 8 is because %d is an int printing method, so the 5 after the decimal point is omitted
insert image description here

multiple choice three

如下函数的 f(1) 的值为( )
int f(int n)
{
    
    
static int i = 1;
if(n >= 5)
return n;
n = n + i;
i++;
return f(n);
}
A: 5      B: 6       C: 7       D: 8

[Answer] C
[Analysis] This question is a recursive question, n>=5 is a restriction, but it should be noted that static modified i
must not think that int i=1, i will always be 1, static Modification allows it to retain the previously changed value, so i is always increasing.
The detailed process is shown in the figure:
insert image description here
insert image description here

multiple choice four

Do the following 3 pieces of program code have the same effect? ​​( )

int b;
(1)const int *a = &b;
(2)int const *a = &b;
(3)int *const a = &b;
A: (2)=(3)  B: (1)=(2)  C: 都不一样  D: 都一样

[Answer] B
[Analysis] We need to understand the meaning of const, as long as we understand the meaning, we can do this question
const is to modify the variable behind it so that its address or value cannot be changed.
For example:
(1) const is modified as a, which means that a cannot be changed, because a represents the address, so the address represented by a cannot be changed, but the value of a can be changed.
(2) const is still modified by *a (it has nothing to do with int)
(3) const is modified by a, which means that the value represented by a cannot be changed, but the address of a can be changed.

Programming question one

Verifying Nicoches Theorem

任何一个整数 m 的立方都可以写成 m 个连续奇数之和
例如
1^3=1
2^3=3+5
3^3=7+9+11
4^3=13+15+17+19
输入一个正整数 m(m≤100) 
将 m 的立方写成 m 个连续奇数之和的形式输出。
注意:本题含有多组输入数据。
输入描述:输入一个int整数
输出描述:输出分解后的string
示例:
输入:6
输出:31+33+35+37+39+41

[Problem analysis]
We need to use n to represent the cube of m, and then the first odd number a1=x can be set from the sum of consecutive odd numbers, and then Sm=m*x+(m- 1) 2,
the relationship m^3=m
x+2 * (m-1) * m can be obtained
, that is, x=m * m- m+1
because x is the first item, in order to find the specific value of x We need to use for loop

[code]

int main()
{
    
    
	int m = 0;
		scanf("%d", &m);
		int n = m * m * m;
		int x = m * m - m + 1;
		for (int i = 1; i <= m; i++)
		{
    
    
			if (i == m)
				printf("%d", x);
			else
			{
    
    
				printf("%d+", x);
				x += 2;
			}

		}
	return 0;
}

Programming question two

Arithmetic series 2, 5, 8, 11, 14, ... . (3 from 2 is the arithmetic sequence of the tolerance), find the sum of the first n items of the arithmetic sequence

注意:本题有多组输入
输入描述:输入一个正整数 n 。
输出描述:输出一个相加后的整数
示例:
输入:2 输入:275
输出:7 输出:113575
说明:2+5=7 说明:2+5+...+821+824=113575

[Problem Analysis]
The solution to this question is similar to the previous question, but it is for Sn,
so you need to set a sum (that is, Sn), and x can be solved through the formula sum+=x, x+=3 plus a loop Question

[code]

#include<stdio.h>
int main()
{
    
    
	int n, x = 2, sum = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
    
    
		sum += x;
		x += 3;
	}
	printf("%d", sum);
	return 0;
}

Supongo que te gusta

Origin blog.csdn.net/2301_79178723/article/details/132379006
Recomendado
Clasificación