C语言编程题

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个回答  2023-01-05
1.将程序段填充完整(实现12个月每个月天数的输出)
case 1: case 3: case 5: case 7: case 8: case 10: case 12: days=31;break;
case 4: case 6: case 9: case 11: days=30;break;
case 2:
if((yy%4==0 && yy%100!=0) || yy%400==0) days=29;
else days=28;
break;
2.输入4个整数a,b,c,d,编写程序,将它们按从大到小的顺序输出(if语句实现)
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
if(a>b && a>c && a>d) printf("%d ",a);
if(b>a && b>c && b>d) printf("%d ",b);
if(c>a && c>b && c>d) printf("%d ",c);
if(d>a && d>b && d>c) printf("%d ",d);
3.假设奖金税率如下(ma代表税前奖金且ma>0,r代表税率),利用switch语句编写程序对输入的一个奖金数,输出税率和应交税款及实得奖金数(扣除奖金税后)。
int ma;
float r,ts,sd;
scanf("%d",&ma);
switch(ma/1000)
{
case 0: r=0;break;
case 1: r=0.05;break;
case 2: r=0.08;break;
default: r=0
第2个回答  2017-11-09
/* 第2题 */
#include

int main(void)
{
unsigned N;
unsigned i, j;
/* 每一行第一个元素的值 */
unsigned lf;
/* 要打印的元素的值 */
unsigned nxt;

printf("请输入一组数据:\n");
while(scanf("%u", &N) == 1)
{
/* 处理新一组数据时把第一个元素置为1 */
lf = 1;
/* 行号从0开始,列号从1开始 */
for (i = 0; i < N; i++)
{
/* 处理新一行数据时计算该行第一个元素 */
/* 增量是当前行号 */
lf += i;
nxt = lf;
/* 根据行数减少本行要打印的元素个数 */
for (j = 1; j <= N - i; j++)
{
printf("%-2u", nxt);
if (j < N)
{
/* 增量是当前行号与下个列号之和 */
nxt += i + (j + 1);
putchar(' ');
}
}
putchar('\n');
}
}

return 0;
}本回答被网友采纳
第3个回答  2022-12-02

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

注:只需要注意二月份的天数,判断下是不是闰年就OK

第2题:

依次对比四个数字,要达到从大到小输出,那么前面的数比后面数字小就交换两个值,依次两两全部比较

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

}

第4个回答  2023-10-19
1. 下面是完成计算某年某月有几天的程序代码:
```c
#include <stdio.h>
int main() {
int yy, mm, days;
printf("input year and month: ");
scanf("%d %d", &yy, &mm);

switch (mm) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0)
days = 29;
else
days = 28;
break;
default:
printf("Invalid month!\n");
return 1;
}

printf("The days of %d %d is %d\n", yy, mm, days);
return 0;
}
```
2. 下面是按从大到小顺序输出4个整数的程序代码:
```c
#include <stdio.h>
int main() {
int a, b, c, d;
printf("Input four integers: ");
scanf("%d %d %d %d",&a, &b, &c, &d);
if (a >= b && a >= c && a >= d) {
printf("%d ", a);
if (b >= c && b >= d) {
printf("%d ", b);
if (c >= d)
printf("%d %d\n", c, d);
else
printf("%d %d\n", d, c);
} else if (c >= b && c >= d) {
printf("%d ", c);
if (b >= d)
printf("%d %d\n", b, d);
else
printf("%d %d\n", d, b);
} else {
printf("%d ", d);
if (b >= c)
printf("%d %d\n", b, c);
else
printf("%d %d\n", c, b);
}
} else if (b >= a && b >= c && b >= d) {
printf("%d ", b);
if (a >= c && a >= d) {
printf("%d ", a);
if (c >= d)
printf("%d %d\n", c, d);
else
printf("%d %d\n", d, c);
} else if (c >= a && c >= d) {
printf("%d ", c);
if (a >= d)
printf("%d %d\n", a, d);
else
printf("%d %d\n", d, a);
} else {
printf("%d ", d);
if (a >= c)
printf("%d %d\n", a, c);
else
printf("%d %d\n", c, a);
}
} else if (c >= a && c >= b && c >= d) {
printf("%d ", c);
if (a >= b && a >= d) {
printf("%d ", a);
if (b >= d)
printf("%d %d\n", b, d);
else
printf("%d %d\n", d, b);
} else if (b >= a && b >= d) {
printf("%d ", b);
if (a >= d)
printf("%d %d\n", a, d);
else
printf("%d %d\n", d, a);
} else {
printf("%d ", d);
if (a >= b)
printf("%d %d\n", a, b);
else
printf("%d %d\n", b, a);
}
} else {
printf("%d ", d);
if (a >= b && a >= c) {
printf("%d ", a);
if (b >= c)
printf("%d %d\n", b, c);
else
printf("%d %d\n", c, b);
} else if (b >= a && b >= c) {
printf("%d ", b);
if (a >= c)
printf("%d %d\n", a, c);
else
printf("%d %d\n", c, a);
} else {
printf("%d ", c);
if (a >= b)
printf("%d %d\n", a, b);
else
printf("%d %d\n", b, a);
}
}
return 0;
}
```
3. 下面是根据奖金数计算税率和应交税款的程序代码:
```c
#include <stdio.h>
int main() {
int ma;
float tr, tax, net_bonus;
printf("Input the bonus amount: ");
scanf("%d", &ma);
switch (ma / 1000) {
case 0:
tr = 0;
break;
case 1:
tr = 0.05;
break;
case 2: case 3:
tr = 0.08;
break;
default:
tr = 0.1;
break;
}
tax = ma * tr;
net_bonus = ma - tax;
printf("Tax rate: %.2f%%\n", tr * 100);
printf("Tax payable: %.2f\n", tax);
printf("Net bonus: %.2f\n", net_bonus);
return 0;
}
```
4. 将给定的程序段改用switch语句实现:
```c
#include <stdio.h>
int main() {
int x, y;
printf("Input a number: ");
scanf("%d", &x);
switch (x / 10) {
case 0: case 1:
y = x;
break;
case 2:
y = x * 2 + 3;
break;
default:
y = -0.5 * x + 10;
break;
}
printf("y = %d\n", y);
return 0;
}
```
相似回答
大家正在搜