fork download
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintWriter;
  4. import java.text.DecimalFormat;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. int n = 1000000;
  10. double[] a = new double[n];
  11. for(int i=0;i<n;i++) {
  12. a[i] = Math.random();
  13. }
  14. // format1(n,a);
  15. // format2(n,a);
  16. // format3(n,a);
  17. format4(n,a);
  18. }
  19.  
  20. public static void format1(int n,double[] a) {
  21. try {
  22. PrintWriter pw = new PrintWriter(new File("out1.txt"));
  23. long stime = System.nanoTime();
  24. for(int i=0;i<n;i++) {
  25. pw.printf("%.7f\n", a[i]);
  26. }
  27. pw.flush();
  28. pw.close();
  29. System.out.println((System.nanoTime() - stime) / 1000000 + " ms");
  30. } catch (FileNotFoundException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34.  
  35. public static void format2(int n,double[] a) {
  36. try {
  37. PrintWriter pw = new PrintWriter(new File("out2.txt"));
  38. long stime = System.nanoTime();
  39. for(int i=0;i<n;i++) {
  40. pw.println(String.format("%.7f", a[i]));
  41. }
  42. pw.flush();
  43. pw.close();
  44. System.out.println((System.nanoTime() - stime) / 1000000 + " ms");
  45. } catch (FileNotFoundException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49.  
  50. public static void format3(int n,double[] a) {
  51.  
  52. DecimalFormat df = new DecimalFormat("0.0000000");
  53. try {
  54. PrintWriter pw = new PrintWriter(new File("out3.txt"));
  55. long stime = System.nanoTime();
  56. for(int i=0;i<n;i++) {
  57. pw.printf(df.format(a[i]));
  58. }
  59. pw.flush();
  60. pw.close();
  61. System.out.println((System.nanoTime() - stime) / 1000000 + " ms");
  62. } catch (FileNotFoundException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66.  
  67. public static void format4(int n,double[] a) {
  68. try {
  69. PrintWriter pw = new PrintWriter(new File("out4.txt"));
  70. long stime = System.nanoTime();
  71. for(int i=0;i<n;i++) {
  72. pw.printf(dtos(a[i],7));
  73. }
  74. pw.flush();
  75. pw.close();
  76. System.out.println((System.nanoTime() - stime) / 1000000 + " ms");
  77. } catch (FileNotFoundException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81.  
  82. // http://q...content-available-to-author-only...a.com/p_shiki37/items/65c18f88f4d24b2c528b#comment-0b10eebdffb1991f3eb9
  83. public static String dtos(double x, int n) {
  84. StringBuilder sb = new StringBuilder();
  85. if (x < 0) {
  86. sb.append('-');
  87. x = -x;
  88. }
  89. x += Math.pow(10, -n) / 2;
  90. // if(x < 0){ x = 0; }
  91. sb.append((long) x);
  92. sb.append(".");
  93. x -= (long) x;
  94. for (int i = 0; i < n; i++) {
  95. x *= 10;
  96. sb.append((int) x);
  97. x -= (int) x;
  98. }
  99. return sb.toString();
  100. }
  101. }
  102.  
Success #stdin #stdout #stderr 0.17s 320576KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
java.io.FileNotFoundException: out4.txt (Permission denied)
	at java.io.FileOutputStream.open0(Native Method)
	at java.io.FileOutputStream.open(FileOutputStream.java:270)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
	at java.io.PrintWriter.<init>(PrintWriter.java:263)
	at Main.format4(Main.java:69)
	at Main.main(Main.java:17)