#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<string.h>
#include<math.h>
#define inf 0x3f3f3f
using namespace std;
__int64 res;
__int64 n;
char a[10000];
int main()
{
while(scanf("%s",a) && a[0]!= '0')
{
n = 0;
for(int i = 0 ; i < strlen(a) ; i++)
n+= a[i]-'0';
while(n >= 10)
{
res = 0;
for(int i = 0 ; n != 0 ;i++)
{
res += n%10;
n = n/10;
}
n = res;
}
printf("%I64d\n",n);
}
return 0;
}
这里最开始直接输入N,错了,输入的数据应该很大,用字符串输入.
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<string.h>
#include<math.h>
#define inf 0x3f3f3f
using namespace std;
__int64 res;
__int64 n;
char a[10000];
int main()
{
while(scanf("%s",a) && a[0]!= '0')
{
n = 0;
for(int i = 0 ; i < strlen(a) ; i++)
n+= a[i]-'0';
while(n >= 10)
{
res = 0;
for(int i = 0 ; n != 0 ;i++)
{
res += n%10;
n = n/10;
}
n = res;
}
printf("%I64d\n",n);
}
return 0;
}
这里最开始直接输入N,错了,输入的数据应该很大,用字符串输入.