主要就是想说一个事,就是以后看数据 的 时候一定要小心 。本题的题目 给的是 non-negative integer N ,非负整数N。那我们做题时就一定要考虑:
1.在测试数据上,0 需不需要特判
2.在我们写的代码中,有没有测试数据是0 而导致程序崩了的情况。
就比如说,我代码里的利用 sum!=0 进行循环,若N=0,那sum=0,会导致出错,,,
<本题应该是第二个测试点中 N=0 >
ac代码
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
string ss;
cin>>ss;
//cout<<"ss:"<<ss<<endl;
long long sum=0;
stack<string>ans;
string word[10]={"zero","one","two","three","four","five",
"six","seven","eight","nine"};
for(int i=0;i<ss.length();i++)
{
int tt=ss[i]-'0';
sum+=tt;
}
if(sum==0) // 所以这里加了一个特判,,,
{
cout<<"zero"<<endl;
}
else
{
while(sum!=0)
{
int tt=sum%10;
ans.push(word[tt]);
sum=sum/10;
}
cout<<ans.top();
ans.pop();
while(!ans.empty())
{
cout<<" "<<ans.top();
ans.pop();
}
cout<<endl;
}
return 0;
}