My Weblog

Blog about programming and math

SPOJ Power Crisis

Power Crisis is from UVA online judge [ problem 151 ] . I just submitted my code which i wrote for uva problem 151 and accepted . This code is just simulation of process . I remember whole night of writing this code and this code was probably my fourth or fifth accepted problem on UVA so it was kind of nostalgia to me and big thanks to problem submitter . After looking at the this code , i can clearly see my transformation in 5 years of coding ;).

#include<stdio.h>
	
	main()
	 {

		int i,j,n,a[100],count,k,l;
		while(scanf("%d",&n)==1)
		  {
			a[0]=1;
			//a[n-1]=13;
			for(j=1;j<=n-2;j++)
			  a[j]=0;

			for(i=1;;i++)
			  {
				//printf("\ni=%d \n",i);
				//count=1;
				  for(j=1;j<=n-1;j++)
					{
						//if(count!=13 && j!=n-1)
						  //{
							a[j]=a[j-1];
						   for(l=1;l<=i;l++)
						     {
							a[j]=(a[j]+1)%n;
							//printf("a[%d]=%d ",j,a[j]);
							//if(a[j]==13 && j!=n-1)
							  // break;
							if(a[j]==0)
							   a[j]=n;
							for(k=0;k<j;k++)
							  {
								if(a[k]==a[j])
								  {
									k=-1;
									a[j]=(a[j]+1)%n;
									 //l++;
									if(a[j]==0)
									 a[j]=n;
									if(a[j]==13 && l==i)
									   break;
								   }
							    }
							 }

								// printf("a[%d]=%d ",j,a[j]);
								 if(a[j]==13)
								   break;
							//	if(count<13)
								  // count++;
						      //}
						//if(a[j]==13)
						//	break;
					    }
				      if(a[j]==13 && j==n-1)
						{
							printf("%d\n",i);
							break;
						}
			          }
			 }
	}		






Advertisements

May 21, 2011 - Posted by | Programming

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: