在代码竞争中使用java将数据写入文件

时间:2015-07-27 17:12:12

标签: java

在练习上一年谷歌代码卡塞问题时,系统给出错误,即使解决方案是正确的。我遇到过两次。当我检查其他可用的程序员解决方案时,我的解决方案是正确的。两者都产生相同的输出。

 public static void main(String[] args) throws IOException {
        // TODO code application logic here
        String three;
        int i=1;
        try
        {
             File input=new File("input.txt");
             File output=new File("output.txt");
             FileWriter file=new FileWriter(output);
             original=3+(Math.sqrt(5));
             Scanner scan=new Scanner(input);
             number=scan.nextInt();
             BufferedWriter buff=new BufferedWriter(file);
             while(scan.hasNext())
             {      
                 buff.write("Case #"+i+": ");
                 number=scan.nextInt();
                 value=Math.pow(original, number);
                 threedigit=((int)value)%1000;
                 if(threedigit<100)
                 {
                     three= "0"+ (Integer.toString(threedigit));
                    // write.write(Integer.parseInt(three));
                    // threedigit=Integer.parseInt(three);
                     buff.write(three);             System.out.println(three);
                 }
                 else
                 {
                     three=Integer.toString(threedigit);
                     System.out.println(three);
                     threedigit=Integer.parseInt(three);
                     buff.write(three);
                 }
                 i++;
                 buff.newLine();

             }
             buff.close();

         }

输出:

Case #1: 935
Case #2: 471
Case #3: 903
Case #4: 751
Case #5: 027
Case #6: 647
Case #7: 647
Case #8: 647
Case #9: 647
Case #10: 607
Case #11: 647
Case #12: 647
Case #13: 991
Case #14: 143
Case #15: 047
Case #16: 647
Case #17: 647
Case #18: 647
Case #19: 647
Case #20: 647
Case #21: 647
Case #22: 647
Case #23: 647
Case #24: 647
Case #25: 943
Case #26: 647
Case #27: 647
Case #28: 647
Case #29: 335

其他程序员的程序:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;


public class p3small {
    public static void main(String[] args) throws Exception {
        // This was generated using the unix calculator bc, using the script generated by the 
        // other java code (and scale = 100), the results manually copied here.
        String sol[] = {"027", "143", "751", "935", "607", "903", "991", "335", "047", "943", "471", "055", "447", "463", 
            "991","095","607", "263", "151", "855", "527", "743", "351", "135", "407", "903", "791", "135", "647"};

        BufferedReader br = new BufferedReader(new FileReader("p3.in"));
        int t = Integer.parseInt(br.readLine());
        FileWriter out = new FileWriter("p3.out");
        for (int i=0; i<t; i++) {
            int n = Integer.parseInt(br.readLine());
            String res = sol[n-2];
            out.write("Case #" + (i+1) + ": " + res + "\n");
        }
        out.close();
        br.close();

    }
}

我知道我在写入文件时犯了一些错误。 请帮助我,它使我失去动力。

0 个答案:

没有答案