zoukankan      html  css  js  c++  java
  • 【每天一道PAT】1005 Spell It Right

    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 (≤10^100​​).

    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.

    思路

    已知读取的数据最大到100位, 因此使用字符串读取;
    计算出总和后再将sum的每位数字存入动态数组中,由之前定义的数字字符串数组,双重映射,输出sum的每位数字的英文。

    #include <stdio.h>
    #include <string.h>
    #include <vector>
    using namespace std;
    vector<int> nums;
    char num[10][20] ={"zero", "one", "two", "three", "four"
                       , "five", "six", "seven", "eight", "nine"};
    int main()
    {
        char N[101] = {0};
        scanf("%s", N);
        int len = strlen(N);
        int sum = 0;
        for (int i = 0; i < len; ++i)
        {
            sum+= N[i]-'0';
        }
        do
        {
            nums.push_back(sum%10);
            sum /=10;
        }while(sum !=0);
        for (int j = nums.size() - 1; j >= 0 ; --j)
        {
            printf("%s", num[nums[j]]);
            if(j != 0)printf(" ");
        }
    
    }
    
    

  • 相关阅读:
    桃花扇
    望故乡
    Unity资源加载方式总结
    [Spark]-RDD详解之变量&操作
    [Spark]-RDD之创建
    [Spark]-RDD初识
    [Spark]-编译(2.3.1)&部署(YARN-Cluster)
    [Spark]-背景
    [Hive]-常规优化以及执行计划解析
    [转载]线上应用故障排查之一:高memory占用
  • 原文地址:https://www.cnblogs.com/xinyuLee404/p/12618756.html
Copyright © 2011-2022 走看看