C language - two solutions for BC29 school start

describe

Little S: School can finally start! I'm so happy!

Little Y: Didn’t you read the news? The start of school has been postponed again.

小S:NOOOOOOOOOOOOOOOOO!

Little S knows that the school was originally scheduled to start on Sunday X and was notified that the start of school has been postponed for N days. What day of the week is the start of school (Sunday is represented by 7)?

Enter description:

The input contains two numbers X, N (1≤X≤7, 1≤N≤1000).

Output description:

Output a number indicating the day of the week when the school starts.

Common solution:

#include <stdio.h>

int main() {
    int X,N;
    //输入
    scanf("%d %d",&X,&N);
    //计算
    int m=(X+N-1)%7+1;

    printf("%d",m);
    return 0;
}

Solution using conditional operators:

Problem-solving idea : Use the true or false judgment of the conditional operator. If (X+N)%7 is within the range of Monday to Saturday, it can be printed directly. If not, it must be Sunday and print 7

#include <stdio.h>

int main() {
    int X, N;
    //输入
    scanf("%d %d", &X, &N);
    //计算
    int m = (X + N) % 7;

    printf("%d", m ? m : 7);
    return 0;
}

Conditional operator concept :

sxp1 ? sxp2 : sxp3

True √ × gets sxp2

False × √ gets sxp3

Guess you like

Origin blog.csdn.net/weixin_70464416/article/details/131704914