pku 1016

Source Code

Problem: 1016 User: cug_fish2008
Memory: 264K Time: 16MS
Language: C++ Result: Accepted
  • Source Code
    #include <iostream>
    using namespace std;
    void change(char c[],char p[]);
    bool cpre(char x[],char y[]);
    int main()
    {
    	char ch[100];
    	while(cin>>ch && ch[0]!='-'){
    		for(int i=0;ch[i]!=0;i++)
    			cout<<ch[i];
    		char y[15][100];
    		change(ch,y[0]);
    		if(cpre(ch,y[0])){
    			cout<<" is self-inventorying"<<endl;
    			continue;}
    		int i;
    		int u=1;
    		strcpy(y[1],y[0]);
    		strcpy(y[0],ch);
    		for(i=2;i<=15;i++){
    			char self[100];
    			change(y[u],self);
    			if(cpre(y[u],self)){
    				cout<<" is self-inventorying after "
    					<<i-1<<" steps"<<endl;
    				break;}
    			bool b=false;
    			for(int j=0;j<u;j++){
    				if(cpre(y[j],self)){
    					cout<<" enters an inventory loop of length "
    						<<i-j<<endl;
    					b=true;}}
    			if(b)break;
    			strcpy(y[++u],self);
    		}
    		if(i>15)cout<<" can not be classified after 15 iterations"<<endl;
    	}
    	return 0;
    }
    void change(char c[],char p[])
    {
    	int x[10]={0};
    	for(int i=0;c[i]!=0;i++)
    		x[c[i]-'0']++;
    	int u=-1;
    	for(int i=0;i<10;i++){
    		if(x[i]!=0){
    			int t=x[i];
    			while(t){
    				p[++u]=t%10+'0';
    				t=int(t*0.1);}
    			p[++u]=i+'0';
    		}
    	}
    	p[++u]=0;
    }
    bool cpre(char x[],char y[])
    {
    	int i;
    	bool b=false;
    	for(i=0;x[i]!=0 || y[i]!=0;i++){
    		if(x[i]!=y[i]){b=true;break;}}
    	if(b)return false;
    	return true;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值