poj1001(高精度)

来源:互联网 发布:数据库物理模型主键 编辑:程序博客网 时间:2024/04/30 22:19
  1. 转载自:http://www.cppblog.com/vontroy/archive/2010/05/24/116233.html
  2. 先说一下Java对于ACM的一些优点吧:
  3. (1) 对于熟悉C/C++的程序员来说Java 并不难学,两周时间基本可以搞定一般的编程,再用些时间了解一下Java库就行了。
  4. Java的语法和C++非常类似,可以说是C++的升级版,只是更加强调面向对象思想而已。(个人见解。。。)
  5. (2) 在一般比赛中,Java程序会有额外的时间和空间,但真正进行大规模运算时Java并不比C/C++慢,
  6. 输入输出效率比较低而已
  7. (3) Java 代码简单且功能强大,有些像高精度之类的算法用Java实现起来更为简洁方便
  8. (ACM真正比赛时是讲究做题速度的,任何题只要能过就行,而不必过于要求程序的速度有多高,不超时就好)。
  9. ***小技巧:某些题目用Java超时的话可以用Java打表然后用C/C++提交
  10. (4) 用Java不易犯细微的错误,比如C/C++中的指针, “if (n = m) ... ” 等。
  11. (5) 目前Eclipse已成基本配置,写Java程序反而比C/C++更方便调试。在具体竞赛时也算多一种选择。
  12. 关于ACM中应用的一些问题:
  13. (1) JDK 1.5.0 及其以上版本提供的Scanner类为输入提供了良好的基础,很好地优化Java的输入问题。
  14. 代码如下:
  15. import java.io.* import java.util.*
  16. public class Main
  17. {
  18. public static void main(String args[])
  19. {
  20. Scanner cin = new Scanner(new BufferedInputStream(System.in));
  21. }
  22. }
  23. 也可以直接 Scanner cin = new Scanner(System.in);
  24. 加Buffer可能会快一些。
  25. (2) 读一个整数: int n = cin.nextInt();
  26. 相当于 scanf("%d", &n);
  27. 或 cin >> n;
  28. 读一个字符串:String s = cin.next();
  29. 相当于 scanf("%s", s);
  30. 或 cin >> s;
  31. 读一个浮点数:double t = cin.nextDouble();
  32. 相当于 scanf("%lf", &t);
  33. 或 cin >> t;
  34. 读一整行: String s = cin.nextLine();
  35. 相当于 gets(s);
  36. 或 cin.getline(...);
  37. 判断是否有下一个输入可以用 cin.hasNext() 或 cin.hasNextInt() 或 cin.hasNextDouble()
  38. (3) 输出一般可以直接用 System.out.print() 和 System.out.println(),前者不输出换行,而后者输出。
  39. System.out.println(n); // n 为 int 型 同一行输出多个整数可以用
  40. System.out.println(new Integer(n).toString() + " " + new Integer(m).toString());
  41. //也可重新定义:
  42. static PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
  43. cout.println(n);
  44. (4)对于输出浮点数保留几位小数的问题,可以使用DecimalFormat类,
  45. import java.text.*;
  46. DecimalFormat f = new DecimalFormat("#.00#");
  47. DecimalFormat g = new DecimalFormat("0.000");
  48. double a = 123.45678, b = 0.12;
  49. System.out.println(f.format(a));
  50. System.out.println(f.format(b));
  51. System.out.println(g.format(b));
  52. 大数:
  53. BigInteger 和 BigDecimal 是在java.math包中已有的类,前者表示整数,后者表示浮点数
  54. import java.math.* // 需要引入 java.math 包
  55. BigInteger a = BigInteger.valueOf(100);
  56. BigInteger b = BigInteger.valueOf(50);
  57. BigInteger c = a.add(b) // c = a + b;
  58. //主要有以下方法可以使用:
  59. BigInteger add(BigInteger other)
  60. BigInteger subtract(BigInteger other)
  61. BigInteger multiply(BigInteger other)
  62. BigInteger divide(BigInteger other)
  63. BigInteger mod(BigInteger other)
  64. int compareTo(BigInteger other)
  65. static BigInteger valueOf(long x)
  66. //输出数字时直接使用 System.out.println(a) 即可
  67. 字符串:
  68. String 类用来存储字符串,可以用charAt方法来取出其中某一字节,计数从0开始:
  69. String a = "Hello"; // a.charAt(1) = 'e'
  70. 用substring方法可得到子串,如上例
  71. System.out.println(a.substring(0, 4)) // output "Hell"
  72. 注意第2个参数位置上的字符不包括进来。这样做使得 s.substring(a, b) 总是有 b-a个字符。
  73. 字符串连接可以直接用 + 号,如
  74. String a = "Hello";
  75. String b = "world";
  76. System.out.println(a + ", " + b + "!"); // output "Hello, world!"
  77. 如想直接将字符串中的某字节改变,可以使用另外的StringBuffer类。
  78. 调用递归(或其他动态方法)
  79. 在主类中 main 方法必须是 public static void 的,在 main 中调用非static类时会有警告信息,
  80. 可以先建立对象,然后通过对象调用方法:
  81. public class Main
  82. {
  83. void dfs(int a)
  84. {
  85. if () return;
  86. dfs(a+1);
  87. }
  88. public static void main(String args[])
  89. {
  90. Main e = new Main();
  91. e.dfs(0);
  92. }
  93. }
  94. 其他注意的事项:
  95. (1) Java 是面向对象的语言,思考方法需要变换一下,里面的函数统称为方法,不要搞错。
  96. (2) Java 里的数组有些变动,多维数组的内部其实都是指针,所以Java不支持fill多维数组。
  97. 数组定义后必须初始化,如 int[] a = new int[100];
  98. (3) 布尔类型为 boolean,只有true和false二值,在 if (...) / while (...) 等语句的条件中必须为boolean类型。
  99. 在C/C++中的 if (n % 2) ... 在Java中无法编译通过。
  100. (4) 下面在java.util包里Arrays类的几个方法可替代C/C++里的memset、qsort/sort 和 bsearch:
  101. Arrays.fill()
  102. Arrays.sort()
  103. Arrays.binarySearch()
  104. 虽然Java功能很强大,但不能完全依赖他,毕竟C和C++还是ACM/ICPC的主流语言,
  105. 适当地使用才能有效提高比赛中的成绩。。。
  106. 附:
  107. 例题:POJ 1001
  108. import java.io.*;
  109. import java.util.*;
  110. import java.math.BigDecimal;
  111. public class Main
  112. {
  113. public static void main(String args[])
  114. {
  115. Scanner cin = new Scanner(System.in);
  116. BigDecimal num;
  117. int n;
  118. String r;
  119. while (cin.hasNextBigDecimal())
  120. {
  121. num = cin.nextBigDecimal();
  122. n = cin.nextInt();
  123. num = num.pow(n);
  124. r = num.stripTrailingZeros().toPlainString();
  125. if (r.startsWith("0.")) r = r.substring(1);
  126. System.out.println(r);
  127. }
  128. }
  129. }   
原创粉丝点击