PAT甲级练习1005. Spell It Right (20)
来源:互联网 发布:网络流行语 我倒 编辑:程序博客网 时间:2024/06/05 15:25
1005. Spell It Right (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).
Output Specification:
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.
Sample Input:12345Sample Output:
one five
import java.math.BigInteger;import java.util.Scanner;public class Main {public static void main(String[] args) {String[] number = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; BigInteger sum = BigInteger.ZERO;StringBuffer sum_string;int sum_int;Scanner s = new Scanner(System.in);BigInteger n = s.nextBigInteger();while(n != BigInteger.valueOf(0)){sum = sum.add(n.mod(BigInteger.TEN));n = n.divide(BigInteger.valueOf(10));}sum_string = new StringBuffer(sum.toString());sum_string.reverse();sum_int = Integer.valueOf(sum_string.toString());System.out.printf("%s", number[sum_int%10]);sum_int = sum_int/10;for(int i=1; i<sum_string.length(); i++){System.out.printf(" %s", number[sum_int%10]);sum_int = sum_int/10;}}}
但是看了一下别人的代码,发现!!!
可以直接字符串读入,然后直接把各位字符想加直接处理得到!!!
还是应该聪明一点
按该思路的代码如下
import java.util.ArrayList;import java.util.Scanner;public class Main {public static void main(String[] args) {ArrayList<Integer> list = new ArrayList<Integer>();String[] number = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; String sum_string;int sum_int = 0;Scanner s = new Scanner(System.in);sum_string = s.next();for(int i=0; i<sum_string.length(); i++){sum_int += sum_string.charAt(i) - '0'; }while(sum_int/10 != 0){list.add(sum_int%10);sum_int /= 10;}System.out.printf("%s", number[sum_int]);for(int i=list.size();i>0;i--){System.out.printf(" %s", number[list.get(i-1)]);}}}
0 0
- PAT甲级练习1005. Spell It Right (20)
- PAT - 甲级 - 1005. Spell It Right (20)
- PAT甲级.1005. Spell It Right (20)
- 【PAT甲级】1005. Spell It Right (20)
- PAT甲级1005. Spell It Right (20)
- PAT甲级 1005. Spell It Right (20)
- PAT甲级1005. Spell It Right (20)
- PAT 甲级 1005. Spell It Right (20)
- PAT甲级1005. Spell It Right
- PAT 甲级 1005. Spell It Right
- [PAT-甲级]1005.Spell It Right
- PAT甲级1005. Spell It Right
- PAT 甲级 1005. Spell It Right
- 1005. Spell It Right (20)-PAT甲级真题
- 【PAT甲级】【C++】1005. Spell It Right (20)
- PAT TEST 甲级1005. Spell It Right (20)
- PAT(甲级)1005. Spell It Right (20)
- PAT甲级 1005:Spell it Right
- ROM RAM 内存 外存
- 利用dataset将数据给到dataGridview
- TCP/IP:tcp快速重传和快速恢复
- 委托和事件
- RocketMQ总结整理
- PAT甲级练习1005. Spell It Right (20)
- PackageManagerService启动流程源码解析
- c++中的动态绑定
- java学习记录----面向对象(三)
- Unity 点击屏幕与UGUI的区分
- 关于数据库索引
- Java动态代理深入解析
- 这是我的第一篇博客
- 欢迎使用CSDN-markdown编辑器