zoukankan      html  css  js  c++  java
  • 1005. Spell It Right

    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:
    12345
    
    Sample 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都不够,所以当做字符串读进来在进行计算

    错误代码:

    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--;
        }
    
      }
    
    }
    
    

      结果

    参考

    http://blog.csdn.net/zhangveni/article/details/50878369

  • 相关阅读:
    C++学习笔记 继承,虚基类
    C++ 学习笔记 静态成员与常成员
    C++学习笔记,初始化列表与构造函数
    C++ 学习笔记 运算符优先级
    C++学习笔记 this指针,对象数组,对象指针数组;
    C++初级基础笔记 标识符 关键字
    C++学习笔记 指向类的数据成员的指针
    C++学习笔记 const修饰类成员与成员函数
    虚幻学习day2 简单手电筒与开关门效果(一)
    虚幻学习Day1(二) 触碰控制灯光开关
  • 原文地址:https://www.cnblogs.com/chen20135517/p/7689203.html
Copyright © 2011-2022 走看看