Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.java
Input Specification:git
Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).spa
Output Specification:.net
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.code
Sample Input:12345Sample Output:
one five
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); String n,str[]=new String [101]; Integer sum=0; n=in.next(); String s[]={"zero","one","two","three","four","five","six","seven" ,"eight","nine"}; for(int i=0;i<n.length();i++){ sum=sum+n.charAt(i)-'0'; } int len=sum.toString().length(); while(len!=0){ if(len!=1){ System.out.print(s[(int) (sum/Math.pow(10,len-1))]+" "); } else{ System.out.print(s[(int) (sum/Math.pow(10,len-1))]); } sum=(int) (sum%(Math.pow(10,len-1))); len--; } } }
注意點:10^100int和double都不夠,因此當作字符串讀進來在進行計算blog
錯誤代碼:three
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); double n; Integer sum=0; n=in.nextDouble(); String s[]={"zero","one","two","three","four","five","six","seven" ,"eight","nine"}; while(n!=0){ sum=(int) (sum+n%10); n=n/10; } int len=sum.toString().length(); while(len!=0){ if(len!=1){ System.out.print(s[(int) (sum/Math.pow(10,len-1))]+" "); } else{ System.out.print(s[(int) (sum/Math.pow(10,len-1))]); } sum=(int) (sum%(Math.pow(10,len-1))); len--; } } }
結果內存
參考ci
http://blog.csdn.net/zhangveni/article/details/50878369字符串