PAT (Basic Level) Practise (中文)
1002. 写出这个数 (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100。
输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。
输入样例:1234567890987654321123456789输出样例:
yi san wu
解:
C++:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string num;
string s[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
cin>>num;
int count = 0;
for(int i=0;i<num.size();++i)
count += num[i]-'0';
int digit=0;
int digitalNum[3];
int tempCount = count;
while(tempCount)
{
digitalNum[digit++] = tempCount %10;
tempCount/=10;
}
cout<<s[digitalNum[digit-1]];
for(int i = digit-2;i>=0;i--)
{
cout<<" "<<s[digitalNum[i]];
}
return 0;
}
Java:
import java.util.Scanner;
class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input= new Scanner(System.in);
String num = input.next();
String []s = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
;
int count = 0;
for(int i=0;i<num.length();++i)
count += num.charAt(i)-'0';
int digit=0;
int []digitalNum = new int [3];
int tempCount = count;
while(tempCount>0)
{
digitalNum[digit++] = tempCount %10;
tempCount/=10;
}
System.out.print(s[digitalNum[digit-1]]);
for(int i = digit-2;i>=0;i--)
System.out.print(" "+s[digitalNum[i]]);
}
}