5 Replies - 469 Views - Last Post: 19 July 2009 - 06:47 PM Rate Topic: -----

#1 anghuiwen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-July 09

Is there a way to shorten this code

Posted 18 July 2009 - 06:26 AM

Hey thanks to drx1 for helping me just now!
I've got another problem again(sorry for asking so many questions i'm just like a 16 year old learning programming)
int main (void)
{
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
	for(a=1;a<27;a++)
	for(b=1;b<27;b++)
	for(c=1;c<27;c++)
	for(d=1;d<27;d++)
	for(e=1;e<27;e++)
	for(f=1;f<27;f++)
	for(g=1;g<27;g++)
	for(h=1;h<27;h++)
	for(i=1;i<27;i++)
	for(j=1;j<27;j++)
	for(k=1;k<27;k++)
	for(l=1;l<27;l++)
	for(m=1;m<27;m++)
	for(n=1;n<27;n++)
	for(o=1;o<27;o++)
	for(p=1;p<27;p++)
	for(q=1;q<27;a++)
	for(r=1;r<27;r++)
	for(s=1;s<27;s++)
	for(t=1;t<27;t++)
	for(u=1;u<27;u++)
	for(v=1;v<27;v++)
	for(w=1;w<27;w++)
	for(x=1;x<27;x++)
	for(y=1;y<27;y++)
	for(z=1;z<27;z++)
	if(d==(a+c))
	if(y==(a+s))
	if(b==(i+v))
	if(f==(h+z))
	if(l==(g+s))
	if(i==(r+o))
	if(k==(j+m))
	if(w==(u+s))
	if((m*m)==u)
	if((v*t)==n)
	if((x*p)==w)
	if((o*z)==q)
	if((g*p)==h)
	if((e*p)==q)
		printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z);
		getch();
		return 0;
}


This program, I know, has super long runtime and cannot be processed by my computer! This is sort of like a puzzle. Anyone have any other method to solve this question? Cos i'm like bruteforcing it bit by bit out and it's impossible.

Thanks a lot for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Is there a way to shorten this code

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Is there a way to shorten this code

Posted 18 July 2009 - 08:15 AM

Could you explain what its supposed to do? Without knowing what you're trying to achieve there's no way that anybody can give you a meaningful answer
Was This Post Helpful? 0
  • +
  • -

#3 quicksilverprince  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 19-May 09

Re: Is there a way to shorten this code

Posted 18 July 2009 - 12:49 PM

I see a couple problems with your code. First of all, instead of multiple ints (a b c d e f g) sometimes its easier to use an array, have you learned those yet? Also using a for loop when you have a value declared "for (int a = 0 <-" this creates a new variable, so try either a for loop with a while loop inside of it (the for loop accesses the array number, the while loop increments) or make a recursive function. Also why don't you just initialise each variable instead of making it work its way up? They will end up at the same value.

p.s. don't ever say I'm too young, that only holds you back!
Was This Post Helpful? 0
  • +
  • -

#4 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: Is there a way to shorten this code

Posted 19 July 2009 - 12:57 AM

How about conditional reduction?
	if(d==(a+c))		  d=a+c
	if(y==(a+s))		  y=a+s
	if(b==(i+v))		   b=i+v
	if(f==(h+z))		   f=h+z
	if(l==(g+s))		   l=g+s
	if(i==(r+o))		   i=r+o
	if(k==(j+m))		 k=j+m
	if(w==(u+s))		 w=u+s
	if((m*m)==u)	   mm=u
	if((v*t)==n)		  vt=n
	if((x*p)==w)		 xp=w
	if((o*z)==q)		  oz=q
	if((g*p)==h)		  gp=h
	if((e*p)==q)		  ep=q


If you're trying to equate both sides of an expression to get the condition, then equate them and combine expressions.

	 d = a + c	   y - s = a		   d = (y-s)+c
etc.
 


Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2239
  • View blog
  • Posts: 9,409
  • Joined: 29-May 08

Re: Is there a way to shorten this code

Posted 19 July 2009 - 09:25 AM

The inner code is being called 26^26 times! Examine what you're trying to do, has there is probably an easier way.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,485
  • Joined: 19-February 09

Re: Is there a way to shorten this code

Posted 19 July 2009 - 06:47 PM

	for(q=1;q<27;a++)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1