题目如下:
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 sumList =new Vector();
for(int i=0;i<1000;i++){
sumList=double_num(sumList);
}
for(int i=0;i
sum+=sumList.get(i);
}
System.out.println(sum);
}
public static Vector double_num(Vector a){
Vector intNum=new Vector();
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
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;
}
}