zoukankan      html  css  js  c++  java
  • [PAT] 1005 Spell It Right (20 分)Java

    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:

    12345
    

    Sample Output:

    one five


     1 import java.math.BigInteger;
     2 import java.util.Scanner;
     3 
     4 /**
     5  * @Auther: Xingzheng Wang
     6  * @Date: 2019/2/15 22:01
     7  * @Description: pattest
     8  * @Version: 1.0
     9  */
    10 public class PAT1005 {
    11 
    12     static String[] dig = {"zero","one","two","three","four","five","six","seven","eight","nine"};
    13     public static void main(String[] args){
    14         Scanner sc = new Scanner(System.in);
    15         BigInteger a = sc.nextBigInteger();
    16         String b = a.toString();
    17         Integer ans = 0;
    18         for (int i = 0; i < b.length(); i++) {
    19             ans += (int)b.charAt(i) - '0';     //Char存的是ASCII值, -'0'是求其偏移量。
    20         }
    21         String res = ans.toString();
    22         for (int i = 0; i < res.length(); i++) {
    23             if(i > 0){
    24                 System.out.print(" ");
    25             }
    26             System.out.print(dig[(int)res.charAt(i) - '0']);
    27         }
    28     }
    29 }
  • 相关阅读:
    梯度下降算法实现
    windows10下Anaconda的安装与tensorflow、opencv的安装与环境配置
    在github创建用户
    看完教材不明白的问题
    自我介绍
    HDU 1098(条件满足 数学)
    HDU 1097(m次幂的个位数 规律)
    HDU 1046(最短路径 **)
    HDU 1045(炮台安置 DFS)
    HDU 1034(传递糖果 模拟)
  • 原文地址:https://www.cnblogs.com/PureJava/p/10497941.html
Copyright © 2011-2022 走看看