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

    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 (≤).

    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

    思路:用字符串存储数据(数据过大),对每一位求和并存入数组,倒序输出即可

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=10010;
    int nums[maxn];
    void trance(int m){
        if(m==0){
            printf("zero");
        }
        else if(m==1){
            printf("one");
        }
        else if(m==2){
            printf("two");
        }
        else if(m==3){
            printf("three");
        }
        else if(m==4){
            printf("four");
        }
        else if(m==5){
            printf("five");
        }
        else if(m==6){
            printf("six");
        }
        else if(m==7){
            printf("seven");
        }
        else if(m==8){
            printf("eight");
        }
        else if(m==9){
            printf("nine");
        }
    }
    int main(){
        string n;
        cin>>n;
        if(n=="0"){
            printf("zero
    ");
            return 0;
        }
        long long sum=0;
        for(int i=0;i<n.size();i++){
            sum+=n[i]-'0';
        }
        int i=0;
        while(sum>0){
            nums[i]=sum%10;
            sum/=10;
            i++;
        }
        for(int j=i-1;j>=0;j--){
            trance(nums[j]);
            if(j>0){
                printf(" ");
            }
            else{
                printf("
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    算法复习:字符串
    【第五天打卡。
    【第四天打卡。
    【第三天打卡。
    第二天打卡。
    【唉
    配环境到崩溃系列
    所谓环境……
    【随便吐槽
    第四天。打卡。【偷懒了两天hhhh
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14901484.html
Copyright © 2011-2022 走看看