C language: a daily exercise (selection + programming)

Table of contents

 Multiple choice questions:

Question one:

Question two:

Question three: 

Question four: 

Question five:

Programming questions:

Question 1: Print 1 to the largest n digits

Example 1

Idea one:

Question 2: Calculate the date to day conversion

Example 1

 Idea one:

My limited strength may not explain and understand some places clearly enough, you can try to read the code yourself, or point out mistakes in the comment area, hope Haihan!


 Multiple choice questions:

Question one:

1. Execute the following program, the correct output is ( )
int x=5,y=7;
void swap()
{         int z;         z=x;         x=y;         y=z; }




int main()
{
        int x=3,y=8;
        swap();
        printf("%d,%d\n",x, y);
        return 0;
}

 A: 5,7         B: 7,5         C: 3,8         D: 8,3

Question two:

2. The following incorrect definition statement is ( )
A: double x[5] = {2.0, 4.0, 6.0, 8.0, 10.0};
B: char c2[] = {'\x10', '\xa', ' \8'};
C: char c1[] = {'1','2','3','4','5'}; D:
int y[5+3]={0, 1, 3 , 5, 7, 9};

Question three: 

3. The test.c file includes the following statement. Among the four variables defined in the file, the variable of pointer type is [multiple choices] ( ) 

#define INT_PTR int*
typedef int* int_ptr;
INT_PTR a, b;
int_ptr c, d;

 A: a         B: b         C: c         D: d

Question four: 

4. If the conditional expression (M)?(a++):(a--) is given, the expression M ( )
A: and (M==0) are equivalent to B: and (M==1), etc. price

C: Equivalent to (M!=0)          D: Equivalent to (M!=1)

Question five:

5. If there is the following definition statement, the correct input statement is [multiple choice] ( )
int b;
char c[10];

A: scanf("%d%s",&b,&c);         B: scanf("%d%s",&b,c);
C: scanf("%d%s",b,c);              D: scanf("%d%s",b,&c); 

Programming questions:

Question 1: Print 1 to the largest n digits

Example 1

enter:

1

return value:

[1,2,3,4,5,6,7,8,9]

Idea one:

        Create a pointer array a , use num to record the number of elements that need to be input , input the values ​​from 1 to num to each array subscript, and return the pointer a .

Note: returnSize here refers to the number of array elements!

int* printNumbers(int n, int* returnSize ) 
{
    int i = 0;
    int num = 1;
    int* a;
    //计算元素个数
    for(i = n;i != 0;i--)
    {
        num *= 10; 
    }
    *returnSize =num-1;
    //开辟需要存储的个数的空间
    a = (int*)malloc(sizeof(int)*(*returnSize));
    for(i = 1;i < num;i++)
    {
        a[i-1] = i;
    }
    return a;
}

Question 2: Calculate the date to day conversion

Example 1

enter:

2012 12 31

output:

366

 Idea one:

        Create year, mon, day for scanf() , and month[] to record the number of days in each month , then judge whether the year is a leap year, and then sum the number of days.

#include <stdio.h>

int main() 
{
    int year = 0;
    //保存每月天数
    int month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int day = 0;
    int mon = 0;
    int sum = 0;
    scanf("%d%d%d",&year,&mon,&day);
    //判断是否是闰年
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    {
        month[2] = 29;
    }
    //求目标月份前的天数和
    for(int i = 1;i < mon ;i++)
    {
        sum += month[i];
    }
    printf("%d",sum+day);


    return 0;
}

My limited strength may not explain and understand some places clearly enough, you can try to read the code yourself, or point out mistakes in the comment area, hope Haihan!

Guess you like

Origin blog.csdn.net/weixin_71964780/article/details/132336357