[C Brushing Questions] day3

1. Multiple choice questions

1. The prototype of the known function is: int fun(char b[10], int *a);, assuming the definition: char c[10];int d;, the correct calling statement is ()

A: fun(c,&d); B: fun(c,d); C: fun(&c,&d); D: fun(&c,d);

【Answer】:

A

[Analysis]:

Test point: The use of & (you can read this article: A trick to teach you when to add & and when not to add & to scanf )

If you want to pass the address:

If the address information cannot be expressed by itself, add &;

It itself represents address information, and there is no need to add &

The c array originally represents the address, so there is no need to add &

d is just a variable, but the second parameter of fun needs to pass the address, so take out the address of d, that is, &b


2. Which of the following expressions will be prohibited by the compiler [Multiple choices] ( )
nt a = 248, b = 4;
int const *c = 21;
const int *d = &a;
int *const e = &b;
int const * const f = &a;

A: *c = 32; B: *d = 43 C: e=&a D: f=0x321f
[Answer]:

ABCD

[Analysis]:

Test point: pointer constants and constant pointers

const is on the left side of *: it means that the pointer points to a constant, then *c and *d cannot be modified

const is on the right side of *: it means that the pointer is a constant pointer, so e and f cannot be modified.

To sum up: it is what follows const, and it cannot be changed directly.


3. The output result of the following program is ( )
#include <stdio.h>
int i;
void prt()
{
   for (i = 5; i < 8; i++)
   printf("%c", '*');
   printf("\t");
} 

int main()
{
   for (i = 5; i <= 8; i++)
   prt();
   return 0;
}

A: *** B: *** *** *** *** C: *** *** D: * * *
[Answer]:

A

[Analysis]:

Test point: logic of for loop + global variables

The logic of the entire code:

i=5 in the main function enters prt() and enters another for loop again,

i=5 prints a *, i++

i=6 prints a *, i++

i=7 prints a *, i++

i=8 , the loop condition i<8 is not satisfied, the loop ends and returns to the main function.

When returning to the main function, i=8. At this time, the for loop i=5 in the main function has ended. i++

So i=9 , then the conditions of the for loop are not met, and the loop is jumped out.


4. The output of the following code segment is ( )
int main()
{
  int a=3;
  printf("%d\n",(a+=a-=a*a));
  return 0;
}

A: -6 B: 12 ​​C: 0 D: -12
[Answer]:

D

[Analysis]:

Test point: operator priority

The priority of the assignment operator is very low , so calculate a*a first and get 9

Then the expression is a+=a-=9, that is, a=a+ (a=a-9) (a=3 at this time)

Then a=a+(-6) (a=-6 at this time), then a=-6+(-6)=-12


5. Which of the following cannot achieve an infinite loop ( )

A: while(1){ } B: for(;1;){ } C: do{ }while(1); D: for(;0;){ }

【Answer】:

D

[Analysis]:

Test points: In C language, 0 means false and non-0 means true.

If the first three judgment conditions are all 1 and always true, then there will be an infinite loop.

The last judgment condition is 0, which is always false and will not enter the loop directly.


2. Programming questions

1. Record negatives and positives

e25f594061a64f0c9d745b70fd6768d0.png

[Reference answer]: 

When n integers are input, the sum will be added once the input is entered.

Note: The case where the dividend is 0 must be considered separately

#include <stdio.h>
int main() 
{
    int n = 0;
    int count1 = 0;
    int count2 = 0;
    int a = 0;
    double sum = 0;
    scanf("%d", &n);
//输入n个整数
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a);
       //统计正数
        if (a > 0)
        {
            sum += a;
            count1++;
        }
       //统计负数
        else if (a < 0)
            count2++;
    }
    //注意被除数为0的情况
    if (count1 != 0)
        printf("%d %.1lf", count2, sum / count1);
    else
        printf("%d 0.0", count2);
    return 0;
}

2. Minimum number of rotated array

fac41414552240aabdf74b1162dcb810.png

[Reference answer]:  

Examination of this question: binary search

Idea: Continuously improve on the basis of normal binary search

First, we found through the rules that since the original array is in ascending order, the minimum value of the rotated array must be in ascending order on the left and in descending order on the right.

Then comes the looping part:

(1) When the middle value is greater than the rightmost value, it means: mid is to the left of the minimum value

(2) The middle value is less than the rightmost value: mid is to the right of the minimum value (or is mid)

Note 1:

This cannot be like a normal binary search, left=mid+1;right=mid-1; (+1 or -1 may skip min)

Then let left=mid; right=mid,

It turns out that this can never break out of the loop.

Keep experimenting and let right=mid-1

Note 2:

If you write it like above, the result is still incorrect

eg:【2,2,2,1,2】

In this case, the middle value is equal to the rightmost value, so let right-- (can only be reduced one by one, and the minimum value will be skipped if it is large)

 * @param nums int整型一维数组 
 * @param numsLen int nums数组长度
 * @return int整型
 */
int minNumberInRotateArray(int* nums, int numsLen )
{
   int left=0;
   int right=numsLen-1;
   //找有升序有降序(最小值的左边升序,右边降序)
   while(left<right)
   {

    int mid=(left+right)/2;

    if(nums[mid]>nums[right])//中间值大于最右边说明:mid在最小值的左边
    {
        left=mid+1;
    }
    else if(nums[mid]<nums[right])//中间值小于最右边说明:mid在最小值的右边(或者就是mid)
    {
        right=mid;//这里不能直接像二分查找一样right=mid-1
    }
    else //这里是为了排除重复的情况,但是又不能直接right=mid,就一个一个减
    {
       right--;
    }
   }
   return nums[left];
    
}

Guess you like

Origin blog.csdn.net/qq_73017178/article/details/132979595
Recommended