题目描述
编一个“念数字”的程序。当你输入一个0-99之间的数后,计算机就会用汉语拼音输出这个数的“念”法。
比如:35,念出来应该是:san shi wu;16念出来应该是shi liu,0念出来应该是ling!
输入格式
一行,一个整数。
输出格式
一行,这个整数的念法
定义一个拼音数组,然后根据整数拆分出十位和个位
#include <iostream>
using namespace std;
int main()
{
char * py[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
int n;
cin>>n;
int t = n/10;
int g = n%10;
if(n==0)
cout<<py[0];
else
{
if(t != 0)
{
if(t>1)
cout<<py[t]<<" ";
cout<<"shi";
if(g != 0)
cout<<" ";
}
if(g != 0)
{
cout<<py[g];
}
}
}
拆分出各位数字,并判断位数,如果是2位数,就中间输出一个shi
10单独判读
运行结果:
代码:
#include <iostream>
using namespace std;
int main()
{
int n;
const char* p[] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
cin >> n;
if (n == 10)
cout << "shi";
else if (n < 10)
cout << p[n];
else
{
int a = n / 10;
int b = n % 10;
if (b != 0)
cout << p[a] << " shi " << p[b];
else
cout << p[i] << " shi";
}
return 0;
}
#include<stdio.h>
int main()
{
char a[10][5]={
"ling",
"yi",
"er",
"san",
"si",
"wu",
"liu",
"qi",
"ba",
"jiu",
};
int b;
scanf("%d",&b);
if(b/10>1)
{
printf("%s ",a[b/10]);
}
if(b/10>0)
{
printf("shi ");
}
if(b%10>0 || b/10==0)
{
printf("%s",a[b%10]);
}
}
以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:
思路:可以使用数组存储数字和对应的拼音,然后根据输入的数字输出对应的拼音。
示例代码:
#include <iostream>
using namespace std;
string pinyin[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
string tenPinyin[10] = {"", "shi", "bai", "qian"};
int main() {
int num;
cin >> num;
if (num == 0) {
cout << "ling" << endl;
return 0;
}
if (num < 0) {
cout << "fu ";
num = -num;
}
int digits[5] = {0};
int len = 0;
while (num > 0) {
digits[len++] = num % 10;
num /= 10;
}
bool needZero = false;
for (int i = len - 1; i >= 0; i--) {
if (digits[i] == 0) {
needZero = true;
continue;
}
if (needZero) {
cout << "ling ";
needZero = false;
}
cout << pinyin[digits[i]];
if (i > 0) {
cout << " " << tenPinyin[i];
}
if (i == 1 && digits[i] == 1 && len > 2) {
cout << " " << tenPinyin[i];
}
if (i > 1 && digits[i-1] == 0 && digits[i-2] == 0) {
cout << " ling";
}
if (i != 0) {
cout << " ";
}
}
cout << endl;
return 0;
}
解释:
首先定义两个数组,一个存储0-9的拼音,一个存储十位及以上的拼音(注意第一个为空字符串)。
接着读入输入的数字,如果是0直接输出“ling”,如果是负数先输出“fu ”,并将数字变为正数。
将数字按位存储在数组digits中。
从高位到低位遍历digits数组,根据当前位的数字输出对应的拼音,需要注意以下几点:
a. 如果当前位为0,需要记录下来,等到下一位不为0再输出“ling”。
b. 如果当前位为1且不是最高位,需要特殊处理,比如16应该输出“shi liu”而不是“yi shi liu”。
c. 如果当前位是千位或百位,且后两位都是0,需要输出“ling”。
最后输出换行符。
这样就完成了“念数字”的程序。
由于要求计算三个的结果并记录,可以先声明一个为零的变量,当第N个条件成立时给此变量加上2的N-1次方,就可以达到标记的目的,在后面的SWITCH中每一个值都有其独特的意义。
#include <iostream>
using namespace std;
int main()
{
int a, n{};
while (1)
{
n = 0;//重置n的值
cout << "请输入一个数 : ";
cin >> a;
if (a % 4 == 0)
n += 1;
if (a % 7 == 0)
n += 2;
if (a % 9 == 0)
n += 4;
switch (n)//根据n的值来选择
{
case 1:
cout << "能被其中一个数 4 整除" << endl;
break;
case 2:
cout << "能被其中一个数 7 整除" << endl;
break;
case 3:
cout << "能被其中两个数 4 7 整除" << endl;
break;
case 4:
cout << "能被其中一个数 9 整除" << endl;
break;
case 5:
cout << "能被其中两个数 4 9 整除" << endl;
break;
case 6:
cout << "能被其中两个数 7 9 整除" << endl;
break;
case 7:
cout << "能同时被 4 7 9 整除" << endl;
break;
default:
cout << "不能被 4 7 9 任一个整除" << endl;
break;
}
}
}