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 comple
杭电ACM1.2.1 Biker's Trip Odometer
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=2&problemid=2
1 //author:pz
2
3 import java.text.DecimalFormat;
4 import java.util.Scanner;
5
6 public class Main{
7 final double PI = 3.1415927;
8 final int InchesPerFoot = 12;
9 final int FeetPerMile = 5280;
10 final int SecondsPerMinute = 60;
11 final int MinutesPerHour = 60;
12
13 double diameter;
14 int revolutions;
15 double time;
16 double distance;
17 double mph;
18
19 static int count = 1;
20
21 Main(double diameter, int revolutions, double time){
22 this.diameter = diameter;
23 this.revolutions = revolutions;
24 this.time = time;
25 }
26
27 void calculate(){
28 DecimalFormat df = new DecimalFormat("0.00");//这里如果写 #.00 将会通不过。。。郁闷了很长时间
29 double perimeter = PI * diameter;
30 double inchesDistance = perimeter * revolutions;
31 double feetDistance = inchesDistance / InchesPerFoot;
32 distance = feetDistance / FeetPerMile;
33 double minuteTime = time / SecondsPerMinute;
34 double hourTime = minuteTime / MinutesPerHour;
35 mph = distance / hourTime;
36 System.out.println("Trip #" + count + ": " + df.format(distance) + " " + df.format(mph));
37 count ++;
38 }
39
40 public static void main(String args[]){
41 Scanner scan = new Scanner(System.in);
42 while(scan.hasNext()){
43 double diameter = scan.nextDouble();
44 int revolutions = scan.nextInt();
45 if(revolutions == 0)
46 break;
47 double time = scan.nextDouble();
48 Main trip = new Main(diameter, revolutions, time);
49 trip.calculate();
50 }
51 }
52 }