C language written test training [the fourth day]

Article Directory

1. Assuming that the variable has been correctly defined, the following program segment that cannot count the number of input characters (excluding the carriage return) in a line is ( )

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

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

4. The following function is to find the greatest common divisor of two int numbers, and point out the problems existing in it [multiple choices] ( )

5. Execute the following program segment, the execution times of statement 3 is ( )

6. Collection of errors

Idea 1: Bitwise XOR

Idea 2: Sort and count

7. Password check

ASCII code table


  Hello everyone, I am Ji Ning.

  Today is the fourth day of C language written test training, come on!

1. Assuming that the variable has been correctly defined, the following program segment that cannot count the number of input characters (excluding the carriage return) in a line is ( )

A: n=0; while(ch=getchar()!='\n')n++;   B: n=0; while(getchar()!='\n')n++;

C: for(n=0; getchar()!='\n'; n++);   D: n=0; for(ch=getchar();ch!='\n';n++); 

  This question is relatively simple. If you look carefully at the question, you will find that the value of the variable ch in option D has not changed, so there is no function of counting the number of characters entered in a line. Choose D for this question

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

int main()
{
  int m, n;
  printf("Enter m,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

  This question tests the degree of seriousness, and the question is not difficult.

 

  The final result is m=n=1, so choose C  for this question

3. 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 

   The knowledge point of this question is about the switch...case statement 

  When there is no break behind the case in the switch...case statement, the program will continue. The ADescriptor is 11 characters, plus the carriage return, a total of 12 characters. There are 4 equal to the vowel aeiou, so v1=4, and whether they are equal to the vowel or not, they will all experience default, so v0 and v2 are both 12, this question chooses D

4. The following function is to find the greatest common divisor of two int numbers, and point out the problems existing in it [multiple choices] ( )

int gcd(char x,char y)
{
 int min = x < y ? x : y;
 for (min = 0; min > 0; min--)
   if (x % min = 0 && y % min = 0)
     return min;
}

A: The parameter type is incorrect B: The initial value of the loop variable min is incorrect

C: The symbol for judging equal to is wrong D: The return type is wrong

A. The actual parameter of the function is int, but the formal parameter is incorrect, and data will be truncated and lost.

B.min is updated to 0 at the beginning of the for loop, and is no longer the smaller value of the two formal parameters;

C. Mistakenly write == as the = assignment operator when judging whether it is divisible;

D. The function will eventually return an int value, and the return value type is fine, but here I want to emphasize a problem that is not written in the option. If it is a question on Niuke.com, a compilation error will be reported, saying that the function is not available in all cases All have return values, and only
return values ​​when the  if condition is true ; but generally this situation can pass on vs, and the compiler will give a default return value.

So choose ABC  for this question

5. Execute the following program segment, the execution times of statement 3 is ( )

for(i = 0; i <= n-1; i++)  // (1)
  for(j = n; j > i; j--)  // (2)
    state;        // (3)

A: n(n+2)/2    B: (n-1)(n+2)/2    C: n(n+1)/2    D: (n-1)(n+2) 

  The outer loop has n times, when i=0, the inner loop is n times, when i=1, the inner loop is n-1 times, when i=2, the inner loop is n-2 times, and so on, the total number of times It is n+(n-1)+(n-2)+......+2+1, which is an arithmetic sequence, obtained from the arithmetic sum formula, equal to n(n+1)/2, so Choose C for this question

6.  Wrong Collection

  The set s contains integers from 1 to n. Unfortunately, due to data errors, a certain number in the set was copied to the value of another number in the set, causing the set to lose a number and have a number repeated. Given an array nums represents the result of the set S after an error occurs. Please find the repeated integers, find the missing integers, and return them in the form of an array. This question comes from leetcode

Idea 1: Bitwise XOR

  The first idea of ​​this question is to construct an array containing the correct integers from 1 to n, and then perform bitwise XOR of all the numbers in these two numbers, because except for the repeated and missing number, other The numbers of all appear in pairs, so the result of the bitwise XOR is the result of the bitwise XOR of the two numbers in the answer.

  After getting this result, calculate the first binary digit 1 of the result from the back to the front, and then record the shifted binary digit, and use the result of the binary digit &1 corresponding to this digit as the grouping standard, and combine the two arrays The numbers inside will be divided into two groups: the binary bit positions are 1 and 0, because the origin of the classification is the result of the bitwise XOR of the two required numbers, so the two of them will definitely be assigned to different group, and other numbers that meet the requirements appear in pairs, and the result of bitwise XOR is 0, then these two numbers are obtained. For details, refer to the detailed explanation of the ^ (bitwise XOR) operator,  which is similar to the advanced version of the single dog problem in it.

  Finally, it is only necessary to judge the order in which these two numbers are output.

int* findErrorNums(int* nums, int numsSize,int*returnSize) {
    int* nums2 = (int*)malloc(sizeof(int) * numsSize);
    int* nums3 = (int*)malloc(sizeof(int) * numsSize*2);
    int* nums4 = (int*)malloc(sizeof(int) * numsSize*2);
    int i = 0, ret = 0, pos = 0;
    for (i = 0; i < numsSize; i++)
    {
        nums2[i] = i + 1;
    }
    for (i = 0; i < numsSize; i++)
    {
        ret ^= nums[i];
        ret ^=nums2[i];
    }
    while ((ret & 1) != 1)
    {
        ret = ret >> 1;
        pos++;
    }
    int count1= 0,count2=0;
    int j = 0;
    int z = 0;
    for (i = 0; i < numsSize; i++)
    {
        if (((nums[i] >> pos) & 1) == 1)
        {
            nums3[j] = nums[i];
            j++;
            count1++;
        }
        else
        {
            nums4[z] = nums[i];
            z++;
            count2++;
        }
        if (((nums2[i] >> pos) & 1) == 1)
        {
            nums3[j] = nums2[i];
            j++;
            count1++;
        }
        else
        {
            nums4[z] = nums2[i];
            z++;
            count2++;
        }
    }
    int a1 = 0, a2 = 0;
    for (i = 0; i < count1; i++)
    {
        a1 ^= nums3[i];
    }
    for (i = 0; i < count2; i++)
    {
        a2 ^= nums4[i];
    }
    int* arr = (int*)malloc(sizeof(int) * 2);
    for (i = 0; i < numsSize; i++)
    {
        if (a1 == nums[i])//a1重复
        {
            arr[0] = a1;
            arr[1] = a2;
            return arr;
        }
    }
    *returnSize=2;
    arr[0] = a2;
    arr[1] = a1;
    free(nums2);
    free(nums3);
    free(nums4);
    return arr;
}

Idea 2: Sort and count

  First use the qsort function to sort to ensure that the original array is not in descending order, then define an array to record the number of occurrences of each number in the original array, and ensure one-to-one correspondence, then the value of the lost array position in the new array is 0, The value of the position where the number is repeated is 2, which corresponds to the subscript of the original array.

 int Comper(const void* p1, const void* p2)
{
	return (*(int*)p1) - (*(int*)p2); 
}

 int* findErrorNums(int* nums, int numsSize, int* returnSize){
    int* a = (int*)malloc(sizeof(int)*(numsSize));
    qsort(nums, numsSize, sizeof(int), Comper);
    for(int i = 0; i < numsSize; i++){     
        a[i] = 0;
    }
    for(int i = 0; i < numsSize; i++){    
        a[nums[i]-1]++;
    }
    int* res = (int*)malloc(sizeof(int)*2);
    for(int i = 0; i < numsSize; i++){
        if(a[i] == 2){
            res[0] = i+1;
        }
        if(a[i] == 0){
            res[1] = i+1;
        }
    }
    *returnSize = 2;
    return res;
}

qsort function usage guide: qsort function

7. Password check

  Xiao Ming recently developed a website. When a user registers an account, he needs to set a password for the account. In order to strengthen the security of the account, Xiao Ming has certain requirements for the strength of the password:

1. The password can only be composed of uppercase letters, lowercase letters and numbers;

2. The password cannot start with a number;

3. At least two of the three character types of uppercase letters, lowercase letters and numbers appear in the password;

4. Password length should be at least 8

Now Xiao Ming has received n passwords, and he wants you to write a program to judge which of these passwords are suitable and which are illegal.

    This question is simpler than the previous question, you only need to hold the ASCII code and keep clicking. 

void Password_Check(char* str) {
    int len = strlen(str);
    char* ps = str;
    if (len > 100 || len < 8) {
        printf("NO\n");
        return;
    }
    if (*str <= 57 && *str >= 48) {
        printf("NO\n");
        return;
    }
    do {
        if ((*ps < 48) || 
            (*ps > 57 && *ps < 65) ||
            (*ps > 122) ||
            (*str > 90 &&*str < 97)) {
            printf("NO\n");
            return;
        }
    } while (*++ps);
    ps = str;
    int count = 0;
    do {
        if (*ps <= 57 && *ps >= 48) {
            count++;
            break;
        }
    } while (*++ps);
    ps = str;
    do {
        if ((*ps <= 90 && *ps >= 65)) {
            count++;
            break;
        }
    } while (*++ps);
    ps = str;
    do {
        if (*ps >= 97 && *ps <= 122) {
            count++;
            break;
        }
    } while (*++ps);
    if (count < 2)
    {
        printf("N0\n");
        return;
    }
    printf("YES\n");
}


int main() {
    int n = 0;
    scanf("%d", &n);
    while (n--) {
        char* str = (char*)malloc(sizeof(char) * 101);
        scanf("%s", str);
        Password_Check(str);
        free(str);
    }
    return 0;
}

ASCII code table

In the standard ASCII code table, the ASCII code of the known English letter A is 01000001, and the ASCII code of the English letter D is_Baidu Know

Guess you like

Origin blog.csdn.net/zyb___/article/details/132152878