PAT 1005

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

解析:此题需要注意一点,虽然题干上说的是输入integer n,但是input上面说了范围为N (<= 10100).所以我们需要用字符串读取。不能使用int

Code:
/*************************************************************************
    > File Name: 1005.cpp
    > Author:
    > Mail:
    > Created Time: 2015年12月08日 星期二 19时43分08秒
 ************************************************************************/

#include<iostream>
#include<sstream>
#include<string>
using namespace std;

string dic[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};

int main(){
    string n;
    cin>>n;

    int sum = 0;
    for(int i=0; i<n.size(); i++){
        sum += n[i]-‘0‘;
    }

    stringstream ss;
    ss << sum;
    string output;
    ss >> output;
    for(int i=0; i<output.size(); i++){
        cout<<dic[output[i]-‘0‘];
        if(i != output.size()-1){
            cout<<" ";
        }
    }
    return 0;
}
时间: 2024-10-13 20:52:41

PAT 1005的相关文章

PAT 1005. Spell It Right

PAT 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 li

PAT 1005. Spell It Right (20)

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 w

PAT 1005 继续(3n+1)猜想

1005. 继续(3n+1)猜想 (25) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候,我们需要计算3.5.8.4.2.1,则当我们对n=5.8.4.2进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这4个数已经在验证3的时候遇到过了,我们称5.8.4.2是被3"覆盖"的数.我们称一个数列中的某个数n为&quo

pat 1005 Programming Pattern (35 分)

Programmers often have a preference among program constructs. For example, some may prefer if(0==a), while others may prefer if(!a). Analyzing such patterns can help to narrow down a programmer's identity, which is useful for detecting plagiarism. No

pat 1005. 继续(3n+1)猜想 (25)

卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候,我们需要计算3.5.8.4.2.1,则当我们对n=5.8.4.2进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这4个数已经在验证3的时候遇到过了,我们称5.8.4.2是被3“覆盖”的数.我们称一个数列中的某个数n为“关键数”,如果n不能被数列中的其他数字所覆盖. 现在给定一系

PAT 1005继续(3n+1)猜想 (25)

import java.util.Arrays; import java.util.Scanner; public class Main7 { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int a=0;int b=0; int[] data = new int[n]; boolean[] check = new boolean[n]; int

PAT 1005. Spell It Right (注意考虑0的情况)

代码: #include<cstdio> #include<cstring> using namespace std; char s[10][20]; int main() { strcpy(s[0],"zero");//对字符数组赋值 strcpy(s[1],"one"); strcpy(s[2],"two"); strcpy(s[3],"three"); strcpy(s[4],"four

PAT——1005. 继续(3n+1)猜想 (25)

卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候,我们需要计算3.5.8.4.2.1,则当我们对n=5.8.4.2进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这4个数已经在验证3的时候遇到过了,我们称5.8.4.2是被3"覆盖"的数.我们称一个数列中的某个数n为"关键数",如果n不能被数列中的

POJ 3690 0与* 二维哈希 模板 +multiset

Constellations Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5923   Accepted: 1164 Description The starry sky in the summer night is one of the most beautiful things on this planet. People imagine that some groups of stars in the sky f