题目大意:FM要去逛街,他可以走任何街道,在任何路口转弯。他走一条陌生街道的速度是20公里每小时,走一条熟悉街道的速度是50公里每小时。输入是街道信息,输出消耗时间。
解法:本质就是浮点运算,求两点间距离。
参考代码:
#include<iostream> #include<string> #include<cmath> #include<cstdio> using namespace std; int main(){ int x0,y0,x1,x2,y1,y2,time; double dist=0.0,d; string str; char *p; while(cin>>x0>>y0){ //x0,y0 is useless, but still need to be read in cin.get(); dist=0.0; while(getline(cin,str)&&str!="java"){ p=&str[0]; //change a string to a char point!!! sscanf(p,"%d%d%d%d",&x1,&y1,&x2,&y2); d=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); dist+=sqrt(d); } dist=dist*2/1000.0/20.0*60.0; time=int(dist+0.5); //convert double to int cout<<time/60<<':'; if(time%60<10)cout<<'0'<<time%60<<endl; else cout<<time%60<<endl; } return 0; }