hdu 杭电 1013 老是WA,求大牛指教~~
#include<iostream>usingnamespacestd;#include<string>intdeal(intb){intsum=0;inti;for(i...
#include <iostream>
using namespace std;
#include <string>
int deal(int b)
{
int sum = 0;
int i;
for (i = 10 ;i <= b * 10; i*=10)
{
sum += (b % i) / (i / 10);
}
return sum;
}
int work(int a)
{
if (a < 10)
{return a;}
else
{
return work(deal(a));
}
}
int main()
{
int ii;
while (cin>>ii)
{
if (ii == 0) continue;
cout<<work(ii)<<endl;
}
return 0;
} 展开
using namespace std;
#include <string>
int deal(int b)
{
int sum = 0;
int i;
for (i = 10 ;i <= b * 10; i*=10)
{
sum += (b % i) / (i / 10);
}
return sum;
}
int work(int a)
{
if (a < 10)
{return a;}
else
{
return work(deal(a));
}
}
int main()
{
int ii;
while (cin>>ii)
{
if (ii == 0) continue;
cout<<work(ii)<<endl;
}
return 0;
} 展开
1个回答
展开全部
要注意这题没说输入数据的范围在int范围内,所以最好用字符串输入,就可以AC了
#include <iostream>
using namespace std;
#include <string>
int deal(int b)
{
int sum = 0;
int i;
for (i = 10 ;i <= b * 10; i*=10)
{
sum += (b % i) / (i / 10);
}
return sum;
}
int work(int a)
{
if (a < 10)
{return a;}
else
{
return work(deal(a));
}
}
int solve(char *s) {
int sum = 0;
for(int i = 0; s[i]; i ++) {
sum += s[i] - '0';
}
return work(sum);
}
int main()
{
char ii[5000];
while (cin>>ii)
{
if (strcmp(ii, "0") == 0) continue;
cout<<solve(ii)<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
#include <string>
int deal(int b)
{
int sum = 0;
int i;
for (i = 10 ;i <= b * 10; i*=10)
{
sum += (b % i) / (i / 10);
}
return sum;
}
int work(int a)
{
if (a < 10)
{return a;}
else
{
return work(deal(a));
}
}
int solve(char *s) {
int sum = 0;
for(int i = 0; s[i]; i ++) {
sum += s[i] - '0';
}
return work(sum);
}
int main()
{
char ii[5000];
while (cin>>ii)
{
if (strcmp(ii, "0") == 0) continue;
cout<<solve(ii)<<endl;
}
return 0;
}
追问
谢了~~大牛~~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询