团体程序设计天梯赛——L1-007 念数字

团体程序设计天梯赛——L1-007 念数字

https://pintia.cn/problem-sets/994805046380707840/problems/994805136889593856

输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出fu字。十个数字对应的拼音如下:

0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu

输入格式:

输入在一行中给出一个整数,如:1234。

提示:整数包括负数、零和正数。

输出格式:

在一行中输出这个整数对应的拼音,每个数字的拼音之间用空格分开,行末没有最后的空格。如 yi er san si。

输入样例:

-600

输出样例:

fu liu ling ling

因为有负号’-'的存在,所以我定义成为了char类型的数组,然后输入这个数组,对数组中所有的元素进行遍历,然后进行逻辑判断。(这里用switch(a[i]-‘0’)可能会更方便一点,但是我ctrl+c,ctrl+v用的太顺手了哈哈哈哈)

#include<iostream>
#include<string>
using namespace std;
int main(){
	char a[100];
	cin>>a;
	for(int i = 0; a[i] != '\0'; i++){
		if(a[i] == '-')	cout<<"fu";
		if(a[i] == '0') cout<<"ling";
		if(a[i] == '1') cout<<"yi";
		if(a[i] == '2') cout<<"er";
		if(a[i] == '3') cout<<"san";
		if(a[i] == '4') cout<<"si";
		if(a[i] == '5') cout<<"wu";
		if(a[i] == '6') cout<<"liu";
		if(a[i] == '7') cout<<"qi";
		if(a[i] == '8') cout<<"ba";
		if(a[i] == '9') cout<<"jiu";
		if(a[i+1] != '\0') cout<<" ";
	}
	cout<<endl;
} 

猜你喜欢

转载自blog.csdn.net/NCU_CirclePlan/article/details/86681904