C/C++教程

PAT乙级 1002 写出这个数 C++

本文主要是介绍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;
}

这篇关于PAT乙级 1002 写出这个数 C++的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!