I. Reversion Count

I. Reversion Count
Description:


There is a positive integer X, X's reversion count is Y. For example, X=123, Y=321; X=1234, Y=4321. Z=(X-Y)/9, Judge if Z is made up of only one number(0,1,2...9), don't consider '+'and '-'.


Input:


Input contains of several test cases. Each test case only contains of a number X, L is the length of X. ( 2 <= L < 100)


Output:


Output “YES”or “NO”.


样例输入


10
13
样例输出


YES

YES

做了三个小时好不容易出来结果了,他告诉我超时……

#include <stdio.h>
#include <stdlib.h>
char a[101],b[101];

int isonly(int c){
   int a=c%10;
    while(c){
        int c2=c%10;
        if(c2!=a)
            return 0;
        c/=10;
    }
    return 1;
}

int main(void)
{
    int len,i,j;
    int c;
    for(;;){
        len=0;

        for(i=0;;i++){
            scanf("%c",&a[i]);
            if(a[i]=='\n')
                break;
            len++;
        }
        for(j=len,i=0;j>=0;j--,i++){
            b[j]=a[i];
        }

       c=(atoi(a)-atoi(b))/9;
       if(c<0)
           c=-c;
       if(c==0)
            printf("YES\n");
        else if(isonly(c))
            printf("YES\n");
        else
            printf("NO\n");
    }


    return 0;
}


下边这个是一个大佬的程序,c++
#include<iostream>
#include<algorithm>
using namespace std;

string x;
int z[100],y[100];
int len;

void turn()
{
    len=0;
    for(int i=x.size()-1;i>=0;--i)
    {
        y[i]=x[i]-'0';
        z[len++]=x[i]-'0';
    }
}

bool com()
{
    for(int i=len-1;i>=0;--i)
        if(z[i]!=y[i])
        {
            if(z[i]<y[i])
            {
                swap(z,y);
                break;
            }
            else
                break;
        }

    for(int i=0;i<len;++i)
    {
        if(z[i]<y[i])
        {
            z[i+1]-=1;
            z[i]+=10;
            z[i]-=y[i];
        }
        else
            z[i]-=y[i];
    }

    --len;
    while(!z[len]&&len>0)
        --len;
    if(len==0)	return true;

    for(int i=len;i>=0;--i)
    {
        if(z[i]>=9||z[i]==0)
        {
            if(i!=0)	z[i-1]+=z[i]%9*10;
            z[i]/=9;
        }
        else
        {
            if(i!=0)	z[i-1]+=z[i]*10;
            z[i]=0;
        }
    }
    while(!z[len]&&len>0)
        --len;
    if(len==0)	return true;
    for(int i=1;i<=len;++i)
        if(z[i]!=z[i-1])	return false;
    return true;
}

int main()
{
    while(cin>>x)
    {
        turn();
        if(com())
            cout<<"YES\n";
        else
            cout<<"NO\n";
    }

    return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/han_hhh/article/details/80038498
个人分类: 个人编写
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭