POJ 1016 Numbers That Count

模拟题,,,,注意 15次操作限定要精准不要多一次也不要少一次。  找循环的时候从最近的找

代码比较挫。。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
char s[88], ts[105], nt[105], cpy[17][105];
int vis[10], a[105];
int len, l1;
void deal()
{
    int i, j;
    for(i=0; i<len; i++)
    {
        vis[ts[i]-'0']++;
    }
    for(i=0; i<10; i++)
    {
        if(vis[i]!=0)
        {
            j=0;
            while(vis[i]!=0)
            {
                a[j++]=vis[i]%10;
                vis[i]/=10;
            }
            for(--j; j>=0; j--)
                nt[l1++]=a[j]+'0';
            nt[l1++]=i+'0';
        }
    }
    nt[l1]='\0';
}
int main()
{
    int ans, op, k, i;
    while(scanf("%s",s)!=EOF)
    {
        if(strcmp(s,"-1")==0) break;
        strcpy(ts,s);
        ans=0;
        op=-1;
        strcpy(cpy[0],s);
        while(ans<15)
        {
            len=strlen(ts);
            l1=0;
            memset(vis,0,sizeof(vis));
            deal();
            if(strcmp(nt,ts)==0)
            {
                op=0;
                break;
            }
            for(i=ans-1; i>=0; i--)
            {
                if(strcmp(cpy[i],nt)==0)
                {
                    op=1;
                    k=ans-i+1;
                    break;
                }
            }
            if(op==1)
                break;
            ans++;
            strcpy(cpy[ans],nt);
            strcpy(ts,nt);
        }
        if(op==0)
        {
            if(ans==0) printf("%s is self-inventorying\n",s);
            else printf("%s is self-inventorying after %d steps\n",s,ans);
        }
        else if(op==1)
            printf("%s enters an inventory loop of length %d\n",s,k);
        else printf("%s can not be classified after 15 iterations\n",s);
    }
    return 0;
}




posted @ 2013-08-18 16:34  Ink_syk  阅读(95)  评论(0编辑  收藏  举报