全国计算机等级考试二级c语言课后编程练习参考答案.doc
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
printf("%g,%g,%g的平均数是%g\n",a,b,c,average);
}
运行结果:
输入a,b,c: 1.23 2.365 3.48
1.23,2.365,3.48的平均数是2.4
3.20
#include<stdio.h>
void main()
(
int a,b,c,temp;
printff'输入a,b,c:");
3.18
#include<stdio.h>
void main()
(
int a,b,c,d;
printf(HEnter a & b:");
scanf("%d%d"/&a,&b);
c = a/b;
d = a%b;
printf("a/b = %d, a%%b = %d\n",c,d);
)
运行结果:
Enter a &b: 1500 350
void main()
(
intx,y;
printff'Enter x:
scanf(”%d“,&x);
if(x == 0)
y = x-i;
if(x > -5 && x < 0)
y = x;
if(x > 0 && x < 10)
y = x +1;
printf("y = %d\n",y);
}
运行结果:
Enter x: 0
printf("%d是偶数\n“,num);
else
printf("%d是奇数\nn,num);
}
运行结果:
请输入一个数:15
15是奇数
4.24
#include<stdio.h>
void main()
{
int a,b,c,max;
printf("输入a,b,c:");
scanf(”%d%d%d”,&a,&b,&c);
printf(”该学生的年龄为:%d\n"/age);
)
运行结果:
输入生H:1990 10 10
当前日期:2012 1110
该学生的年龄为:22
4.23
#include<stdio.h>
void main()
int num;
printf("请输入一个数:");
scanf("%d“,&num);
if(num % 2 == 0)
scanf("%d%d%d"/&a,&b,&c);
temp = a;
a = c;
c = b;
b = temp;
printf(”交换后a,b,c的值:%d,%d,%d\n”,a,b,c);
)
运行结果:
输入a,b,c:789
交换后a,b,c的值:9,7,8
4.21
#include<stdio.h>
void main()
else if(x < 0&& x> -5)
V= x;
printf("y = %d\n"/y);
}
运行结果:
Enter x: 8
y = 9
(4)switch语句
#include<stdio.h>
void main()
{
int x,y;
printf("Enterx:");
scanf("%d",&x);
max = a > b ? a : b;
max = max > c? max : c;
printf("%d,%d,%d中的最大值为:%d\n",a,b/c/max);
}
运行结果:
输入a,b,c: 9 10 8
9,10,8中的最大值为:10
4.25
(1)不嵌套的if语句
#include<stdio.h>
printf(”输入生日:”);
scanf("%d%d%d,'/&year/&month,&day);
printf(”当前日期;
scanf("%d%d%d"/&nowYear,&nowMonth,&nowDay);
age = now Year - year;
if(nowMonth < month 11 nowMonth == month && nowDay < day) age-;
switch(x)
case -4:
case -3:
case -2:
case -1: y = x;break;
case 0: y = x - l;break;
default: y = x + 1;
)
pnntf(',y = %d\n,,/y);
}
运行结果:
Enter x: 6
y = 7
5.17
⑴使用for语句
{
int a,m;
printf("Enter a:");
scanf("%d",&a);
switch(a/10)
{
case 9:
case 8:
case 7:
case 6: m = 5;break;
case 5: m = 4;break;
case 4: m = 3;break;
case 3: m = 2;break;
default: m = 1;
}
printf("a = %d,m = %d\n",a,m);
}
运行结果:
Enter a: 90
a = 90,m = 5
Enter a:35
a = 35,m = 2
4.22
#include<stdio.h>
void main()
{
int year,month,day,nowYear;nowMonth,nowDay,age;
y = -i
⑵嵌套的if语句
#include<stdio.h>
void main()
{
int x,y;
printfቤተ መጻሕፍቲ ባይዱ"Enterx:");
scanf("%d”,&x);
if(x < 10&& x >-5)
{
if(x > 0)
y = x + 1;
if(x == 0)
y = x-i;
if(x < 0)
y = x;
)
printf("y = %d\n"/y);
}
运行结果:
Enter x: -4
y = -4
(3)if-else语句
#include<stdio.h>
void main()
{
int x,y;
printf("Enterx:");
scanf("%d",&x);
if(x == 0)
Y= x-1;
else if(x > 0 && x v 10) y = x + l;
a/b = 4, a%b = 100
3.19
#include<stdio.h>
void main()
{
double a,b,coverage;
printf("输入a,b,c:");
scanf(”%lf%lf%%,&a,&b,&c);
average = (a+b+c於;
average = (int)(average * 10 + 0.5) / 10.0;
#include<stdio.h>
void main()
(
int i,flag = l,sum = 0;
}
运行结果:
输入a,b,c: 1.23 2.365 3.48
1.23,2.365,3.48的平均数是2.4
3.20
#include<stdio.h>
void main()
(
int a,b,c,temp;
printff'输入a,b,c:");
3.18
#include<stdio.h>
void main()
(
int a,b,c,d;
printf(HEnter a & b:");
scanf("%d%d"/&a,&b);
c = a/b;
d = a%b;
printf("a/b = %d, a%%b = %d\n",c,d);
)
运行结果:
Enter a &b: 1500 350
void main()
(
intx,y;
printff'Enter x:
scanf(”%d“,&x);
if(x == 0)
y = x-i;
if(x > -5 && x < 0)
y = x;
if(x > 0 && x < 10)
y = x +1;
printf("y = %d\n",y);
}
运行结果:
Enter x: 0
printf("%d是偶数\n“,num);
else
printf("%d是奇数\nn,num);
}
运行结果:
请输入一个数:15
15是奇数
4.24
#include<stdio.h>
void main()
{
int a,b,c,max;
printf("输入a,b,c:");
scanf(”%d%d%d”,&a,&b,&c);
printf(”该学生的年龄为:%d\n"/age);
)
运行结果:
输入生H:1990 10 10
当前日期:2012 1110
该学生的年龄为:22
4.23
#include<stdio.h>
void main()
int num;
printf("请输入一个数:");
scanf("%d“,&num);
if(num % 2 == 0)
scanf("%d%d%d"/&a,&b,&c);
temp = a;
a = c;
c = b;
b = temp;
printf(”交换后a,b,c的值:%d,%d,%d\n”,a,b,c);
)
运行结果:
输入a,b,c:789
交换后a,b,c的值:9,7,8
4.21
#include<stdio.h>
void main()
else if(x < 0&& x> -5)
V= x;
printf("y = %d\n"/y);
}
运行结果:
Enter x: 8
y = 9
(4)switch语句
#include<stdio.h>
void main()
{
int x,y;
printf("Enterx:");
scanf("%d",&x);
max = a > b ? a : b;
max = max > c? max : c;
printf("%d,%d,%d中的最大值为:%d\n",a,b/c/max);
}
运行结果:
输入a,b,c: 9 10 8
9,10,8中的最大值为:10
4.25
(1)不嵌套的if语句
#include<stdio.h>
printf(”输入生日:”);
scanf("%d%d%d,'/&year/&month,&day);
printf(”当前日期;
scanf("%d%d%d"/&nowYear,&nowMonth,&nowDay);
age = now Year - year;
if(nowMonth < month 11 nowMonth == month && nowDay < day) age-;
switch(x)
case -4:
case -3:
case -2:
case -1: y = x;break;
case 0: y = x - l;break;
default: y = x + 1;
)
pnntf(',y = %d\n,,/y);
}
运行结果:
Enter x: 6
y = 7
5.17
⑴使用for语句
{
int a,m;
printf("Enter a:");
scanf("%d",&a);
switch(a/10)
{
case 9:
case 8:
case 7:
case 6: m = 5;break;
case 5: m = 4;break;
case 4: m = 3;break;
case 3: m = 2;break;
default: m = 1;
}
printf("a = %d,m = %d\n",a,m);
}
运行结果:
Enter a: 90
a = 90,m = 5
Enter a:35
a = 35,m = 2
4.22
#include<stdio.h>
void main()
{
int year,month,day,nowYear;nowMonth,nowDay,age;
y = -i
⑵嵌套的if语句
#include<stdio.h>
void main()
{
int x,y;
printfቤተ መጻሕፍቲ ባይዱ"Enterx:");
scanf("%d”,&x);
if(x < 10&& x >-5)
{
if(x > 0)
y = x + 1;
if(x == 0)
y = x-i;
if(x < 0)
y = x;
)
printf("y = %d\n"/y);
}
运行结果:
Enter x: -4
y = -4
(3)if-else语句
#include<stdio.h>
void main()
{
int x,y;
printf("Enterx:");
scanf("%d",&x);
if(x == 0)
Y= x-1;
else if(x > 0 && x v 10) y = x + l;
a/b = 4, a%b = 100
3.19
#include<stdio.h>
void main()
{
double a,b,coverage;
printf("输入a,b,c:");
scanf(”%lf%lf%%,&a,&b,&c);
average = (a+b+c於;
average = (int)(average * 10 + 0.5) / 10.0;
#include<stdio.h>
void main()
(
int i,flag = l,sum = 0;