标签:
-------------------------------------------
需要注意的就是考虑各种情况,各种!各种!!各种!!!各种情况,真是一个让人崩溃但是炒鸡修身养性的题啊....:)
AC代码:
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 5 public class Main { 6 7 public static void main(String[] args) throws IOException { 8 9 BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); 10 11 boolean first=true; 12 while(first || reader.ready()){ 13 first=false; 14 String expression=reader.readLine(); 15 String ans=solve(expression); 16 System.out.println(ans); 17 } 18 19 } 20 21 public static String solve(String expressions){ 22 int a=expressions.charAt(0)-‘0‘; 23 int b=expressions.charAt(2)-‘0‘; 24 int c=expressions.charAt(4)-‘0‘; 25 int d=expressions.charAt(6)-‘0‘; 26 27 char o=expressions.charAt(3); 28 29 if(b==0 || d==0) return "0"; 30 31 int e=(o==‘-‘)?(a*d-b*c):(a*d+b*c); 32 int f=b*d; 33 34 if(e==0) return "0"; 35 if(e==f) return "1"; 36 if(f==1) return Integer.toString(e); 37 38 int t=gcd(Math.abs(e),Math.abs(f)); 39 if(t==f) return Integer.toString(e/t); 40 return String.format("%d/%d",e/t,f/t); 41 } 42 43 public static int gcd(int a,int b){ 44 return b==0?a:gcd(b,a%b); 45 } 46 47 }
题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=111
标签:
原文地址:http://www.cnblogs.com/cc11001100/p/5893865.html