C language stage test questions

Hello everyone, I am deep fish~

[Preface]: This part is a staged test question after learning the C language. The last programming question is somewhat difficult and needs to be figured out. If you type a lot of code, it will naturally feel easy. Come on, iron juicers! ! !

1. Multiple choice questions

1. After the following program is executed, the output result is ()

#include<stdio.h>
int cnt = 0;
int fib(int n)
{
	cnt++;
	if (n == 0)
		return 1;
	else if (n == 1)
		return 2;
	else
		return fib(n - 1) + fib(n - 2);
}
void main()
{
	fib(8);
	printf("%d", cnt);
}

[Answer]: 67

The problem of function recursion: enter the fib function once, cnt++, to calculate the value of cnt, that is, how many times the fib function has been entered , then list the dendrogram: until it reaches 1 and 0, it will not enter the fib function again, and then Each number listed will enter the function to use cnt++, count the total number of columns, and the corresponding value is the value of cnt: 1+2+4+8+16+22+12+2=67


2. The final value of the following program k is ()

    int i = 10;
	int j = 20;
	int k = 3;
	k *= i + j;

[Answer]: 90

This question examines the issue of operator precedence

Assignment operators (*=, /=, +=, -=) have very low priority , so i+j must be calculated first, and then *k, which is k*=30, which is 90


3. True or false: C language itself has no input and output statements

【Answer】 : correct

The C language has a given grammar, and the input and output are provided by library functions.

The C language stipulates some functions, specifying the name, function, parameters and return type of the function

These specified functions are implemented by the compiler manufacturer. The manufacturers of msvc, gcc, and clang provide the implementation when implementing the compiler. These functions are placed in the standard library and are called library functions.


 4. If there is a definition statement int year=1009, *p=&year, the following statement that cannot increase the value in the variable year to 1010 is ()

A.*p+=1                         B.(*p)++                                C.++(*p)                  D.*p++

[Answer]: D

In fact, the above code is equivalent to int year=1009; int *p=&year

A.* (dereference operator) has higher priority than += (assignment operator), p dereference points to year, then year++

B. With parentheses, calculate *p first, then year++

C. There are also parentheses, as long as (*p) is year++

D.*p++, ++ has higher priority than *, ++ is executed first, p++ is followed by ++, the value is p first, then p++, here is the address ++ , not the element pointed to by the address ++


5.char a;int b;float c;double d;

Then the type of the value of the expression a*b+dc is

[Answer]: double type

First, a*b (char*int), the char type is transformed into an int type, and the result of the int *int type is the int type

Then the int+double type, at this time, the int type is arithmetically converted to the double type, and the result of the double+double type is the double type

Finally, the double-float type, at this time, the float type is arithmetically converted to the double type, and the result of the double-double type is the double type

[Note]: In this process, plastic promotion and arithmetic conversion have occurred, and the type is changed first and then the operation is performed.

2. Programming questions

1. Find the least common multiple

【topic】:

 [Question 1] : Conventional practice

The least common multiple must be greater than or equal to the two numbers entered, so you can start adding from the larger number of the two numbers, and see which number you add can divide the two numbers at the same time

#include <stdio.h>
int main() 
{
   int a=0;
   int b=0;
   //输入
   scanf("%d %d",&a,&b);
   //求较大值
   int m=a>b?a:b;
   //从较大者开始求最小公倍数
   while(1)
   {
    if(m%a==0&&m%b==0)
    break;
    m++;
   }
   //输出
   printf("%d\n",m);
    return 0;
}

[Problem Solution 2] : The least common multiple = the product of the two input numbers / the greatest common divisor (rolling and dividing method)

#include <stdio.h>
int main() 
{
   int a=0;
   int b=0;
   int c=0;
   //输入
   scanf("%d %d",&a,&b);
   int n=a*b;
   //求最大公约数
   while(c=a%b)
   {
    a=b;
    b=c;
   }
   //输出
   printf("%d\n",n/b);
    return 0;
}

[Solution 3]: The highest efficiency

Suppose m is the least common multiple of a and b, then m can be divisible by both a and b, m/a=i, m/b=j, then we can see whether a*i can divide b , eg : To find the least common multiple of 3 and 5, first check whether 3*1 can divide 5, then check whether 3*2 can divide 5, and so on, until 3*i can divide 5, then 3*i is the smallest common multiple

#include <stdio.h>
int main() 
{
   int a=0;
   int b=0;
   //输入
   scanf("%d %d",&a,&b);
   int i=1;
   while(a*i%b)
   {
    i++;
   }
   //输出
   printf("%d\n",a*i);
    return 0;
}

[Note]: We'd better write the int type as long long type, because a and b can reach a maximum of 100,000, and their multiplication is very large, and the input and output of long long type are % lld

2. Inversion of the string 

【topic】:

【answer】:

Idea: Reverse the entire string, then each word (or reverse each word first, then the entire string)

#include <stdio.h>
#include<string.h>
void reverse(char* left, char* right) 
{
    while (left < right) 
    {
        char tmp = *left;
        *left = *right;
        *right = tmp;
        left++;
        right--;
    }
}
int main() 
{
    char arr[101] = {0};
    gets(arr);
    int len=strlen(arr);
    //1.逆序整个字符串
    reverse(arr,arr+len-1);
    //2.逆序每个单词
    char*cur=arr;
    while(*cur!='\0')
    {
        char*start=cur;
        while(*cur!=' '&&*cur!='\0')
        {
            cur++;
        }
        char*end=cur-1;
        reverse(start,end);
        if(*cur==' ')
        cur++;
    }
    printf("%s\n",arr);
    return 0;
}

【Precautions】:

(1) Input string : Cannot use the scanf function to input normally, because it stops reading as soon as it encounters a space, gets(arr)

If you want to use it, you have to write scanf ("%[^\n]s", arr); it means that the scanf function does not stop until it reads \n

You can also use getchar:

int ch=0;

int i=0;

while(   (ch=getchar(  ) )!='\n')

{
    arr[i++]=ch;
}

(2) Reverse the entire string : the idea is to exchange the left and right characters one by one until left>right to stop the exchange

There is also how to pass the address of the last character when the reverse custom function passes parameters: the address of the first character + the length of the string -1

 (3) Output in reverse order: first define a cur pointer to point to the front of the string, and then start traversing, taking a word as a loop, when the pointer points to something other than a space or \0, cur continues to traverse down until it is encountered, then To invert this word, we use the start and end pointers to point to the beginning and end of a word respectively, and then the word can also be reversed as a string

When a word ends, you need to enter the next word. At this time, you need to use cur++, but if this is the last word, you don’t need cur++, because ++ will cause the array to go out of bounds

This time the content is over. If you have any questions, welcome to the comment area or private message to communicate. I think the author’s writing is okay, or I have gained a little. Please, please move your little hands and give me a one-click three-link. grateful !

Guess you like

Origin blog.csdn.net/qq_73017178/article/details/132109782