hero-image
Last updated on

2022级《程序设计基础I》实验2-选择结构程序设计


求绝对值(选择结构)

problemId:1117

Description

从键盘上输入任意一个整数,然后输出它的绝对值!

Input

从键盘上输入任意一个整数。

Ouput

输出它的绝对值。

samples

<input>—> -4 <output>—> 4

Code

#include <stdio.h>
int main(){
    int a;
    scanf("%d",&a);
    if(a>=0)
    {
        printf("%d",a);
    }
    else
    {
        printf("%d",-a);
    }
    return 0;
}

时间间隔

problemId:1177

Description

从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示。

如:3点5分25秒应表示为—03:05:25.假设两个时间在同一天内,时间先后顺序与输入无关。

Input

输入包括两行。

第一行为时间点1。

第二行为时间点2。

Ouput

以“小时:分钟:秒”的格式输出时间间隔。

格式参看输入输出。

samples

<input>—> 12:01:12 13:09:43 <output>—> 01:08:31

Code

#include <stdio.h>
int main(){
    int h,m,s;
    int h1,m1,s1;
    int minute;
    scanf("%d:%d:%d",&h,&m,&s);
    scanf("%d:%d:%d",&h1,&m1,&s1);
    minute=(h1*3600+m1*60+s1)-(h*3600+m*60+s);
    if(minute<=0)
    {
        minute=-minute;
    }
    h1=minute/3600;
    m1=(minute-3600*h1)/60;
    s1=minute-3600*h1-60*m1;
    printf("%02d:%02d:%02d",h1,m1,s1);
    return 0;
}

求两个整数之中较大者

problemId:1153

Description

输入两个整数,请编程求其中的较大者。

Input

在一行中输入用空格隔开的两个整数,例如5 9。

Ouput

输出两个整数之中较大者,输出形式举例:max=9。

samples

<input>—> 5 9 <output>—> max=9

Code

#include <stdio.h>
int main(){
    int a,b;
    scanf("%d %d",&a,&b);
    if (a>=b)
    {
        printf("max=%d",a);
    }
    else{
        printf("max=%d",b);
    }
    return 0;
}

小鑫吃苹果

problemId:3105

Description

每年平安夜的时候妈妈都会给小鑫邮寄两个大苹果,两个苹果的重量分别为xy。以前小鑫都是自己默默的吃掉两个大苹果,但是这次小鑫决定要把最重的苹果送给他的女神。可惜他比较笨分不出哪个苹果重哪个苹果轻,所以请你帮他找到最重的苹果,输出最重的重量。

### Input 单组输入。 

两个正整数表示苹果的重量xy(1 <= (x, y) <= 1000)

Ouput

 输出两个苹果中最重的重量。

samples

<input>—> 100 200 <output>—> 200

Code

#include <stdio.h>
int main(){
    int a,b;
    scanf("%d %d",&a,&b);
    if (a>=b)
    {
        printf("%d",a);
    }
    else{
        printf("%d",b);
    }
    return 0;
}

小鑫の日常系列故事(一)——判断对错

problemId:2732

Description

话说小鑫可是一个奇人,在他刚出生的时候,就能口算出1000000以内的加法。因为他有这样一项能力,他的父母专门雇佣了一位可爱的保姆姐姐(内部消息不超过二十岁哦)来训练他。可是这位保姆姐姐有时候脑袋会秀逗一下,如果被小鑫的父母发现了可是要丢掉工作的。于是她找到了身为程序员的你,你能用你的双手来帮助他解决问题么?

### Input  输入有两行,第一行为两个整数a,b(a,b>0)。第二行为一个数,为小鑫对于a+b口算出的答案。 ### Ouput  输出为一行。判断小鑫给出的答案是否正确,如果是输出“YES”,否则输出“NO”。(输出不包括引号) ### samples \--> 1 2 3 \--> YES ### Code ``` #include int main(){ int a,b,c; scanf("%d%d%d",&a,&b,&c); if(a+b==c){ printf("YES"); } else{ printf("NO"); } return 0; } ``` # 小鑫追女神 problemId:3102 ### Description

小鑫长得比较丑,但还是对女神垂涎不止,小鑫向女神表白了。女神毕竟是女神,女神的世界里,只有010代表女神拒绝了他,1代表女神接受了他。现在你需要判断女神到底是接受了他还是拒绝了他。若接受,输出“I like you”(不包括引号),若拒绝,输出“He he”(不包括引号)。

### Input 单组输入。

 输入只有一个数,保证只有01

Ouput

 输出女神对小鑫的态度,“I like you”(不包括引号)或“He he”(不包括引号)

samples

<input>—> 0 <output>—> He he

Code

#include <stdio.h>
int main(){
    int a;
    scanf("%d",&a);
    if(a==1)
    printf("I like you");
    else
    printf("He he");
    return 0;
}

求三个整数的最大值

problemId:1154

Description

请编写程序,输入三个整数,求出其中的最大值输出。

Input

在一行上输入三个整数,整数间用逗号分隔。

Ouput

输出三个数中的最大值。

samples

<input>—> 5,7,9 <output>—> max=9

Code

#include <stdio.h>
int main(){
    int a,b,c;
    scanf("%d,%d,%d",&a,&b,&c);
    if(a<=b){
        a=b;
    }
    if(a<=c){
        a=c;
    }
    printf("max=%d",a);
    return 0;
}

相加和最大值

problemId:1148

Description

输入三个整数a,b,c。并进行两两相加,最后比较相加和的最大值。

Input

输入数据包含三个整数,用空格分开。

Ouput

输出两两相加后的最大值。

samples

<input>—> 1 2 3 <output>—> 5

Code

#include <stdio.h>
int main(){
    int a,b,c;
    int a1,b1,c1;
    scanf("%d %d %d",&a,&b,&c);
    a1=a+b;
    b1=a+c;
    c1=b+c;
    if(a1<=b1){
        a1=b1;
    }
    if(a1<=c1){
        a1=c1;
    }
    printf("%d",a1);
    return 0;
}

时间格式转换

problemId:3765

Description

24 小时制的时间格式为 "HH:mm",如 “05:20”,而 12 小时制的时间格式为 "h:mm AM/PM",如 "5:20 AM"。

24 小时制到 12 小时制的对应关系如下:

  • 0 时:12 时 (AM)
  • 1~11 时:1~11 时 (AM)
  • 12 时:12 时 (PM)
  • 13~23 时:1~11 时 (PM)

例如:"00:00" 对应 "12:00 AM","01:20" 对应 "1:20 AM","12:35" 对应 "12:35 PM","13:17" 对应 "1:17 PM","23:59" 对应 "11:59 PM"。

现在给你一个 24 小时制的时间,请你编写程序将其转换为 12 小时制的时间。

### Input

输入只有一行,包含一个 24 小时制的时间。

### Ouput

输出一行,表示转换为 12 小时制以后的时间。其中分钟部分若不足两位需要加 0 补足两位。

### samples \--> 00:05 \--> 12:05 AM ### Code ``` #include int main(){ int h,m; scanf("%d:%d",&h,&m); if (h==0) { printf("12:%02d AM",m); } else if (h>0 && h<12) { printf("%d:%02d AM",h,m); } else if (h==12) { printf("12:%02d PM",m); } else printf("%d:%02d PM",h-12,m); return 0; } ``` # 从大到小输出a、b、c(选择结构) problemId:1118 ### Description

从键盘输入三个整数a、b、c,要求将输出的数据按从大到小排序后输出。

Input

从键盘上输入三个整数a、b、c,每个整数之间用空格分开。

Ouput

从大到小顺序输出a、b、c的值。

samples

<input>—> 4 3 5 <output>—> 5 4 3

Code

#include <stdio.h>
int main(){
    int a,b,c,tmp;
    scanf("%d %d %d",&a,&b,&c);
    if (a<=b)
    {
        tmp=a;
        a=b;
        b=tmp;
    }
    if (c>a)
    {
        printf("%d %d %d",c,a,b);
    }
    else if (c<b)
    {
        printf("%d %d %d",a,b,c);
    }
    else
    {
        printf("%d %d %d",a,c,b);
    }    
    return 0;
}

Code

#include <stdio.h>
int main(){
    int a,b,c,tmp;
    scanf("%d%d%d",&a,&b,&c);
    if (a<=b)
    {
        tmp=a;
        a=b;
        b=tmp;
    }
    if (c>a)
    {
        printf("%d %d %d",c,a,b);
    }
    else if (c<b)
    {
        printf("%d %d %d",a,b,c);
    }
    else
    {
        printf("%d %d %d",a,c,b);
    }    
    return 0;
}

Code

#include <stdio.h>
int main(){
    int a,b,c,tmp;
    scanf("%d %d %d",&a,&b,&c);
    if (a<=b)
    {
        a=b;
    }
    if (c>a)
    {
        printf("%d %d %d",c,a,b);
    }
    else if (c<b)
    {
        printf("%d %d %d",a,b,c);
    }
    else
    {
        printf("%d %d %d",a,c,b);
    }    
    return 0;
}

三个数排序

problemId:1183

Description

输入三个整数x,y,z,请把这三个数由小到大输出。

Input

输入数据包含3个整数x,y,z,分别用逗号隔开。

Ouput

输出由小到大排序后的结果,用空格隔开。

samples

<input>—> 2,1,3 <output>—> 1 2 3

Code

#include <stdio.h>
int main(){
    int a,b,c,tmp;
    scanf("%d,%d,%d",&a,&b,&c);
    if (a<=b)
    {
        tmp=a;
        a=b;
        b=tmp;
    }
    if (c>a)
    {
        printf("%d %d %d",b,a,c);
    }
    else if (c<b)
    {
        printf("%d %d %d",c,b,a);
    }
    else
    {
        printf("%d %d %d",b,c,a);
    }    
    return 0;
}

找中间数

problemId:1190

Description

输入三个整数,找出其中的中间数。(这里的中间数指的是大小,不是位置。)

Input

输入3个整数。

Ouput

输出中间数。

samples

<input>—> 1 2 3 <output>—> 2

Code

#include <stdio.h>
int main(){
    int a,b,c,tmp;
    scanf("%d%d%d",&a,&b,&c);
    if (a<=b)
    {
        tmp=a;
        a=b;
        b=tmp;
    }
    if (c>a)
    {
        printf("%d",a);
    }
    else if (c<b)
    {
        printf("%d",b);
    }
    else
    {
        printf("%d",c);
    }    
    return 0;
}

整除

problemId:1202

Description

判断一个数 $n$ 能否同时被 $3$ 和 $5$ 整除。

### Input

输入一个正整数 $n$

### Ouput

如果能够同时被3和5整除,输出Yes,否则输出No。

### samples \--> 15 \--> Yes ### Code ``` #include int main(){ int a; scanf("%d",&a); if(a%3==0&&a%5==0){ printf("Yes"); } else printf("No"); return 0; } ``` # 闰年 problemId:1580 ### Description

时间过得真快啊,又要过年了,同时,我们的人生也增长了一年的阅历,又成熟了一些。可是,你注意过今年是不是闰年呢,明年呢?

以上是闰年的计算方法的流程图,聪明的你能否通过编程计算任意给出的一个年份是否是闰年呢?相信这个问题你能很快解决掉。

 

Input

只有一个整数year,代表年份。

Ouput

如果是闰年输出Yes,否则输出No。

samples

<input>—> 2000 <output>—> Yes

Code

#include <stdio.h>
int main(){
    int a;
    scanf("%d",&a);
    if (a%4==0)
    {
        if (a%100!=0)
        {
            printf("Yes");
        }
        else if (a%100==0)
        {
            if (a%400==0)
            {
                printf("Yes");
            }
            else
            printf("No");
        }
    }
    else
    printf("No");
    return 0;
}

C/C++经典程序训练3---模拟计算器

problemId:1133

Description

简单计算器模拟:输入两个整数和一个运算符,输出运算结果。

### Input

第一行输入两个整数,用空格分开;
第二行输入一个运算符(+、-、*、/)。
所有运算均为整数运算,保证除数不包含0。

### Ouput

输出对两个数运算后的结果。

### samples \--> 30 50 * \--> 1500 ### Code ``` #include int main(){ int a,b; char c; scanf("%d%d\n%c",&a,&b,&c); if (c=='+') printf("%d",a+b); else if (c=='-') printf("%d",a-b); else if (c=='*') printf("%d",a*b); else printf("%d",a/b); return 0; } ``` # 某年某月的天数 problemId:1160 ### Description

输入年和月,判断该月有几天?

Input

输入年和月,格式为年\月。

Ouput

输出该月的天数。

samples

<input>—> 2009\1 <output>—> 31

Code

#include <stdio.h>
int main(){
    int a,m,judge;
    scanf("%d\\%d",&a,&m);
    if (a%4==0)
    {
        if (a%100!=0)
        {
            judge=1;
        }
        else if (a%100==0)
        {
            if (a%400==0)
            {
                judge=1;
            }
            else
                judge=0;
        }
    }
    else
    judge=0;
    if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
    printf("31");
    else if (m==4||m==6||m==9||m==11)
    {
        printf("30");
    }
    else{
        if(judge){
            printf("29");
        }
        else
            printf("28");
    }
    return 0;
}

输入数字星期,输出英文(switch语句)

problemId:1119

Description

从键盘上输入数字星期,然后输出它的英文。

其对应关系是:

1 Monday

2 Tuesday

3 Wednesday

4 Thursday

5 Friday

6 Saturday

7 Sunday

Input

从键盘输入数字星期,输入数字在1-7之间。

Ouput

输出该数字对应的英文星期表示。

samples

<input>—> 2 <output>—> Tuesday

Code

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int a;
    scanf("%d",&a);
    switch(a)
    {
 
    case 1:
        printf("Monday");
 
    break;
        case 2:
        printf("Tuesday");
 
    break;
        case 3:
        printf("Wednesday");
 
    break;
        case 4:
        printf("Thursday");
 
    break;
        case 5:
        printf("Friday");
 
    break;
        case 6:
        printf("Saturday");
 
    break;
        case 7:
        printf("Sunday");
 
    break;
    }
 
 
    return 0;
}