zoukankan      html  css  js  c++  java
  • [PTA] PAT(A) 1005 Spell It Right (20分)

    Problem

    Description

    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

    Each input file contains one test case. Each case occupies one line which contains an $N$($leq10^{100}$).

    ## Output

    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 ### Sample Input ``` 12345 ``` ### Sample Output ``` one five ```

    Solution

    Analyse

    将一个给出的数,每一位的数值全部加起来,将最终的结果用英文输出。
    每一位的数值加起来,可以直接读入一行,然后一个字符一个字符处理,也可以直接读取一个字符,处理一个字符。
    最终的结果用英文输出,将每一个数字对应的英文存储在数组中,数字作为下标使用。将最终的结果转换为字符串,然后一个个字符进行处理

    Code

    #include <bits/stdc++.h>
    using namespace std;
    
    string word[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    
    void print_english(int x) {
    	string str = to_string(x);
    	for (int i = 0; i < str.size(); i++) {
    		if (i) cout << " ";
    		cout << word[str[i] - '0'];
    	}
    	cout << endl;
    }
    
    int main(void) {
    	char ch;
    	int sum = 0;
    	while (cin >> ch) {
    		int x = ch - '0';
    		sum += x;
    	}
    	print_english(sum);
    }
    

    Result

  • 相关阅读:
    java内置数据类型
    docker安装及配置
    redis持久化
    golang linux安装
    TCP/IP协议
    php高并发,大流量
    C语言阐述进程和线程的区别
    python 消息队列-rabbitMQ 和 redis介绍使用
    python 新手题目-文件读写-关键字判断
    python IO模式(多路复用和异步IO深入理解)
  • 原文地址:https://www.cnblogs.com/by-sknight/p/11403501.html
Copyright © 2011-2022 走看看