作者:莫启飞
环境:vs2019
Problem Description
给出在[0, 100]范围内的整数数字,你能把对应的中文写出来吗?
Input
第一行输入一个T (T <= 20),后面每行一个数字,题目保证数字是[0, 100]。
Output
第一行输入一个T (T <= 20),后面每行一个数字,题目保证数字是[0, 100]。
Sample Input
5 1 22 3 4 100Sample Output
一 二十二 三 四 一百
#include<iostream>
using namespace std;
string arr[10] = { "零","一","二","三","四","五","六","七","八","九"};
void sum(int x) {
if (x < 10)
{
cout << arr[x];
}
else if (x == 10) {
cout << "十";
}
else if (x == 100) {
cout << "一百";
}
else {
int y = x / 10;
cout << arr[y] << "十";
int z = x - y * 10;
if (z > 0)
cout << arr[z];
}
cout << endl;
}
int main() {
int n;
cin >> n;
while (n--) {
int x;cin >> x;
sum(x);
}
return 0;
}
bug及心得:一开始:int y = x / 10;//这里得的是十位数的数,列如 47/10=4
cout << arr[y] << "十";
int z = x - y * 10; // 这里得的是个位数的数 或者写成int z = x%10;
if (z > 0)//这里特别容易忘记,不然打印的全部都是以十结尾,
cout << arr[z];