pat乙级 1002. 写出这个数 (20)


1002. 写出这个数 (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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

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

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

输入样例:
1234567890987654321123456789
输出样例:
yi san wu

提交代码

/*总体思路为用一个字符串储存这几个数字,然后通过循环计算出数字之和n,再将n的各个数表示出来*/
#include <iostream>
#include <string> 
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	string s;
	cin>>s;
	int l=s.length();
	int i,j,n;
	n=0;
	k=0; 
	int a[10000]={0};
	//计算各个数之和 
	for(i=0;i<l;i++){
		a[i]=s[i]-'0';
		n=n+a[i];     
	}
	//用数组b储存汉语拼音; 
	string b[10];
	b[0]="ling";
	b[1]="yi";
	b[2]="er";
	b[3]="san";
	b[4]="si";
	b[5]="wu";
	b[6]="liu";
	b[7]="qi";
	b[8]="ba";
	b[9]="jiu";
	//count的作用是标记一共有多少位数需要表达 
	int count=0;
	int shuzi;
	i=0;
	//q数组的作用是存储要输出的各个数的拼音们 ,注意此时是从个位数的拼音往前存储的 
    string q[100];
    while(n!=0){
		j=n%10;
		shuzi=j;
		q[i]=b[shuzi];
		i++; 
    	n=n/10;
    	count++;
	}
    //从第一位的拼音开始输出,一直到倒数第二位 
	for(i=count-1;i>0;i--){
		cout<<q[i]<<" "; 
	}
	//输出最后一位,没有空格 
    cout<<q[0];
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41945920/article/details/79967105
今日推荐