第五次作业
- 编写程序判断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>m2?m1:m2;
printf("max=%f\n",max);
}

3.已知某商场进行促销活动,对于消费的价格有折扣活动,即消费1000元。打九折;消费2000元,打8.5折;消费3000元,打七折;消费5000元,打六折。编写程序代码求出消费者实际花费的价格。
#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元",price*0.9);
else
printf("实际需要支付:%.2f元\n",price);
}

4.输入年份,月份,判断该月有多少天
30 31 29 28
闰年:能被4整除但不能被100整除,或者能被400整除
#include<stdio.h>
main()
{
int a,b;
printf("年份与月份\n");
scanf("%d%d",&a,&b);
if(a%4==0&&a%100!=0&&b==2||a%400==0&&b==2)
printf("29");
else if(b==2)
printf("28");
else if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("31");
else
printf("30");
}

5.输入三条边,判断是否可以构成三角形(任意两边之和大于第三边)
#include <stdio.h>
main()
{
float a,b,c;
printf("请输入三角形边长:\n");
scanf ("%f%f%f",&a,&b,&c);
if ((a-b>=c)||(b-c>=a)||(c==0))
printf("不能构成三角形!\n");
else
printf("能够成三角形!\n");
}


浙公网安备 33010602011771号