1.出租车计价

#include<stdio.h>
#include<math.h>
int main()
{
double d,t,m;
int a;
printf("Enter distance and time:\n");
scanf("%lf%lf",&d,&t);
if(t>=5){
if(d<=3&&d>=0){
m=10+0.4*t;
a=floor(m+0.5);
printf("money=%d\n",a);
}
else{
if(d>3&&d<=10){
m=10+(d-3)*2+0.4*t;
a=floor(m+0.5);
printf("money=%d\n",a);
}
else{
if(d>10){
m=24+(d-10)*3+0.4*t;
a=floor(m+0.5);
printf("money=%d\n",a);
}
else
printf("Input Error\n");
}}}
else{
if(t>=0&&t<5){
if(d<=3&&d>=0){
m=10;
a=floor(m+0.5);
printf("money=%d\n",a);
}
else{
if(d>3&&d<=10){
m=10+(d-3)*2;
a=floor(m+0.5);
printf("money=%d\n",a);
}
else{
if(d>10){
m=24+(d-10)*3;
a=floor(m+0.5);
printf("money=%d\n",a);
}
else
printf("Input Error\n");
}}}
else
printf("Input Error");
}
return 0;
}

 

 

2、书上P66页第5题,三角形判断题。

#include<stdio.h>
#include<math.h>
int main()
{
double x,s,w,y,h,n,a,b,c,circumference,area,t;
printf("Enter:x1 y1\nEnter:x2 y2\nEnter:x3 y3\n");
scanf("%lf%lf%lf%lf%lf%lf",&x,&y,&s,&h,&w,&n);
a=sqrt((x-s)*(x-s)+(y-h)*(y-h));
b=sqrt((x-w)*(x-w)+(y-n)*(y-n));
c=sqrt((s-w)*(s-w)+(h-n)*(h-n));
if((x-s)/(y-h)==(x-w)/(y-n)){
printf("Impossible!\n");
}
else{
circumference=a+b+c;
t=circumference/2;
area=sqrt(t*(t-a)*(t-b)*(t-c));
printf("circumference=%.2f,area=%.2f\n",circumference,area);
}
}

3、总结心得。

if-else语句后的大括号{},影响if-else将要执行下面几条语句,大括号要标注清楚。函数floor(x+0.5)可以实现四舍五入到整数。三角形判断中,使用a+b>c不能得出正确结果,使用斜率相等能得出。

 

posted on 2019-04-09 19:49  黄科迪  阅读(208)  评论(1编辑  收藏  举报