C language daily practice (10)

Foreword:
Daily practice series, each issue contains 5 multiple-choice questions and 2 programming questions . The blogger will explain in as much detail as possible, so that beginners can listen clearly. The daily practice series will continue to be updated, and there must be an update within three days during the summer vacation. After the school starts, it will be updated according to the academic situation.

 Five multiple choice questions:

1. If x=2014, the return value of the following function is ( )

int fun(unsigned int x)
{
int n = 0;
while(x + 1)
{
n++;
x = x | (x + 1);
} 
return n;
}

 A、20   B、21    C、23     D、25

Analysis: Observe the code and find that the main structure of the code is a loop, and every time a loop is performed, n+1, and the value returned at the end is the size of n, then our goal is to count the number of loops. Inside the loop, x and x+1 perform the | (or) operation. The rule of the | operation is that as long as one of the bits is true, it will be true, and if two of them are false at the same time, it will be false.

And performing the | operation with a number 1 greater than itself will stably change the bit of x to 1, until all of them become 1, and then it will be 0 after another cycle. In 2014, it can be written as 0000 ......0111 1101 1110, there are 9 1s and 23 0s in total, then after 23 cycles, that is, when n is 23, x is 1111.......1111, 32-bit average It is 1, and want to loop again, but x+1 is 0, which does not satisfy the loop condition, so the loop ends and returns 23. Therefore the answer is C

 2. The following function fun calculates prod=1*2*3*…*n and returns the calculation result. But when n>12, the return value is incorrect. To find out the error of this program, the correct debugging method is ( )

int fun(int n)
{
int prod = 1 , i = 0;
for(i = 1;i <= n;i++)
{
prod *= i;
}
return prod;
}

A. Monitor the value of the variable prod, set a breakpoint at the line prod *= i;, and then run in a single step until the cause of the error is found
B, monitor the value of the variable prod, set a breakpoint at the line return prod;, after the program is interrupted , you can find the cause of the error C, set a breakpoint at prod=1;, and then find the cause of the error D
in the function call stack , monitor the value of the variable i, in for (i=1; i<=n; i++
) set a breakpoint at the line, and then run a single step until the cause of the error is found

Analysis: According to the known situation of the topic, when n<=12, the result is correct, indicating that there is a problem in the calculation process with the increase of parameters, so set a breakpoint at prod *= i; to check the reason . The cause of the error is integer overflow when the data is too large, so the answer is A

3. What are the possible output results of the following codes [multiple choice] ( )

#include <stdio.h>
typedef union
{
	int a;
	struct
	{
		short b;
		short c;
	};
}X;
int main()
{
	X x;
	x.a = 0x20150810;
	printf("%x,%x\n", x.b, x.c);
    return 0;
}

A、2015,810      B、50810,201     C、810,2015    D、20150,810

Analysis: For 0x20150810
, if it is stored in big-endian mode:

From low address to high address: 20 15 08 10 Output from low address to high address: 20 15 08 10
If stored in little endian mode:

From low address to high address: 10 08 15 20 Output from high address to low address: 08 10 20 15
This number is assigned to the union xa in int type, and the structure members b and c are accessed separately to get the low address 2 bytes and 2 bytes of the high address, the big endian is 2015 and 810, the little endian is 810 and 2015, so the answer is AC

4. After running the following program, if you input 65 14 <Enter> from the keyboard, the output will be ( )

#include<stdio.h
int main()
{
int m, n;
printf("Enter m,n\n");
scanf("%d %d", &m,&n);
while (m!=n) //1
{
while(m>n) m=m-n; //2
while(n>m) n=n-m; //3
}
printf("m=%d\n",m);
return 0;
}

A、3    B、2     C、1     D、0 

Analysis: Observing the code, it is found that it mainly realizes a function, which is to subtract the two input numbers from each other until they are equal, and stop the loop. The data is not large, so you can directly substitute it in, 65-14=51, 51-14=37, 37-14=23, 23-14=9, 14-9=5, 9-5=4, 5-4=1, 4-1=3, 3-1=1, 2- 1=1, 1==1 terminates the loop and prints 1, so the answer is C. When the data is not large, we can directly substitute the result, but when the data is large to a certain extent, we still need to analyze the relationship between the two data, and analyze the specific situation.

5. If you enter ADescriptor<Enter> from the keyboard when running the following program, the running result of the following program is ( )

#include <stdio.h>
int main()
{
	char c;
	int v0 = 0, v1 = 0, v2 = 0;
		do
		{
			switch (c = getchar())
			{
			case'a':case'A':
			case'e':case'E':
			case'i':case'I':
			case'o':case'O':
			case'u':case'U':v1 += 1;
			default: v0 += 1; v2 += 1;
			}
		} while (c != '\n');
		printf("v0=%d,v1=%d,v2=%d\n", v0, v1, v2);
		return 0;
}

A、v0=7,v1=4,v2=7 B、v0=8,v1=4,V2=8    C、v0=11,v1=4,v2=11 D、v0=12,v1=4,v2=12 

Analysis: Observing the code, it is found that the main composition is a loop wrapping a switch, its switch statement reads from the characters we input, and performs a series of calculations through the read characters. But one thing to note is that there is no break in its switch, that is to say, once it executes the above, it will keep going down until the end, and then pass the loop judgment. ADescriptor<Enter>. A total of 11 characters, a, e, i, o, u case, add up to 4, let v1=v0=v2=4, the remaining 7 characters make v0+7, v2+7, so finally v1= 4, v0=v2=11, it looks like this, but one thing to note is that it is a dowhile loop, that is, the content of the loop is executed first and then judged, that is to say, the carriage return of '\n' also executes 1 times, finally let v1=4, v0=v2=12, choose D

 Programming question 1:

9. Palindrome - LeetCode  

Idea: The last digit is stripped out through the calculation of the modulus, while the next digit is continuously searched by /10, the stored upside-down number is continuously *10 to restore the digit, and the final comparison is enough. One thing to note is that negative numbers cannot be palindromic numbers. 

bool isPalindrome(int x) {
    if (x < 0)
        //当x为-1时,倒过来为1-,可看出,负数不可能为回文数
    {
        return false;
    }
    long long int sum = 0; long long int x1 = x;
    while (x1)
    {
        sum = sum * 10 + x1 % 10;
        x1 /= 10;
    }
    if (sum == x)
    {
        return true;
    }
    else
        return false;
}

Programming question 2:

LeetCode official website - a technology growth platform loved by global geeks

Idea: Judging by the number of US$5 and US$10 bills in our hands, we need to pay attention to that we have no money at the beginning, so we can only accept US$5, and we will not be able to find anything else. 

bool lemonadeChange(int* bills, int billsSize) {
    //一开始我们是穷光蛋,一块钱都找不出
    if (bills[0] != 5)
        return false;
    int five = 0; int ten = 0;
    //只需要计算5块钱和10块钱的数目即可,因为找钱不可能拿20找
    int i = 0;
    for (i = 0; i < billsSize; i++)
    {
        if (bills[i] == 5)
            //给我们5美元的时候,直接收就行
        {
            five += 1;
        }
        if (bills[i] == 10)
            //10美元就一种情况,找5美元,收10美元
        {
            five -= 1;
            if (five < 0)
            {
                return false;
            }
            ten += 1;
        }
        if (bills[i] == 20)
            //两种情况,找3张5或1张10和1张5
        {
            if (five >= 3 && ten < 1)
            //确实没有办法了,就找3张5出去,可以找的时候找1张5和1张10
            //因为5相比10适用范围更大
            {
                five -= 3;
            }
            else
            {
                five -= 1;
                ten -= 1;
            }
            if (five < 0 || ten < 0)
            {
                return false;
            }
        }
    }
    return true;
}

  Well, today's practice is over here, thank you friends for visiting, I wish you all a bright future O(∩_∩)O

Guess you like

Origin blog.csdn.net/fq157856469/article/details/132402270