zoukankan      html  css  js  c++  java
  • A1005Spell 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 (≤).

    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

    思路:

    定义字符串s存储N,将字符转化成数字累加,最后将和sum转化成字符串,利用字符串数组映射转化输出。

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 int main() {
     5     string s;
     6     cin >> s;
     7     int sum = 0;
     8     for (int i = 0; i < s.length(); i++) {
     9         sum += s[i] - '0';
    10     }
    11     string str = to_string(sum);
    12     string mp[11] = { "zero","one","two","three","four","five","six","seven","eight","nine","ten" };
    13     for (int i = 0; i < str.length(); i++) {
    14         if (i != 0)cout << " ";
    15         cout << mp[str[i]-'0'];
    16     }
    17     return 0;
    18 }
    作者:PennyXia
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    快速生成移动设备应用图标的在线工具 makeappicon
    绝对令人的惊叹的CSS3折叠效果
    GBin1专题之Web热点秀#14
    对一个正整数n,求出n!中末尾0的个数。
    大整数加法
    HDOJ2013
    HDOJ1203 I NEED A OFFER!
    HDOJ1005【苏哥的解法】
    HDOJ1297
    HDOJ1004
  • 原文地址:https://www.cnblogs.com/PennyXia/p/12296170.html
Copyright © 2011-2022 走看看