C语言编程练习----2021级山东理工大学ACM实验二题解

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

Description

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

Input

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

Output

输出它的绝对值。

Sample

Input 

-4

Output 

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

		printf("%d", -x);
	}
	return 0;
}

B - C语言实验——时间间隔

Description

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

Input

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

Output

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

Sample

Input 

12:01:12
13:09:43

Output 

01:08:31
#include <stdio.h>
int main()
{
	int a, b, c, d, e, f;//两个时间的时分秒变量定义
	scanf("%d:%d:%d\n%d:%d:%d", &a, &b, &c, &d, &e, &f);
	a = 3600 * a + 60 * b + c, b = 3600 * d + 60 * e + f, c = a - b;
	if (c > 0)
		c = c;
	else
		c = -c;
	printf("%02d:%02d:%02d", c / 3600, c / 60 % 60, c % 60);
	return 0;
}

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

Description

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

Input

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

Output

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

Sample

Input 

5 9

Output 

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

D - 小鑫吃苹果

Description

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

Input

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

Output

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

Sample

Input 

100 200

Output 

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

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

Description

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

Input

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

Output

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

Sample

Input 

1 2
3

Output 

YES
#include <stdio.h>
int main()
{
	int a = 0; int b = 0;
	scanf("%d %d", &a, &b);
	int c = 0;
	scanf("%d", &c);
	if (c ==a + b) {//=是赋值,==才是他俩相等的关系
		printf("YES");
	}
	else {
		printf("NO");
	}
	return 0;
}

F - 小鑫追女神

Description

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

Input

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

Output

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

Sample

Input 

0

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;
}

G - C语言实验——求三个整数的最大值

Description

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

Input

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

Output

输出三个数中的最大值。

Sample

Input 

5,7,9

Output 

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

}

H - 相加和最大值

Description

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

Input

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

Output

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

Sample

Input 

1 2 3

Output 

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

I - 时间格式转换

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

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("%d:%02d PM\n", a, b);
	else if (a >= 13 && a <= 23) printf("%d:%02d PM\n", a - 12, b);
	return 0;
}
!!!    ==是关系,=是赋值!
#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("%d:%02d PM\n", a, b);
	else  printf("%d:%02d PM\n", a - 12, b);
	return 0;
}
第二个代码就是最后用的else,单纯的用else指的是后面不用加条件注明了,他是除了上面那些条件的补集;而用第一个else if就必须注明单个的条件。

G - C语言实验——求三个整数的最大值

Description

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

Input

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

Output

输出三个数中的最大值。

Sample

Input 

5,7,9

Output 

max=9
知识点补充:用数组会比较好
三个数排大小 
a b 
b c 
a b 
if(a > b) swap(a, b) 
if(b > c) swap(b, c) 
if(a > b) swap(a, b) 
a < b < c 
n个数排序(数组)
法一
#include<stdio.h>int main(){
	int a, b, c, max, min;
	scanf("%d %d %d", &a, &b, &c);
	if (a > b)  max = a, min = b;//先确定a,b的大小只有两种情况不考率相等
	else max= b, min = a;
	if (c > max)printf("%d %d %d", c, max, min);
    else if (c<min)printf("%d %d %d", max, min, c);
	else if (c<max && c>min)printf("%d %d %d", max, c, min);
	return 0;

}

法二---数组

H - 相加和最大值

Description

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

Input

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

Output

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

Sample

Input 

1 2 3

Output 

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

I - 时间格式转换

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

Output 

12:05 AM

Hint

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

#include <stdio.h>
int main()
{
    int a1,b1,c1=0;//第一组数据
    int a2,b2,c2=0;
    scanf("%d:%d:%d\n%d:%d:%d",&a1,&b1,&c1,&a2,&b2,&c2);//格式化输入
    int d,e=0;
    d=a1*3600+b1*60+c1;
    e=a2*3600+b2*60+c2;
    int g=0;
    g=d-e;
    if (g>0)g=g;
    else g=-g;
    printf("%02d:%02d:%02d",g/3600,g/60%60,g%60);
    return 0;
}

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

Description

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

Input

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

Output

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

Sample

Input 

4 3 5

Output 

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

K - C语言实验——三个数排序

Description

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

Input

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

Output

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

Sample

Input 

2,1,3

Output 

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

L - C语言实验——找中间数

Description

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

Input

输入3个整数。

Output

输出中间数。

Sample

Input 

1 2 3

Output 

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

M - C语言实验——整除

Description

判断一个数 nn 能否同时被 33 和 55 整除。

Input

输入一个正整数 nn

Output

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

Sample

Input 

15

Output 

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

N - 闰年

Description

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

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

Input

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

Output

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

Sample

Input 

2000

Output 

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

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

Description

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

Input

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

Output

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

Sample

Input 

30 50
*

Output 

1500
#include<stdio.h>
int main()
{
	int a, b; char c;
	scanf("%d %d\n%c", &a, &b,&c);//C就是用来代表+-*/四种可能的
	if (c == '+')printf("%d", a + b);//输出字符用''
	if (c == '-')printf("%d", a - b);
	if (c == '*')printf("%d", a * b);
    if (c == '/')printf("%d", a / b);
	return 0;
}

P - C语言实验——某年某月的天数

Description

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

Input

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

Output

输出该月的天数。

Sample

Input 

2009\1

Output 

31

Hint

注意判断闰年啊

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

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

Description

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

Input

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

Output

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

Sample

Input 

2

Output 

Tuesday
 
#include<stdio.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;
	}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恰逢*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值