PTA A1005&A1006

时间:2019-09-15
本文章向大家介绍PTA A1005&A1006,主要包括PTA A1005&A1006使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

A1005 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 (≤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

单词

consecutive

英 /kən'sekjʊtɪv/ 美 /kən'sɛkjətɪv/
adj. 连贯的;连续不断的

compute

英 /kəm'pjuːt/ 美 /kəm'pjʊt/

n. 计算;估计;推断
vt. 计算;估算;用计算机计算

vi. 计算;估算;推断

题目分析

这题还是很简单的,10的100次方显然超过了int的范围,long int可能都不行,所以这题肯定不能用处理数字的方法解决,所以我把这个数字看作字符,相加时把每个字符减去一个'0',然后再用sprintf把数字改为字符串,代码如下。

具体代码

#include<stdio.h>
#include<stdlib.h>

const char *ch[] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
int N;
int main(void)
{
    char c[101];
    int i = 0;
    while ((c[i] = getchar()) != '\n')i++;
    int sum = 0;
    while (i--)
    {
        sum += c[i] - '0';
    }
    char c2[101];
    sprintf(c2, "%d", sum);
    printf("%s", ch[c2[0] - '0']);
    i = 1;
    while (c2[i] != '\0')
    {
        printf(" %s", ch[c2[i] - '0']);
        i++;
    }
    system("pause");
}

原文地址:https://www.cnblogs.com/z-y-k/p/11522606.html