PAT乙级 1002 写出这个数 C++
作者:互联网
//读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
#include <iostream>
#include <stdio.h>
#include <string.h>
int main(void)
{
char num[102] = { 0 };
char pinyin[3] = { 0 };
int i = 0;
int sum = 0;
int cnt = 0;
std::cin >> num;
for (i = 0; i < 102; i++)
{
if (strcmp(&num[i], "\0") == 0) break;
sum += num[i] - '0';
}
if (sum / 100 != 0)
{
pinyin[0] = sum / 100;
pinyin[1] = (sum - (sum / 100) * 100) / 10;
pinyin[2] = sum % 10;
cnt = 3;
}
else if ((sum / 100 == 0) && (sum / 10 != 0))
{
pinyin[0] = sum / 10;
pinyin[1] = sum % 10;
cnt = 2;
}
else
{
pinyin[0] = sum % 10;
cnt = 1;
}
for (i = 0; i < cnt; i++)
{
switch (pinyin[i])
{
case 0:std::cout << "ling"; break;
case 1:std::cout << "yi"; break;
case 2:std::cout << "er"; break;
case 3:std::cout << "san"; break;
case 4:std::cout << "si"; break;
case 5:std::cout << "wu"; break;
case 6:std::cout << "liu"; break;
case 7:std::cout << "qi"; break;
case 8:std::cout << "ba"; break;
case 9:std::cout << "jiu"; break;
}
if (i < cnt-1) std::cout << " ";
}
return 0;
}
标签:std,case,cnt,PAT,cout,pinyin,sum,C++,1002 来源: https://www.cnblogs.com/ocha/p/16481574.html