Quantum Core

Forum for RPG Maker enthusiasts
Home­QC Website­Portal­FAQ­Search­Register­Memberlist­Usergroups­Log in
Post new topic   Reply to topicShare | 
 

 A permutation problem

View previous topic View next topic Go down 
AuthorMessage
Fantasist
Admin


Posts: 51
Join date: 2008-01-12

PostSubject: A permutation problem   Tue Jan 22, 2008 7:55 pm

I had an assignment in C and I still haven't cracked a certain problem. I had to generate all combinations of 3-digit numbers from 1, 2 and 3, non-repeating. If it was repeating, it's simple. It's something like this:

Code:
for(i=1; i<=3; i++){
  for(j=1; j<=3; j++){
    for(k=1; k<=3; k++){
      printf("\n%d%d%d", i, j, k);
    }
  }
}


But my teach didn't approve. she said that I had to use 'swapping of variables' in the loop like this:
Code:
for(i=1; i<=3; i++){
  for(j=1; j<=3; j++){
    for(k=1; k<=3; k++){
      THE_MYSTERY_CODE
    }
  }
}


So I need the mystery code, which is supposed to swap vars. Any ideas?
Back to top Go down
View user profile http://quantumcore.forumotion.com
Nortos



Posts: 32
Join date: 2008-01-22

PostSubject: Re: A permutation problem   Wed Jan 23, 2008 5:13 am

O.o I have never used C in my life...can't help you sorry
Back to top Go down
View user profile
Fantasist
Admin


Posts: 51
Join date: 2008-01-12

PostSubject: Re: A permutation problem   Wed Jan 23, 2008 7:00 pm

It's not about C, it's about logic. Here, I'll put it in a different way:

Question: You have to generate all 3-digit numbers possible using 1, 2 and 3, without repeating anyone. So 123, 231, 132... are allowed, but 112, 222, 331 aren't allowed. So technically, there are 6 possible combinations:

123
132
213
231
312
321

All I did was put the first number constant and changed the other two numbers, I did that for 1, 2 and 3. So there are 6 possible combinations. Now, we have to use RGSS to generate, say print those 6 numbers using only loops and swapping.

Here's what swapping does:
Spoiler:
 


So here's the template, in a manner of speaking. It's the loop function in RGSS, I think you know it:

Code:
for i in 1 to 3
  for j in 1 to 3
    for k in 1 to 3
      <mystery code which involves swapping of variables, but don't know WHICH variables>
    end
  end
end


So. I need that code inside the three loops.
Back to top Go down
View user profile http://quantumcore.forumotion.com
Nortos



Posts: 32
Join date: 2008-01-22

PostSubject: Re: A permutation problem   Sun Jan 27, 2008 5:24 pm

I'm not too sure stumped but anyway here's a guess...

Code:

for i in 1 to 3
  print i
end
for j in 1 to 3
swap
print j
end
for k in 1 to 3
swap_1
print k
end

def swap(1, 2, 3)
  temp = 1
  1 = 2
  2 = 3
  3 = 1
end

def swap_1(1, 2, 3)
  temp = 1
  1 = 3
  2 = 1
  3 = 2
end


probably wrong...
Back to top Go down
View user profile
Fantasist
Admin


Posts: 51
Join date: 2008-01-12

PostSubject: Re: A permutation problem   Sun Jan 27, 2008 5:42 pm

Hm, I don't get you, what does swap and swap_1 do exactly? both of them have 3 parameters. And do you mean to put the loops in one another? Even then, I don't think this would work.

I asked my teach about it and she said just one other person figured it out (GAH! And I was supposed to be best in C classes till then Sad ). I should go and ask him. But mind you, that guy has a very high IQ. In the beginning of college, there werenn't any classes, teachers just introduced themselves and soe of them gave u puzzles to solve. He solved all of them without a problem and I had a tough time cracking two of them.
Back to top Go down
View user profile http://quantumcore.forumotion.com
Nortos



Posts: 32
Join date: 2008-01-22

PostSubject: Re: A permutation problem   Mon Jan 28, 2008 4:03 am

Razz I'm not going to uni for another 3 yrs only just 16, anyway I was too tired it was 1am to figure out how do properly
Back to top Go down
View user profile
Fantasist
Admin


Posts: 51
Join date: 2008-01-12

PostSubject: Re: A permutation problem   Mon Jan 28, 2008 7:47 pm

lol! nvm then Very Happy
Back to top Go down
View user profile http://quantumcore.forumotion.com
 

A permutation problem

View previous topic View next topic Back to top 
Page 1 of 1

Permissions of this forum:You can reply to topics in this forum
Quantum Core :: Computer Discussion :: Computer Programming-
Post new topic   Reply to topic