C语言程序设计基础OJ练习题(实验二选择结构)

一、C语言实验——求绝对值(选择结构)

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

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

Output

输出它的绝对值。

Sample Input

-4

Sample Output

4

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

 

二、C语言实验——时间间隔

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示。
如:3点5分25秒应表示为--03:05:25.假设两个时间在同一天内,时间先后顺序与输入无关。

Input

输入包括两行。
第一行为时间点1。
第二行为时间点2。

Output

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

Sample Input

12:01:12
13:09:43

Sample Output

01:08:31

#include <stdio.h>
int main()
{
    int a,b,c,x,y,z,s;
    scanf("%d:%d:%d %d:%d:%d",&a,&b,&c,&x,&y,&z);
    s = a*3600+b*60+c-x*3600-y*60-z;
    if(s<0)
    {
        s = -s;
    }
    printf("%02d:%02d:%02d\n",s/3600,s%3600/60,s%60);
}

三、C语言实验——求两个整数之中较大者

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

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

Output

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

Sample Input

5 9

Sample Output

max=9

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

 

四、小鑫吃苹果

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

单组输入。 
两个正整数表示苹果的重量x,y(1 <= (x, y) <= 1000)

Output

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

Sample Input

100 200

Sample Output

200

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

 

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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

 输入有两行,第一行为两个整数a,b(a,b>0)。第二行为一个数,为小鑫对于a+b口算出的答案。

Output

 输出为一行。判断小鑫给出的答案是否正确,如果是输出“YES”,否则输出“NO”。(输出不包括引号)

Sample Input

1 2
3

Sample Output

YES

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

 

六、小鑫追女神

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

单组输入。
 输入只有一个数,保证只有0或1。

Output

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

Sample Input

0

Sample Output

He he

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

 

七、C语言实验——求三个整数的最大值

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

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

Output

输出三个数中的最大值。

Sample Input

5,7,9

Sample Output

max=9

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

八、相加和最大值

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

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

Output

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

Sample Input

1 2 3

Sample Output

5

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

九、时间格式转换

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem 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 小时制的时间。

Output

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

Sample Input

00:05

Sample Output

12:05 AM

Hint

输入部分可以使用 scanf("%d:%d") 读入;输出的数字部分可以使用 printf("%d:%02d") 输出。

#include <stdio.h>
int main()
{
    int a,b;
    scanf("%d:%d",&a,&b);
    if(a==0)
      printf("12:%02d AM\n",b);
    else if(a>=1&&a<=11)
      printf("%d:%02d AM\n",a,b);
    else if(a==12)
      printf("12:%02d PM\n",b);
    else if(a>12&&a<24)
      printf("%d:%02d PM\n",a-12,b);
    return 0;
}

十、C语言实验——从大到小输出a、b、c(选择结构)

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

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

Output

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

Sample Input

4 3 5

Sample Output

5 4 3

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

十一、C语言实验——找中间数

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

输入3个整数。

Output

输出中间数。

Sample Input

1 2 3

Sample Output

2

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

十二、C语言实验——整除

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

输入一个正整数n。

Output

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

Sample Input

15

Sample Output

Yes

#include <stdio.h>
int main()
{
    int a;
    scanf("%d",&a);
    if(a%3==0&&a%5==0)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

十三、闰年

Time Limit: 1000 ms Memory Limit: 32768 KiB

Submit Statistic

Problem Description

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

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

 

Input

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

Output

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

Sample Input

2000

Sample Output

Yes

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

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

Time Limit: 1000 ms Memory Limit: 8192 KiB

Submit Statistic

Problem Description

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

Input

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

Output

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

Sample Input

30 50
*

Sample Output

1500

十五、C语言实验——某年某月的天数

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

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

Output

输出该月的天数。

Sample Input

2009\1

Sample Output

31

Hint

注意判断闰年啊

十六、C语言实验——输入数字星期,输出英文(switch语句)

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

从键盘上输入数字星期,然后输出它的英文。
其对应关系是:
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday

Input

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

Output

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

Sample Input

2

Sample Output

Tuesday

猜你喜欢

转载自blog.csdn.net/qq_38074938/article/details/86522590