HDOJ1717 decimal fraction of 2 (JAVA implementation, an analog math +)

Topic links: HDOJ1717

Decimal fractions of 2

The Description Problem:
Ray listen in math class the teacher said, can be expressed in terms of any decimal fraction, he began a technology up, and soon he was finished, but he thought a problem, how to put into a recurring decimal fraction what?
you are to write a program that not only can the ordinary into the most simple decimal fractions, decimals can also be put into the most simple fraction.

Input:
The first line is an integer N, the number of sets of data.
Each set of data only a decimal fraction, the integer portion is zero. Decimal digits no more than 9, with the loop portion () enclosed.

Output:
the most simple fraction corresponding to each decimal into output pairs per line.

Sample Input:
3
0.(4)
0.5
0.32(692307)

Sample Output:
4/9
1/2
17/52

This question is very simple thought would just see the title, but to convert decimals to fractions when, found himself with no ideas. So Gangster reference to some articles recurring decimal fraction turn

Japan's Tetsuya Noguchi Code in the "Good heavens! Mathematical original can learn "how the method described in decimals converted to scores, are presented below:
1. decimals 0.7272 ...... two cyclic 7,2 knots, and therefore into a score of 72/99 = 1 / 8. several i.e. divided by the cycle numbers on several 9. Another example

0.123123 ...... 1,2,3 three circular section, and therefore into fraction 123/999 = 41/333. This method is only applicable to the first to start after the decimal point

Circulating decimal, if not start from the first cycle of the decimal, the following method must be used.

2. decimals 0.41666 0.41666 first ...... ...... ...... 41.666 multiplied by 100 to obtain, as will be appreciated ...... 41 + 0.666, it is written score

41 + 6/9 + 2 = 41/3 = 125/3 because multiplying a start 100, divided by 100, i.e. 125/3 ÷ 100 = 125/300 = 5/12.

Attach AC Code

import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner cin=new Scanner(System.in);
		int n=cin.nextInt();
		while(n-->0) {
			String s=cin.next();
			if(!s.contains("(")) {
				String t=s.substring(s.indexOf('.')+1);
				int f=t.length();
				int tt=Integer.parseInt(t);
				int ff=(int)Math.pow(10, f);
				int res=gcd(tt,ff);
				tt/=res;
				ff/=res;
				System.out.println(tt+"/"+ff);
			}else if(s.charAt(2)=='('){
				int start=s.indexOf("(");
				int end=s.indexOf(")");
				String tt=s.substring(start+1, end);
				int ff=tt.length();
				int nn=0;
				for(int i=0;i<ff;i++) {
					nn=nn*10+9;
				}
				int ttt=Integer.parseInt(tt);
				int res=gcd(ttt,nn);
				ttt/=res;
				nn/=res;
				System.out.println(ttt+"/"+nn);
			}else if(s.charAt(2)!='(') {
				int start=s.indexOf("(");
				int end=s.indexOf(")");
				String tt=s.substring(start+1, end);
				int ff=tt.length();
				int nn=0;
				for(int i=0;i<ff;i++) {
					nn=nn*10+9;
				}
				int ttt=Integer.parseInt(tt);
				StringBuilder str=new StringBuilder();
				for(int i=2;i<start;i++) {
					str.append(s.charAt(i));
				}
				int length=str.toString().length();
				int t=Integer.parseInt(str.toString());
				t=t*nn+ttt;
				nn=nn*(int)Math.pow(10, length);
				int res1=gcd(t,nn);
				t/=res1;
				nn/=res1;
				System.out.println(t+"/"+nn);
			}
		}
	}
	public static int gcd(int m,int n) {
		return m%n==0?n:gcd(n,m%n);
	}
}

I variable naming is very chaotic, it is estimated we do not understand, I just wrote to his watch

This question is not difficult from a single analog point of view, the difficulty lies in the combination between the analog and mathematics, recurring decimal fraction of this is important! ! If the time in the online information removed, then this question in less than half an hour, but check the information of the time accounted for more than half, indeed simulation questions ah!

Published 24 original articles · won praise 32 · views 1695

Guess you like

Origin blog.csdn.net/weixin_45729946/article/details/103989788