PAT-B 1002. 寫出這個數(shù) (20)

傳送門

https://pintia.cn/problem-sets/994805260223102976/problems/994805324509200384

題目

讀入一個自然數(shù)n,計(jì)算其各位數(shù)字之和,用漢語拼音寫出和的每一位數(shù)字。
輸入格式:每個測試輸入包含1個測試用例,即給出自然數(shù)n的值。這里保證n小于10100。
輸出格式:在一行內(nèi)輸出n的各位數(shù)字之和的每一位,拼音數(shù)字間有1 空格,但一行中最后一個拼音數(shù)字后沒有空格。
輸入樣例:
1234567890987654321123456789
輸出樣例:
yi san wu

分析

Java實(shí)現(xiàn)方法是:
1.讀入一行字符串,然后將字符串轉(zhuǎn)為char數(shù)組;
2.將char數(shù)組的每位轉(zhuǎn)為int型,這里注意要減去0的ASCII碼為48,然后相加起來;
3.然后將和再轉(zhuǎn)為char數(shù)組,根據(jù)每位的數(shù)字,輸出對應(yīng)的拼音即可,需要注意的是最后沒有空格。

C++實(shí)現(xiàn)方法與Java類似,這里就不再贅述。

源代碼

//C/C++實(shí)現(xiàn)
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <sstream>

using namespace std;

int main(){
    char c[102];
    gets(c);
    int sum = 0;
    for(int i = 0; i < strlen(c); i ++){
        sum += (c[i] - '0');
    }
    stringstream ss;
    ss << sum;
    string s = ss.str();
    for(int j = 0; j < s.size(); j ++){
        if(s[j] == '0'){
            printf("ling");
        }
        else if(s[j] == '1'){
            printf("yi");
        }
        else if(s[j] == '2'){
            printf("er");
        }
        else if(s[j] == '3'){
            printf("san");
        }
        else if(s[j] == '4'){
            printf("si");
        }
        else if(s[j] == '5'){
            printf("wu");
        }
        else if(s[j] == '6'){
            printf("liu");
        }
        else if(s[j] == '7'){
            printf("qi");
        }
        else if(s[j] == '8'){
            printf("ba");
        }
        else{
            printf("jiu");
        }
        if(j != s.size() - 1){
            printf("%c", ' ');
        }
    }
    printf("%c", '\n');
    return 0;
}
//Java實(shí)現(xiàn)
import java.util.Scanner;
public class Main {

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.next();
        char[] c1 = s1.toCharArray();
        int sum = 0;
        for(int i=0;i<c1.length;i++) {
            int n = c1[i] - 48;
            sum += n;
        }
        String s2 = String.valueOf(sum);
        char[] c2 = s2.toCharArray();
        for(int j=0;j<c2.length;j++) {
            if (c2[j] == '1') {
                System.out.print("yi");
            }
            else if (c2[j] == '2') {
                System.out.print("er");
            }
            else if (c2[j] == '3'){
                System.out.print("san");
            }
            else if (c2[j] == '4') {
                System.out.print("si");
            }
            else if (c2[j] == '5') {
                System.out.print("wu");
            }
            else if (c2[j] == '6'){
                System.out.print("liu");
            }
            else if (c2[j] == '7') {
                System.out.print("qi");
            }
            else if (c2[j] == '8') {
                System.out.print("ba");
            }
            else if(c2[j] == '9') {
                System.out.print("jiu");
            }
            else{
                System.out.print("ling");
            }
            if(j < c2.length - 1){
                System.out.print(' ');
            }
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容