Java进阶之欧拉工程 第十六篇【2的1000次方各位之和为多少】

题目如下:

215 = 32768 并且其各位之和为 is 3 + 2 + 7 + 6 + 8 = 26.

21000 的各位数之和是多少?

原题如下:

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 21000?

解题思路:这道题和之前的大数求和的思想有点类似,就是用数组存储大数的每一位数,然后相加,上次写的大数相加的函数稍作修改,这里也就可以用了,java代码如下:


public class Launcher {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int sum=0;
		Vector<Integer> sumList =new Vector<Integer>();
		for(int i=0;i<1000;i++){
			sumList=double_num(sumList);
		}
		for(int i=0;i<sumList.size();i++){
			sum+=sumList.get(i);
		}
		System.out.println(sum);
	}

	public static Vector<Integer> double_num(Vector<Integer> a){  
        Vector<Integer> intNum=new Vector<Integer>();   
        int c=0;  
        int count=0;//进位数,可能取值0和1  
        int localNum=0;//相同位数相加后去掉进1剩下的值  
        if(a.isEmpty())                                                                                            
        {  
                a.add(2);  
            return a;  
        }  
        for(int i=0;i<a.size(); i++){     
                  
        	c=a.get(i)*2;          //位数乘2 
            localNum=(c+count)%10;  
            intNum.add(localNum);  
            if(c+count>9){            //检测是否进位  
                 count=1;  
            }else{  
                 count=0;                  
            }  
        }
        if(count==1){                                                                                                               
                intNum.add(count);    
        }
        
        return intNum;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值