Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

出现超时现象,可以帮忙解决一下吗

Posted by zycs at 2019-08-11 12:07:02 on Problem 1016
#include <iostream>
#include <stdio.h>

using namespace std;


string s[16];
char obj[80];

string g(string obj){
	int num[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	for (int i = 0; i < obj.length(); i++){
		num[(int)(obj[i] - '0')]++;
	}
	string temp = "";
	for (int i = 0; i < 10; i++){
		if (num[i] != 0){
			char c[10];
			sprintf(c, "%d", num[i]);
			temp += c;
			sprintf(c, "%d", i);
			temp += c;
		}
	}
	return temp;
}

void f(int index){
	if (index == 16){
		
		for (int i = 0; i < index; i++){
			for (int j = i + 1; j < index; j++){
				if (s[i] == s[j]){
					printf("%s", s[0].c_str());
					printf(" enters an inventory loop of length ");
					printf("%d\n", j - i);
					//cout<<s[0]<<" enters an inventory loop of length "<<j - i<<endl;
					return;
				}
			}
			
		}
		printf("%s", s[0].c_str());
		printf(" can not be classified after 15 iterations\n");
		//cout<<s[0]<<" can not be classified after 15 iterations"<<endl;
		return;
	}
	s[index] = g(s[index - 1]);
	//cout<<s[index]<<endl;
	if (s[index] == s[index - 1]){
		if (index == 1){
			printf("%s", s[0].c_str());
			printf(" is self-inventorying\n");
			//cout<<s[0]<<" is self-inventorying"<<endl;
		}
		else {
			printf("%s", s[0].c_str());
			printf(" is self-inventorying after ");
			printf("%d", index - 1);
			printf(" steps\n");
			//cout<<s[0]<<" is self-inventorying after "<<index - 1<<" steps"<<endl; 
		}
		return;
	}	
	f(index + 1);
}

int main(void){
	
	while (true){
		
		scanf("%s", obj);
		s[0] = obj;
		if (s[0] == "-1") break;
		//f(1);
		printf("%s", obj);
		for (int index = 1; index <= 16; index++){
			if (index == 16){
				for (int i = 0; i < index; i++){
					for (int j = i + 1; j < index; j++){
						if (s[i] == s[j]){
							
							printf(" enters an inventory loop of length ");
							printf("%d\n", j - i);
							//cout<<s[0]<<" enters an inventory loop of length "<<j - i<<endl;
							goto a;
						}
					}
					
				}
				
				printf(" can not be classified after 15 iterations\n");
				//cout<<s[0]<<" can not be classified after 15 iterations"<<endl;
				goto a;
			}
			s[index] = g(s[index - 1]);
			//cout<<s[index]<<endl;
			if (s[index] == s[index - 1]){
				if (index == 1){
					
					printf(" is self-inventorying\n");
					//cout<<s[0]<<" is self-inventorying"<<endl;
				}
				else {
					
					printf(" is self-inventorying after ");
					printf("%d", index - 1);
					printf(" steps\n");
					//cout<<s[0]<<" is self-inventorying after "<<index - 1<<" steps"<<endl; 
				}
				goto a;
			}
		}	
		a:;
	}
	
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator