zoukankan      html  css  js  c++  java
  • 1005. Spell It Right (20) -PAT

    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
    我的代码
    #include<stdio.h>
    
    int main() {
        int sum = 0;
        char input[1000];
        int i = 0;
        while ((input[i] = getchar()) != '
    ') {
            i++;
        }
        int k = i;
        for (i = 0; i < k; i++) {
            sum = sum+input[i]-'0';
        }
        printf("%d ", sum);
        int a = sum / 100;
        int b = (sum - 100*a) / 10;
        int c = sum - a*100 - b*10;
        printf("%d %d %d 
    ", a, b, c);
        switch (a) {
        case 9:printf("nine "); break;
        case 8:printf("eight "); break;
        case 7:printf("seven "); break;
        case 6:printf("six "); break;
        case 5:printf("five "); break;
        case 4:printf("four "); break;
        case 3:printf("three "); break;
        case 2:printf("two "); break;
        case 1:printf("one "); break;
        case 0:break;
        }
        switch (b) {
        case 9:printf("nine "); break;
        case 8:printf("eight "); break;
        case 7:printf("seven "); break;
        case 6:printf("six "); break;
        case 5:printf("five "); break;
        case 4:printf("four "); break;
        case 3:printf("three "); break;
        case 2:printf("two "); break;
        case 1:printf("one "); break;
        case 0:break;
        }
        switch (c) {
        case 9:printf("nine"); break;
        case 8:printf("eight"); break;
        case 7:printf("seven"); break;
        case 6:printf("six"); break;
        case 5:printf("five"); break;
        case 4:printf("four"); break;
        case 3:printf("three"); break;
        case 2:printf("two"); break;
        case 1:printf("one"); break;
        case 0:printf("zero"); break;
        }
        return 0;
    }
     

       感悟:1、读取特大数字的时候 可考虑用字符数组来储存该数 ,第5行。

       2、((input[i] = getchar()) != ' ');     红色括号一定要加! 类似的还有 scanf("%d", &n) != EOF

       3、如何求一个较小的数的各个位上的数字 假设sum 是不超过999的一个数.

      百位  int a = sum / 100;  
      十位  int b = (sum - 100*a) / 10;
      个位  int c = sum - a*100 - b*10;
    4、switch case 语句的运用
  • 相关阅读:
    【Unity】自定义编辑器窗口——拓展编辑器功能
    【Unity】AssetBundle的使用——打包/解包
    【Unity】使用Resources类管理资源
    【Unity】使用AssetDatabase编辑器资源管理
    【Unity】协程Coroutine及Yield常见用法
    【Unity】制作简易定时器(Timer)
    python3使用csv模块读写csv文件
    Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
    MongoDB服务无法启动,发生服务特定错误:100
    ValueError: update only works with $ operators
  • 原文地址:https://www.cnblogs.com/SusanHmmup/p/5064903.html
Copyright © 2011-2022 走看看