ACM学习之路(一阶)
杭电1.2.1 Biker’s Trip Odometer(C++实现)
因为本人才打算参加ACM竞赛,所以一切都得从头开始,现在我将我的解题代码放在CSDN上.一是为了激励自己,二是可能会给予一些初学者帮助.
题目描述:
Most bicycle speedometers work by using a Hall Effect sensor fastened to the front fork of the bicycle. A magnet is attached to one of the spokes on the front wheel so that it will line up with the Hall Effect switch once per revolution of the wheel. The speedometer monitors the sensor to count wheel revolutions. If the diameter of the wheel is known, the distance traveled can be easily be calculated if you know how many revolutions the wheel has made. In addition, if the time it takes to complete the revolutions is known, the average speed can also be calculated.
For this problem, you will write a program to determine the total distance traveled (in miles) and the average speed (in Miles Per Hour) given the wheel diameter, the number of revolutions and the total time of the trip. You can assume that the front wheel never leaves the ground, and there is no slipping or skidding.
C++代码如下:
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
float diameter;
int num;
float timeoftrip;
float sum;
int flag=0;
const double p=3.1415927;
while(cin>>diameter>>num>>timeoftrip){
if(num==0)
return 0;
sum=num*diameter*p/5280/12;
flag++;
cout<<"Trip #"<<flag<<":"<<" "<<fixed<<setprecision(2)<<sum<<" "<<fixed<<setprecision(2)<<sum/(timeoftrip/3600)<<endl;
}
return 0;
}