第四次作业
1.编写程序判断n是正数还是负数
#include<stdio.h>
main()
{
int n;
printf("请输入一个整数:\n");
scanf("%d",&n);
if(n>0)
printf("%d是正数!\n",n);
else
if(n==0)
printf("%d既不是正数,也不是负数!\n",n);
else
printf("%d是负数!\n",n);
}

2.使用条件运算符,找出a,b,c,d四个数中最大的数
#include<stdio.h>
main()
{
double a,b,c,d,m1,m2,max;
printf("输入四个数字:\n");
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
m1=a>b?a:b;
m2=c>d?c:d;
max=m1>m1?m1:m2;
printf("Max=%f\n",max);
}

3.已知某商场进行促销活动,对于消费的价格有折扣活动,即消费1000元打9折;消费2000元打8.5折;消费3000元打7折;消费5000元打6折;编写程序代码求出消费者实际花费的价格
#include<stdio.h>
main()
{
float price;
printf("输入购买商品价格:\n");
scanf("%f",&price);
if(price>=5000)
printf("实际需要支付:%.2f元\n",price*0.6);
else if(price>=3000)
printf("实际需要支付:%.2f元\n",price*0.7);
else if(price>=2000)
printf("实际需要支付:%.2f元\n",price*0.85);
else if(price>=1000)
printf("实际需要支付:%.2f元\n",price*0.9);
else
printf("实际需要支付:%.2f元\n",price);
}

4.输入年份,月份,判断该月有多少天
#include<stdio.h>
int main()
{
int y,m;
printf("输入年和月:\n");
scanf("%d%d",&y,&m);
switch(m)
{
case 2:{if(y%4==0&&y%100!=0||y%400==0)
printf("29天");
else
printf("28天");}break;
case 4:
case 6:
case 9:
case 11:printf("30天");break;
default:printf("31天");break;
}
return 0;
}

5.输入三条边,判断是否可以构成三角形(任意两边之和大于第三边)
#include<stdio.h>
main()
{
int a,b,c;
printf("三角形的三条边\n");
scanf("%d%d%d",&a,&b,&c);
if (a+b>c&&a+c>b&&b+c>a)
printf("可以");
else
printf("不可以");
}



浙公网安备 33010602011771号