首页 > 专题系列 > Java解POJ > POJ 1079 Ratio [解题报告] Java
2013
11-09

POJ 1079 Ratio [解题报告] Java

Ratio

问题描述 :

If you ever see a televised report on stock market activity, you’ll hear the anchorperson say something like “Gainers outnumbered losers 14 to 9,” which means that for every 14 stocks that increased in value that day, approximately 9 other stocks declined in value. Often, as you hear that, you’ll see on the screen something like this:

Gainers 1498

Losers 902

As a person with a head for numbers, you’ll notice that the anchorperson could have said “Gainers outnumbered losers 5 to 3”, which is a more accurate approximation to what really happened. After all, the exact ratio of winners to losers is (to the nearest millionth) 1.660754, and he reported a ratio of 14 to 9, which is 1.555555, for an error of 0.105199; he could have said “5 to 3”, and introduced an error of only 1.666667-1.660754=0.005913. The estimate “5 to 3” is not as accurate as “1498 to 902” of course; evidently, another goal is to use small integers to express the ratio. So, why did the anchorperson say “14 to 9?” Because his algorithm is to lop off the last two digits of each number and use those as the approximate ratio.

What the anchorman needs is a list of rational approximations of increasing accuracy, so that he can pick one to read on the air. Specifically, he needs a sequence {a_1, a_2, …, a_n} where a_1 is a rational number with denominator 1 that most exactly matches the true ratio of winners to losers (rounding up in case of ties), a_{i+1} is the rational number with least denominator that provides a more accurate approximation than a_i, and a_n is the exact ratio, expressed with the least possible denominator. Given this sequence, the anchorperson can decide which ratio gives the best tradeoff between accuracy and simplicity.

For example, if 5 stocks rose in price and 4 fell, the best approximation with denominator 1 is 1/1; that is, for every stock that fell, about one rose. This answer differs from the exact answer by 0.25 (1.0 vs 1.25). The best approximations with two in the denominator are 2/2 and 3/2, but neither is an improvement on the ratio 1/1, so neither would be considered. The best approximation with three in the denominator 4/3, is more accurate than any seen so far, so it is one that should be reported. Finally, of course, 5/4 is exactly the ratio, and so it is the last number reported in the sequence.

Can you automate this process and help the anchorpeople?

输入:

input contains several pairs of positive integers. Each pair is on a line by itself, beginning in the first column and with a space between the two numbers. The first number of a pair is the number of gaining stocks for the day, and the second number is the number of losing stocks for the day. The total number of stocks never exceeds 5000.

输出:

For each input pair, the standard output should contain a series of approximations to the ratio of gainers to losers. The first approximation has ’1′ as denominator, and the last is exactly the ratio of gainers to losers, expressed as a fraction with least possible denominator. The approximations in between are increasingly accurate and have increasing denominators, as described above.

The approximations for a pair are printed one to a line, beginning in column one, with the numerator and denominator of an approximation separated by a slash (“/”). A blank line separates one sequence of approximations from another.

样例输入:

5 4 
1498 902 

样例输出:

1/1 
4/3 
5/4 

2/1 
3/2 
5/3 
48/29 
53/32 
58/35 
63/38 
68/41 
73/44 
78/47 
83/50 
88/53 
93/56 
377/227 
470/283 
563/339 
656/395 
749/451

解题代码:

import java.io.*;
 public class Main {
  public static int gcd(int a,int b){
   while(a%b!=0){
     int temp=a;
     a=b;
     b=temp%b;
   }
    return b;
}

public static void main(String[] args) throws IOException{
  StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
   while(in.nextToken()!=StreamTokenizer.TT_EOF){
    int a,b;
    double last=100000;
    a=(int)in.nval;	
    in.nextToken();	
    b=(int)in.nval;	
    int devide=gcd(a,b);
     if(devide!=1) {
      a/=devide;
      b/=devide;
     }	
   for(int i=1;i< b;i++) {
     double up=(double)a/((double)b/(double)i);
     int tup1=(int)up;
     int tup2=tup1+1;
     int upf;
    if(Math.abs((double)tup1/i-(double)a/b)< Math.abs((double)tup2/i-(double)a/b))	
	   upf=tup1;
    else upf=tup2;	
    if(Math.abs((double)upf/i-(double)a/b)< last){	
     last=Math.abs((double)upf/i-(double)a/b);	
     System.out.println(upf+"/"+i);	
     }
   }	
    System.out.println(a+"/"+b);
    System.out.println();	
	}

   }
}

  1. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。

  2. 网站做得很好看,内容也多,全。前段时间在博客园里看到有人说:网页的好坏看字体。觉得微软雅黑的字体很好看,然后现在这个网站也用的这个字体!nice!

  3. 因为是要把从字符串s的start位到当前位在hash中重置,修改提交后能accept,但是不修改居然也能accept