一、用gets出错的

编译条件:C++(clang++11)

#include<bits/stdc++.h>
using namespace std;

static const int maxn=1e5+5;

char solve[maxn];

int main()
{
    long long num;
    while( nullptr!=gets(solve) )
    {

        int len=-1;
        len=strlen(solve);
        if( 1==len && solve[0]=='0' )
        {
            break;
        }

        while( 1 )
        {
            num=0;
            len=strlen(solve);
            if( 1==len )
            {
                printf("%d\n",solve[0]-'0');
                break;
            }
            else
            {
                for(int i=0; i<len; ++i)
                {
                    num+=( solve[i]-'0' );
                }
                sprintf(solve,"%lld",num);
            }
        }


    }
    return 0;
 } 
编译错误
收起
编译错误:您提交的代码无法完成编译
a.cpp:11:18: error: use of undeclared identifier 'gets'
while( nullptr!=gets(solve) )
^
1 error generated.

二、方法一——用scanf解决

编译条件:C++(clang++11)

#include<bits/stdc++.h>
using namespace std;

static const int maxn=1e5+5;

char solve[maxn];

int main()
{
    long long num;
    while( ~scanf("%s",solve) )
    {

        int len=-1;
        len=strlen(solve);
        if( 1==len && solve[0]=='0' )
        {
            break;
        }

        while( 1 )
        {
            num=0;
            len=strlen(solve);
            if( 1==len )
            {
                printf("%d\n",solve[0]-'0');
                break;
            }
            else
            {
                for(int i=0; i<len; ++i)
                {
                    num+=( solve[i]-'0' );
                }
                sprintf(solve,"%lld",num);
            }
        }


    }
    return 0;
 }