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]);
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值