张云聪
问题链接:http://acm.nyist.net/JudgeOnline/bestcode.php?pid=97
问题分析:
在哥哥追上弟弟的时候存在以下等式:
假设用了ts弟弟追上了哥哥,则
弟弟提前走的距离+弟弟在t时间内走的距离=哥哥在t时间内走的距离
狗狗走的距离=狗狗的速度×t
由第一个等式可以求出时间t,第二个求出结果。
代码:
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <vector> #include <queue> #include <stack> #include <map> #include <string> #include <algorithm> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { int n; scanf("%d",&n); while(n--){ double m,x,y,z; scanf("%lf%lf%lf%lf",&m,&x,&y,&z); //double t=(m*x)/(y-x); printf("%.2lf\n",z*(m*x)/(y-x)); } return 0; }优秀代码: 01. #include<iostream> 02. #include<stdio.h> 03. using namespace std; 04. int main() 05. { 06. int n; 07. cin>>n; 08. while(n--) 09. { 10. int s,a,b,c; 11. cin>>s>>a>>b>>c; 12. printf("%.2lf\n",s*a/(double)(b-a)*c); 13. } 14. 15. } 对比:不同之处在于我为了方便直接全部用了double型的变量,而优秀代码只是在结果的分母中转换为了double型。
收获:
printf("%.2lf") lf用于double型变量的输出,.2用于控制小数点的固定位数。
还有就是明天查查c语言书,看看除法得出的结果的类型是怎么控制的。