• PAT甲1005 Spell it right【字符串】


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

    题意:

    给一个最多100位的数字,把各位上的数求和,把结果各位上的数字翻译成英文输出。

    思路:

    暴力。

     1 #include <iostream>
     2 #include <set>
     3 #include <cmath>
     4 #include <stdio.h>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <queue>
     9 #include <map>
    10 using namespace std;
    11 typedef long long LL;
    12 #define inf 0x7f7f7f7f
    13 
    14 char s[105];
    15 string spell[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    16 int str[105];
    17 
    18 int main()
    19 {
    20     scanf("%s", s);
    21     int n = strlen(s);
    22     int ans = 0;
    23     for(int i = 0; i < n; i++){
    24         ans += s[i] - '0';
    25     }
    26     int l = 0;
    27     if(ans == 0){
    28         str[l++] = 0;
    29     }
    30     while(ans){
    31         str[l++] = ans % 10;
    32         ans /= 10;
    33     }
    34 
    35     cout<<spell[str[l - 1]];
    36     for(int i = l - 2; i >= 0; i--){
    37         cout<<" "<<spell[str[i]];
    38     }
    39     cout<<endl;
    40 
    41     return 0;
    42 }
  • 相关阅读:
    Java多线程简介
    Java同步简介
    java enum的用法详解
    Instrumentation(3)
    持久化类的三种实例状态
    依赖注入和控制反转
    事务的4个要素及其工作原理
    mysql创建表与索引
    SpringAOP所支持的AspectJ切点指示器
    使用Spring的命名空间p装配属性-摘自《Spring实战(第3版)》
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9866795.html
Copyright © 2020-2023  润新知