project Euler problem13

計算數組中對應位置的存儲個位數的數字時出現錯誤,求商求餘進行對最高位的剝離時出現錯誤,可以直接算出該數是n位數,然後求商temp=number/10^n,再用number—temp*10^n,或者直接用
number=number%10^n;我用的是第一種方法,用時11ms
import java.io.*;

/**
 * Created by Administrator on 2015/5/11.
 */
public class E13 {
    //10050位數寫進一個100*50 的數組;
    public static int[][] readInToArray(String filename)
    {
        int array[][]=new int[100][50];
        File file=new File(filename);
        BufferedReader bf = null;
        try {
            int row=0;
            FileReader fileReader = new FileReader(file);
            bf=new BufferedReader(fileReader);
            String line=new String();
            while ((line=bf.readLine())!=null)
            {
                for(int col=0;col<50;col++)
                    array[row][col]=Integer.parseInt(line.substring(col,col+1));
             row=row+1;
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally {
            try {
                if (bf== null)
                    bf.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return array;
    }
    //計算50位數上每位數的值;
    public static int[] computSum(int[][]array )
    {
        int []tempresult=new int[51];//用來存儲51位數的每位數的值;
           int carry=0;//設置進位標誌
            for(int j=49;j>=0;j--)
            {
                for (int i = 0; i < 100; i++)
                {
                    tempresult[j] += array[i][j];
                }
                tempresult[j]+=carry;
                carry=tempresult[j]/10;
                int valueOfThisPos=compCurSubscript(tempresult[j]);
                tempresult[j]=valueOfThisPos;
            }
        tempresult[50]=carry;
        return tempresult;
    }
    //計算當前位置應該填寫的數字只能是個位數、
    public static int compCurSubscript(int x)
    {
        String str=x+"";
        int length=str.length();
        int temp=0;
        for(int i=0;i<length;i++)
        {
            if((length-i-1)!=0)
              temp=x/compute(length-1-i);
            else
              temp=x;
            x=x-temp*compute(length-i-1);

        }
        return temp;
    }
    //計算最大位數向前進位的數字之和。
     public static int sumofLastCarry(int lastcarry)
     {
         String str=lastcarry+"";
         int length=str.length();
         int temp=0;
         for(int i=0;i<length;i++)
         {   int subsum=0;
             if((length-i-1)!=0)
                 subsum=lastcarry/compute(length-1-i);
             else
             subsum=lastcarry;
             lastcarry=lastcarry-subsum*compute(length-i-1);
             temp+=subsum;
         }
         return temp;

     }
    //求一個n位數的最後的最低一位數每次需要減去的10的倍數、
    public static  int compute(int length)
    {
        int sum=1;
        for(int i=0;i<length;i++)
            sum=sum*10;
        return sum;
    }

    public static void main(String[] args) {
        int[][] array = readInToArray("C:\\Users\\Administrator\\Desktop\\E13.txt");
       int[] result = computSum(array);
       int lastcarry=result[50];//數組的第51個位置記錄的是最大位加完的結果
       String str=lastcarry+"";
        int length=str.length();;
        System.out.print(lastcarry);
        for (int i = 0; i < 10-length; i++)
        {
            System.out.print(result[i]);
        }

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章