PAT-B 1002. 写出这个数 (20) Stupid!

题目链接:点击打开题目链接


这个题需要注意的就是空格的输出,这应该属于这个题目的边界条件。


AC代码:

#include
#include
using namespace std;
int main()
{
	char n;
	char num[10][5] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
	int sum = 0,x;
	int g,s,b;
	while((n = getchar()) != '\n'){
		if(n =='0'){
			x = 0;
		}else if(n =='1'){
			x = 1;
		}else if(n =='2'){
			x = 2;
		}else if(n =='3'){
			x = 3;
		}else if(n =='4'){
			x = 4;
		}else if(n =='5'){
			x = 5;
		}else if(n =='6'){
			x = 6;
		}else if(n =='7'){
			x = 7;
		}else if(n =='8'){
			x = 8;
		}else if(n =='9'){
			x = 9;
		}
		sum += x;
		
	}
	if(!sum){
		cout << num[0] << endl;
	}else{
		b = sum/100;
		s = sum/10%10;
		g = sum%10;
		
		cout << !b?"":num[b];
		if(b != 0){
			cout << " ";
		}
		cout << (!s && !b)?"":num[s];
		if(b || !b&&s){
			cout << " ";
		}else if(s == 0 && b!=0){
			cout << "";
		}
		cout << num[g];
	}
	
		
	return 0;
	
}


感觉自己好傻,sum+=n-'0'; 这一句话就可以代替所有的if-else。


这里是我第二次做到这个题目的做法,也是题目后面加上“Stupid!”的原因。

你可能感兴趣的:(PAT,(Basic,Level),Practice)