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 yellowflying at 2006-07-28 00:58:23 on Problem 1026
#include <stdio.h>
#include <string.h>
int euclid(int p,int q)
{
	int tmp;
	while(q!=0)
	{
		tmp=p%q;
		p=q;
		q=tmp;
	}
	return p;
}
int main()
{
	int n,k,i,j,mi=1,len,t;
	char *p,*q,*temp;
	scanf("%d",&n);
	while(n!=0)
	{
		int *N=new int [n];
		int *M=new int [n];
		char *a=new char [n+1];
		char *b=new char [n+1];
		for(i=0;i<n;i++)
		{
			scanf("%d",&N[i]);
			M[i]=N[i];
		}
		for(i=0;i<n;i++)
		{
			len=0;
			while(M[i]!=0)
			{
				len++;
				t=M[i];
				M[i]=0;
				i=t-1;
			}
			if(len!=0&&len!=1&&mi%len!=0)
				mi=(mi*len)/euclid(mi,len);
		}
		printf("%d\n",mi);
		scanf("%d",&k);
		while(k!=0)
		{
			getchar();
			gets(a);
			for(i=strlen(a);i<n;i++)
				a[i]=' ';
			a[n]='\0';
			b[n]='\0';
			p=a;
			q=b;
			k=k%mi;
			for(i=0;i<k;i++)
			{
				for(j=0;j<n;j++)
					q[N[j]-1]=p[j];
				temp=p;
				p=q;
				q=temp;
			}
			printf("%s\n",p);
			scanf("%d",&k);
		}
		printf("\n");
		scanf("%d",&n);
	}
	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