本文主要是介绍pat乙级 1002写出这个数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天是做题的第一天,写个博客记录自己的做题历程(小白做题,勿喷)
思路很简单,就是算出sum后,通过pow算出最大位数,然后倒过来依次递减通过取余作除,将sum各个位上的数存进数组,最后输出
#include<iostream>
#include<cmath>
using namespace std;
int main(){string arr[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};//simplify codechar ch;int sum=0,i=0,num=0,s[4];while((ch=getchar())!='\n'){// Note the difference between \ 0 and \ nsum+=(ch-'0');//find the sum;}while(sum>=pow(10,i)){//though the power function to find the maximun number of digitsi++;}int count=i-1;//count represents the lagrest multiple of ten. i represents the maxinum number of digits.for(;count>=0;count--){num=pow(10,count);s[count]=sum/num;sum=sum%num;}for(count=i-1;count>=0;count--){if(count<i-1)//this is a very clever waycout<<" ";cout<<arr[s[count]];}return 0;
}
从大佬那学来很多简化的代码
1.string arr[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};//省去了用switch的情况
2. while((ch=getchar())!='\n'){
sum+=(ch-'0');//将巨大的整数化为字符简单计算
}
3.if(count<i-1)
cout<<" ";
cout<<arr[s[count]];//将空格在数字前输入,只要第一个不输入空格,就可以完美满足题目要求
这篇关于pat乙级 1002写出这个数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!