C language daily practice (13)

Foreword:
Daily training 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 the academic situation will be updated during the school period.

 Five multiple choice questions:

1. The result of the program running is ()

#include<stdio.h>
int main()
{
int x=1.9;
x++;
printf("%d",x);
}

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

Analysis: A simple question that examines your understanding of integer variables. If the initial value of an integer variable is assigned a decimal, then the decimal part will be directly discarded on the vs compiler, that is to say, x in Code 1 Assign 1.9, actually just assign 1 to x, then go to x++, make x=2, so the final printed result is 2, choose B 

2. There are integer variables a, b, and c whose initial value is 1. After running the expression ++a| |++b&&++c, the values ​​of a, b, and c are respectively ()

A、2,1,1       B、2,2,2     C、2,2,1      D、2,1,2

Analysis: error-prone questions, examine the use of || (or) operation and && (and) operation, as long as one side of || operation is true, it is true, so when we judge that one side is true, the other side does not need to be judged again. The computer just skips it. && operation, both sides are true to be true, as long as one side is false, it is false, so once one side is found to be false, the other side will not be judged.

Follow the rules, the first thing you encounter is ++a, use ++ first, a=2, 2 is true, it is the || operation, so the latter part is not performed, even the && is not performed, so the last a =2, b=1, c=1, choose A

3. The result of the following program is ()

#include<stdio.h>
int main()
{
	int x = 5;
	int i = 0, j = 0, k = 0;
	x = (i = 4, j = 16, k = 32);
	printf("%d %d %d %d", x,i,j,k);
}

A、5,4,16,32     B、5,0,0,0    C、32,4,16,32    D、32,0,0,0

Analysis: Check your understanding of comma expressions. Comma expressions are performed from left to right, and the result of the entire expression is the last expression.

Going from left to right, first assign i a value of 4, then assign j a value of 16, and finally come to the last expression, k=32, and according to the operation of the comma expression, the result of the entire expression is also k=32, So in the end, x=k=32, and assign values ​​​​to x and k at the same time, so x and k are both 32 in the end. In summary, i=4, j=16, x=k=32, so the final printed result Choose C for 32, 4, 16, 32

4. There is the following program, and the output after the program runs is ()

#include <stdio.h>
int main()
{
int x = 072;
printf("%d\n", x+1);
}

A、72      B、73       C、115       D、59

 Analysis: basic questions, examine your control over the details, octal numbers have a prefix of 0, hexadecimal numbers have a prefix of 0x, and decimal numbers have no prefix. So the value assigned to x by the program is an octal 72, converted to decimal is 2*1+7*8=58, and finally prints out x+1 which is 59, so choose D

5. If you want to define int variables a, b, c, and d and assign them all as 1, the wrong ones in the following writing
are
A, int a=b=c=d=1;
B, int a=1, b=1 , c=1, d=1;
C, int a,b,c,d; a=b=c=d=1;
D, int a,b,c,d=1; a=b=c=d ;

Analysis: Go one by one, look at option A first, the operation of option A does not define the three integer variables b, c, and d, so A is wrong, option B is defined one by one, no problem, B is correct. C options are all defined before assigning, C is correct, D option, first define the four variables a, b, c, d and assign 1 to d, and finally set a=b=c=d, set a, b, c It is equal to d, so in the end, a, b, c, d are all 1, and D is correct. In summary, choose A

Programming question 1: 

Sword Pointer Offer 10- I. Fibonacci Sequence- LeetCode

Tip: Remember to take the model, the blogger didn’t take the model at the beginning, I thought about it for a long time, haha 

int fib(int n) {
    if (n == 0)
    {
        return 0;
    }
    if (n <= 2)
    {
        return 1;
    }
    //以上特殊情况单独处理下
    int f1 = 1; int f2 = 1; int f3 = 0; int i = 0;
    for (i = 3; i <= n; i++)
        //我们一开始计算的就是第三个斐波那契数,所以令i=3
    {
        f3 = (f1 + f2) % 1000000007;
        //先储存下一个斐波那契数
        f1 = f2;
        f2 = f3;
        //进行数据更新
    }
    return f3;
    //返回目标
}

Programming question 2: 

134. Gas Station - LeetCode

Tip: This question is difficult. It is recommended to come here after learning the greedy algorithm. Here, the blogger did not use the greedy algorithm, but used the most unpretentious method to pass it. The purpose is to let everyone feel the rigor of the code. Copying the blogger's code sometimes fails, because the blogger didn't use skills to answer the questions. When your network is not good, it may fail because the time limit is exceeded. Just try a few more times. 

 

int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) {
    int oil = 0;
    int i = 0;
    if(gasSize==1)
    //只有一个单独处理
    {
      if(gas[0]>=cost[0])
      return 0;
    }
    for (i = 0; i < gasSize; i++)
    {
        int tmp = i;
        //不要在函数内对循环变量操作,因此使用tmp来间接操作
        int flag = 1;
        //判定是否可以行进的变量
        oil = gas[tmp];
        if (oil > cost[tmp])
        {
            oil -= cost[tmp];
            for (++tmp; tmp < gasSize; tmp++)
            {
                oil += gas[tmp];
                oil -= cost[tmp];
                if (oil < 0)
                {
                    flag = 0;
                    break;
                }
            }
            if (flag == 0)
            {
                continue;
            }
            for (tmp = 0; tmp < gasSize; tmp++)
            {
                oil += gas[tmp];
                oil -= cost[tmp];
                if (tmp == i)
                {
                    break;
                }
                if (oil < 0)
                {
                    flag = 0;
                    break;
                }
            }
            if (flag == 1)
            {
                return tmp;
            }
            else
            {
                continue;
            }
        }
        else
        {
            continue;
        }
    }
    return -1;
}

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/132645813