1.以下程序计算某年某月有几天,闰年与平年的2月天数不同。判别闰年的条件是:能被4整除但不能被100整除的年是闰年,或者能被400整除的年也是闰年。请分析程序填空。
#include “stdio.h”
main()
{ int yy,mm,days;
printf("input year and month:");
scanf("%d %d",&yy,&mm);
switch(mm)
{
将程序段填充完整(实现12个月每个月天数的输出)
}
printf("the days of %d %d is %d\n",yy,mm,days);
}
2输入4个整数a,b,c,d,编写程序,将它们按从大到小的顺序输出(if语句实现)
3假设奖金税率如下(ma代表税前奖金且ma>0,r代表税率),利用switch语句编写程序对输入的一个奖金数,输出税率和应交税款及实得奖金数(扣除奖金税后)。
① 0 ≤ma<1000时, tr=0%;
② 1000 ≤ma<2000时, tr=5%;
③ 2000 ≤ma<4000时, tr=8%;
④ 4000 ≤ma时, tr=10%。
4将以下程序段改用switch语句来实现,使它完成相同的功能。
if(x>=0 && x<10) y=x;
else if(x<20) y=x*2+3;
else if(x<40) y=-0.5*x+10;
#include <stdio.h>
int main()
{
int yy, mm, days;
printf("input year and month:");
scanf("%d %d", &yy, &mm);
switch(mm)
{
case 1: days = 31; break;
case 2: if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0) days = 29; else days = 28; break;
case 3: days = 31; break;
case 4: days = 30; break;
case 5: days = 31; break;
case 6: days = 30; break;
case 7: days = 31; break;
case 8: days = 31; break;
case 9: days = 30; break;
case 10: days = 31; break;
case 11: days = 30; break;
case 12: days = 31; break;
}
printf("the days of %d %d is %d\n", yy, mm, days);
return 0;
}
a = int(input("请输入整数a:"))
b = int(input("请输入整数b:"))
c = int(input("请输入整数c:"))
d = int(input("请输入整数d:"))
max_num = a
if b > max_num:
max_num = b
if c > max_num:
max_num = c
if d > max_num:
max_num = d
min_num = a
if b < min_num:
min_num = b
if c < min_num:
min_num = c
if d < min_num:
min_num = d
if max_num == a and min_num == d:
print(max_num, c, b, min_num)
elif max_num == a and min_num == c:
print(max_num, d, b, min_num)
elif max_num == a and min_num == b:
print(max_num, d, c, min_num)
elif max_num == b and min_num == d:
print(max_num, c, a, min_num)
elif max_num == b and min_num == c:
print(max_num, d, a, min_num)
elif max_num == c and min_num == d:
print(max_num, b, a, min_num)
其中,首先用if语句判断4个整数的最大值和最小值,然后通过一系列if语句来确定它们的排列顺序,并将它们按从大到小的顺序输出。
#include <stdio.h>
int main() {
double ma, tr, tax, net;
printf("请输入奖金数:");
scanf("%lf", &ma);
switch((int)(ma / 1000)) {
case 0:
tr = 0.0;
break;
case 1:
tr = 0.05;
break;
case 2:
tr = 0.08;
break;
default:
tr = 0.1;
break;
}
tax = ma * tr;
net = ma - tax;
printf("税率为 %.2f%%,应交税款为 %.2f 元,实得奖金数为 %.2f 元\n", tr * 100, tax, net);
return 0;
}
程序先提示用户输入奖金数,然后使用switch语句根据奖金数所在区间确定税率tr的值,再计算应交税款tax和实得奖金数net的值。最后输出税率、应交税款和实得奖金数。
以下是使用switch语句实现的程序段:
switch(x) {
case 0 ... 9:
y = x;
break;
case 10 ... 19:
y = x * 2 + 3;
break;
case 20 ... 39:
y = -0.5 * x + 10;
break;
default:
break;
}
以上是4个问题的答案,已经分割线,如有帮助请采纳。
第1题:最直白的写法
case 1:days=31; break;
case 2: if(yy%400==0||(yy%4==0&&yy%100!=0))
days=29;
else
days=28;
break;
case 3:days=31; break;
case 4:days=30; break;
case 5:days=31; break;
case 6:days=30; break;
case 7:days=31; break;
case 8:days=31; break;
case 9:days=30; break;
case 10:days=31; break;
case 11:days=30; break;
case 12:days=31; break;
依次对比四个数字,要达到从大到小输出,那么前面的数比后面数字小就交换两个值,依次两两全部比较
#include <stdio.h>
int main(){
int a,b,c,d,t;
printf("请输入4个整数:");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a<b){
t=a;
a=b;
b=t;
}
if(a<c){
t=a;
a=c;
c=t;
}
if(a<d){
t=a;
a=d;
d=t;
}
if(b<c){
t=b;
b=c;
c=t;
}
if(b<d){
t=b;
b=d;
d=t;
}
if(c<d){
t=c;
c=d;
d=t;
}
printf("将这四个数进行从大到小排序:%d,%d,%d,%d",a,b,c,d);
return 0;
}