PTA练习题
4-5 求整数均值
#include <stdio.h>
int main(void)
{
int a, b, c, d, e;
float f;
scanf("%d %d %d %d", &a,&b,&c,&d);
e = a + b + c + d;
f = (a + b + c + d) / 4;
printf("Sum = %d; Average = %.1f", e, f);
return 0;
}
错误!
f = (a + b + c + d) / 4; 此时 f 会跟随4取整,故应将4改为4.0
故正确答案为:
#include <stdio.h>
int main(void)
{
int a, b, c, d, e;
float f;
scanf("%d %d %d %d", &a,&b,&c,&d);
e = a + b + c + d;
f = (a + b + c + d) / 4.0;
printf("Sum = %d; Average = %.1f", e, f);
return 0;
}
4-7 计算火车运行时间
#include<stdio.h>
int main()
{
int a,b,c,d;
scanf("%d %d",&a,&b);
/*省去麻烦,直接规定为两个四位数,四个数再加上两个结果太麻烦啦!!*/
c = b/100-a/100;
/*时的运算,直接获得前两位数*/
d = b%100-a%100;
/*分的运算,取余获得后两位*/
/*我插入感叹一句 我是抄的 但是 能写出来的也太太太太聪明了吧!!!*/
if (d<0)
/*分存在负数的情况,补上去!*/
{
d+=60;
c-=1;
}
printf("%02d:%02d",c,d);
return 0;
}
单独马出来看一遍
#include<stdio.h>
int main()
{
int a,b,c,d;
scanf("%d %d",&a,&b);
c = b/100-a/100;
d = b%100-a%100;
if (d<0)
{
d+=60;
c-=1;
}
printf("%02d:%02d",c,d);
return 0;
}