Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3. import java.util.StringTokenizer;
  4.  
  5. /**
  6. * Created by wj on 16/10/21.
  7. */
  8. public class Main {
  9. /**
  10. * judge whether the number is
  11. * pure circulating decimal(-1)
  12. * or mixed recurring decimal(the length of the non-repeating part)
  13. * or non-repeating decimal(0)
  14. * @param num
  15. * @return
  16. */
  17. public static int prime(int num){
  18. //pure circulating decimal
  19. if (num%2 != 0 && num%5 != 0)
  20. return 0;
  21. else{
  22. int prime2 = 0, prime5 = 0;
  23. while(num != 1){
  24. if (num%2 == 0){
  25. prime2++;
  26. num /= 2;
  27. }
  28. else if (num%5 == 0){
  29. prime5++;
  30. num /= 5;
  31. }
  32. else
  33. return Math.max(prime2,prime5);//mixed recurring decimal
  34. }
  35. return 20;//non-repeating decimal
  36. }
  37. }
  38. public static void main(String[] args) {
  39. Scanner input = new Scanner(System.in);
  40. String in = new String();
  41. while(!(in = input.nextLine()).equals("")){
  42. StringTokenizer token = new StringTokenizer(in);
  43. int numerator = Integer.valueOf(token.nextToken());
  44. int denominator = Integer.valueOf(token.nextToken());
  45. if (numerator == 0){
  46. System.out.println(numerator+"/"+denominator+" = 0.(0)");
  47. System.out.println(" 1 = number of digits in repeating cyclen");
  48. }else{
  49. int prime = prime(denominator);
  50. if (prime == 20){
  51. double res = (double)numerator/(double)denominator;
  52. DecimalFormat df = new DecimalFormat();
  53. df.setMaximumFractionDigits(5); // 设置最大小数位,由于最大3000,5位小数就够了
  54. String result = df.format(res);
  55. String dot = (numerator%denominator==0)? "." : "";
  56. System.out.println(numerator+"/"+denominator+" = "+result+dot+"(0)");
  57. System.out.println(" 1 = number of digits in repeating cyclen");
  58. }else{
  59. int number = numerator / denominator;
  60. int tmp_numerator = numerator - number*denominator;
  61. int remainder = 0;
  62. int divisor = tmp_numerator*10;
  63. int quotient = 0;
  64. int standard = tmp_numerator;
  65. String cycle = "";
  66. String noncycle = "";
  67.  
  68. //non-repeating part
  69. for (int i = 0; i < prime; i++) {
  70. quotient = divisor / denominator;
  71. noncycle += quotient;
  72. standard = divisor % denominator;
  73. divisor = standard * 10;
  74. }
  75. //repeating part
  76. while (remainder != standard){
  77. quotient = divisor / denominator;
  78. cycle += quotient;
  79. remainder = divisor % denominator;
  80. divisor = remainder * 10;
  81. }
  82. if (cycle.length()>50)
  83. System.out.println(numerator+"/"+denominator+" = "+number+"."+noncycle+"("+cycle.substring(0,50)+"...)");
  84. else
  85. System.out.println(numerator+"/"+denominator+" = "+number+"."+noncycle+"("+cycle+")");
  86. System.out.println(" "+cycle.length()+" = number of digits in repeating cyclen");
  87.  
  88. }
  89. }
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement