@PAT 题库1005 Spell It Right (20 分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
12345
Sample Output:
one five
先附上我最开始写的用数组存放的代码:
(注:测试部分正确)
#include <stdio.h>
#include <string.h>
char s[1000];
char he[1000];
int a[1000];
int sum=0;
int main(){
int i,n,m,l;
scanf("%d",&n);
sprintf(he,"%d",n);
l=strlen(he);
for(i=0;i<l;i++){sum+=((he[i])-48); }
sprintf(s,"%d",sum);
m=strlen(s);
for(i=0;i<m;i++){
if(i<m-1){
if(s[i]=='0') printf("zero ");
if(s[i]=='1') printf("one ");
if(s[i]=='2') printf("two ");
if(s[i]=='3') printf("three ");
if(s[i]=='4') printf("four ");
if(s[i]=='5') printf("five ");
if(s[i]=='6') printf("six ");
if(s[i]=='7') printf("seven ");
if(s[i]=='8') printf("eight ");
if(s[i]=='9') printf("nine ");
}
else {
if(s[i]=='0') printf("zero");
if(s[i]=='1') printf("one");
if(s[i]=='2') printf("two");
if(s[i]=='3') printf("three");
if(s[i]=='4') printf("four");
if(s[i]=='5') printf("five");
if(s[i]=='6') printf("six");
if(s[i]=='7') printf("seven");
if(s[i]=='8') printf("eight");
if(s[i]=='9') printf("nine");
}
}
}
这个代码的主要思想就是把数字存在在数组a中,在赋值到字符串中方便计算长度,之后再此基础上算出和,但是有个问题就是题目中的数字范围是很大的,用数组是不够的。所以这个代码在pat官网上的测试点4和7是通不过的。得分15
分析:
这是我在牛客上看到的解析
n有10100那么大。显然用int, long都是不够的,java当然可以用自带的BigInteger,对于C/C++,我们可以用字符串读入这些数。求各个数字之和也很简单,直接加即可——对于C/C++,字符c '0’就得到了对应的整数。这个和显然不会太大——不超过900,因为最大就是100个9。然后我们把这个和的各个数字截出来,输出即可。截取各个数字,可以像1001那样用除法,不断除以10即可。更简单的方法是使用sprintf,再把结果写回到字符串,再一位一位输出。关于输出,要注意可以提前把0-9的英文都存下来——存一个字符串数组,再输出就行了。还要注意输出第一个数字之前没有空格。
然后我又找了别人的代码,发现!!
我为什么不直接把他存为字符串char!!!!
这样问题就解决了
使用char存数据的正确代码:
#include <stdio.h>
#include <string.h>
char s[1000];
char he[1000];
int sum=0;
int main(){
int i,n,m,l;
scanf("%s",he);
l=strlen(he);
for(i=0;i<l;i++){sum+=((he[i])-48); }
sprintf(s,"%d",sum);
m=strlen(s);
for(i=0;i<m;i++){
if(i<m-1){
if(s[i]=='0') printf("zero ");
if(s[i]=='1') printf("one ");
if(s[i]=='2') printf("two ");
if(s[i]=='3') printf("three ");
if(s[i]=='4') printf("four ");
if(s[i]=='5') printf("five ");
if(s[i]=='6') printf("six ");
if(s[i]=='7') printf("seven ");
if(s[i]=='8') printf("eight ");
if(s[i]=='9') printf("nine ");
}
else {
if(s[i]=='0') printf("zero");
if(s[i]=='1') printf("one");
if(s[i]=='2') printf("two");
if(s[i]=='3') printf("three");
if(s[i]=='4') printf("four");
if(s[i]=='5') printf("five");
if(s[i]=='6') printf("six");
if(s[i]=='7') printf("seven");
if(s[i]=='8') printf("eight");
if(s[i]=='9') printf("nine");
}
}
}
更加简短的代码连接:https://blog.youkuaiyun.com/sup_heaven/article/details/8451663
代码如下:
#include<stdio.h>
int main(){
char str[110];
char dtoe[10][20]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int i,sum=0;
scanf("%s",str);
for(i=0;str[i]!='\0';i++){
sum=sum+str[i]-'0';
}
if(sum<10){
printf("%s\n",dtoe[sum]);
}
else if(sum<100){
printf("%s ",dtoe[sum/10]);
printf("%s\n",dtoe[sum%10]);
}
else if(sum<1000){
printf("%s ",dtoe[sum/100]);
printf("%s ",dtoe[sum%10/10]);
printf("%s\n",dtoe[sum%10]);
}
return 0;
}
以及在牛客网上看到的是使用指针的形式,代码之后更新