I'm desperate and i don't know if it's even worth it to post:crying::crying:
Consider the following game: There are 100 doors (numbered 1 through 100) and 100 students (numbered 1 through 100) waiting in a line. Initially, all the doors are closed. A student numbered x passes through all the doors that are multiples of x (i.e., student numbered x goes to doors x,2x,3x, ... ) and changes the status of each door, i.e., opens the door if it is closed and closes the door if it is open. After all the students complete their turns, which doors remain open? Write a C program that mimics this experiment and prints the numbers of the doors that remain open at the end.
5 replies to this topic
#1
Posted 07 October 2010 - 08:56 PM
|
|
|
#2
Posted 08 October 2010 - 01:57 AM
First, do not ask others to give you code. Rather ask for help how to solve a problem, and then write code by yourself. Only if you can't clear code of bugs then ask for coding help. And even then post your solution that shows your effort before asking others to code for you.
To solve this problem you have to break it down to arithmetic relations. Door number D is opened/closed by all students S such that S divides D without remainder. Now, if you count values S (1 <= S <= D), say that you get number of divisors K, then if K is even - door remain shut; otherwise, if K is odd, door will be open.
So the solution goes like this.
1. Iterate through all doors 1 <= D <= 100.
2. For each door iterate through all values 1 <= S <= D; if S divides D, increment K by one.
3. After all S values have been evaluated, if K is odd print door number D because it remains open.
To solve this problem you have to break it down to arithmetic relations. Door number D is opened/closed by all students S such that S divides D without remainder. Now, if you count values S (1 <= S <= D), say that you get number of divisors K, then if K is even - door remain shut; otherwise, if K is odd, door will be open.
So the solution goes like this.
1. Iterate through all doors 1 <= D <= 100.
2. For each door iterate through all values 1 <= S <= D; if S divides D, increment K by one.
3. After all S values have been evaluated, if K is odd print door number D because it remains open.
#3
Posted 08 October 2010 - 03:01 AM
You'll need a nested for loop.
#4
Posted 08 October 2010 - 08:27 AM
Sorry this is embarrassing but i forgot to post the link to what i have done:
2shared - download problem4-6.c
2shared - download problem4-6.c
zoranh said:
First, do not ask others to give you code. Rather ask for help how to solve a problem, and then write code by yourself. Only if you can't clear code of bugs then ask for coding help. And even then post your solution that shows your effort before asking others to code for you.
To solve this problem you have to break it down to arithmetic relations. Door number D is opened/closed by all students S such that S divides D without remainder. Now, if you count values S (1 <= S <= D), say that you get number of divisors K, then if K is even - door remain shut; otherwise, if K is odd, door will be open.
So the solution goes like this.
1. Iterate through all doors 1 <= D <= 100.
2. For each door iterate through all values 1 <= S <= D; if S divides D, increment K by one.
3. After all S values have been evaluated, if K is odd print door number D because it remains open.
To solve this problem you have to break it down to arithmetic relations. Door number D is opened/closed by all students S such that S divides D without remainder. Now, if you count values S (1 <= S <= D), say that you get number of divisors K, then if K is even - door remain shut; otherwise, if K is odd, door will be open.
So the solution goes like this.
1. Iterate through all doors 1 <= D <= 100.
2. For each door iterate through all values 1 <= S <= D; if S divides D, increment K by one.
3. After all S values have been evaluated, if K is odd print door number D because it remains open.
#5
Posted 08 October 2010 - 11:23 AM
Either I'm dumb, or the linked site doesn't work. You can just post the text of your code here.
#6
Posted 09 October 2010 - 09:17 AM
Big hint: try using a for loop instead of a while loop when opening/closing doors.
Edited by dargueta, 09 October 2010 - 09:28 AM.
Copy-paste error
sudo rm -rf /
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









