PAT甲级——1005.SpellItRight(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 (≤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.

Sample Input:
12345

Sample Output:
one five

个人最初的题解思路是设定字符串常量,求和得出数值后使用if else计算出各位的数字对应输出:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int sum = 0;
    char str[101];
     const char* a[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
     cin>>str;
     int len=strlen(str);
     for(int i=0;i<len;++i)
     {
        sum = sum+(str[i]-'0');
     }
     //cout<<sum<<endl;
     if(sum>=0&&sum<10)
     {
        printf("%s",a[sum%10]);
     }
     else if(sum>10&&sum<100)
     {
        printf("%s %s",a[sum/10],a[sum%10]);
     }
     else if(sum>100)
     {
        printf("%s %s %s",a[sum/100],a[(sum/10)%10],a[sum%10]);
     }
     return 0;
}

更优的方法是:

#include <iostream>
using namespace std;
int main() {
        string a;
        cin >> a;
        int sum = 0;
        for (int i = 0; i < a.length(); i++)
            sum += (a[i] - '0');
        string s = to_string(sum);
        string arr[10] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};
        cout << arr[s[0] - '0'];
        for (int i = 1; i < s.length(); i++)
                cout << " " << arr[s[i] - '0'];
        return 0;
}

to_string函数将数字转为字符串完美解决了各个位置上的数值输出问题

原文地址:https://www.cnblogs.com/mrcangye/p/12231704.html

时间: 2024-11-29 13:01:47

PAT甲级——1005.SpellItRight(20分)的相关文章

PAT甲级——1035 Password (20分)

To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase)

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

pat 1035 Password(20 分)

1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) fro

pat 1077 Kuchiguse(20 分) (字典树)

1077 Kuchiguse(20 分) The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often

[PTA] PAT(A) 1008 Elevator (20 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Problem portal: 1008 Elevator (20 分) Description  The highest building in our city has only one elevator. A request list is made up with $N$ positive numbers

PAT乙级1088-----三人行 (20分)

1088 三人行 (20分) 输入样例 1: 48 3 7 输出样例 1: 48 Ping Cong Gai 输入样例 2: 48 11 6 输出样例 2: No Solution 思路:1.丙的能力值有可能是小数因此要用double 首次通过代码: 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 5 6 7 int main(){ 8 int m,x,y; 9 int flag=1; 10 s

PAT 数列求和-加强版&#160;&#160;&#160;(20分)(简单模拟)

给定某数字A(1≤A≤9)以及非负整数N(0≤N≤100000),求数列之和S=A+AA+AAA+?+AA?A(N个A).例如A=1, N=3时,S=1+11+111=123 输入格式: 输入数字A与非负整数N. 输出格式: 输出其N项数列之和S的值. 输入样例: 1 3 输出样例: 123 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cmath> 5 #i

pat 1062 最简分数(20 分)错误

一个分数一般写成两个整数相除的形式:N/M,其中 M 不为0.最简分数是指分子和分母没有公约数的分数表示形式. 现给定两个不相等的正分数 N?1??/M?1?? 和 N?2??/M?2??,要求你按从小到大的顺序列出它们之间分母为 K 的最简分数. 输入格式: 输入在一行中按 N/M 的格式给出两个正分数,随后是一个正整数分母 K,其间以空格分隔.题目保证给出的所有整数都不超过 1000. 输出格式: 在一行中按 N/M 的格式列出两个给定分数之间分母为 K 的所有最简分数,按从小到大的顺序,其

PAT 甲级 1005 Spell It Right

https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336 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 c