PAT乙级(Basic Level)1002(C++)

1002. 写出这个数 (20)

读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100

输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。

输入样例:
1234567890987654321123456789
输出样例:
yi san wu
#include
using namespace std;
int main(){
	int n,a;
	cin>>n;
	int sum=0;
	while(n){//计算数字各位之和 
		sum=sum+n%10;
		n=n/10;
	}
	if(a>10){
	    sum=sum/10;
	    a=sum;
    if(a>10){
      a=a/10;	
	}
	switch(a){
		case 1:
			cout<<"yi ";
			break;
		case 2:
			cout<<"er ";
			break;
		case 3:
			cout<<"san ";
			break;
		case 4:
			cout<<"si ";
			break;
		case 5:
			cout<<"wu ";
			break;
		case 6:
			cout<<"liu ";
			break;
		case 7:
			cout<<"qi ";
			break;
		case 8:
			cout<<"ba ";
			break;
		case 9:
			cout<<"jiu ";
			break;
		case 0:
			cout<<"ling ";
			break;
	}
    }
	return 0;
} 

#include  
#include  
#include    
void print_sum(int n){  
    char *name[] ={"ling","yi","er","san","si","wu","liu","qi","ba","jiu","shi"};  
    int a[10];  
    int i=0;  
    for(i=0;n>=10;i++){  
        a[i]=n%10;  
        n=n/10;  
    }  
     a[i++]=n;  
  
 while(i--){  
    if(i!=0){  
      printf("%s ",name[a[i]]);  
    }else{  
    printf("%s\n",name[a[i]]);  
    }  
  }  
  
      
}  
int main(){  
    char s[100];  
    char *n=s;  
    scanf("%s",s);  
    int sum=0;  
    while(*n!='\0'){  
        sum=sum+(*n-'0');  
        n++;  
    }  
    print_sum(sum);  
}  


你可能感兴趣的:(PAT乙级)