Gremlin: Light Switches

Hello, I am having a problem completing this program I am writing.
There are 100 light switches all in the "on" position. I have to ask the user for the number of "gremlins" and their favorite number. Their favorite number is what is used to decide how the light switches are turned on or off.
If there is 1 gremlin and its favorite number is 2, then every other light switch is turned off leaving 50 light switches on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	const int GREMLIN = 100000;
	const int LIGHT = 100;
	int gremlin,
		 number[GREMLIN],
		 light;

	cout << " Number of Gremlins? ";
	cin >> gremlin;

	cout << " Gremlin favorite number?" << endl;
	for(int count = 0; count < gremlin; count++)
	{
		cin >> number[count];
	}

	cout << " Number of light switches still on: ";
	
	return 0;
}


I don't know what to do to figure out the number of light switches that will stay on, please help
Last edited on
After a bit of fiddling around i got this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main()
{
	const int GREMLIN = 100000;
	const int LIGHT = 100;
	int gremlin,
		 number[GREMLIN],
		 light = 0;

	cout << " Number of Gremlins? ";
	cin >> gremlin;

	cout << " Gremlin favorite number?" << endl;
	for(int count = 0; count < gremlin; count++)
	{
		cin >> number[count];
	}

	cout << " Number of light switches still on: ";
	for(int count = 0; count < gremlin; count++)
	{
		light += (LIGHT / number[count]);
	}
	cout << light << endl;

	return 0;
}


This works however, not completely.
I would work, if the numbers go in 100 evenly like

1 Gremlin
2 Favorite
50 is output(number of light switches)

2 Gremlins
2 Gremlin 1 favorite number
2 Gremlin 2 favorite number
100 is ouput

But if its
3 Gremlins
2 gremlin1 favorite #
4 gremlin 2 favorite #
5 gremlin 3 favorite #
65 light switches on is output

However, I'm getting 95 as the output
Last edited on
The issue is you have to be careful to not double count lights out.
Trying to come up with an algorithm to figure it out but still thinking about it??

But there is always the brute force method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include <iostream>

using namespace std;

int main() 
{
   const int GREMLIN(100000);
   const int LIGHT(100);
   bool      on[LIGHT];
   int       i(0),cnt(0);
   int       gremlin,
	     number[GREMLIN];

   cout << " Number of Gremlins? ";
   cin >> gremlin;
   cout << "Num grem: " << gremlin << endl;
   cout << " Gremlin favorite number?" << endl;
   for(int count(0); count < gremlin; count++) {
	cin >> number[count];
   }

   for (i=0; i < LIGHT; i++){
      on[i] = true;
   } 
   // Brute force
   for (i=0; i < gremlin; i++) {
     for ( int j(0); j < 100; j+=number[i]) {
        on[j] = false;
        cnt++;
     }
  }    
  int off(0);
  for (i=0; i < 100; i++){
     // Debug
     cout << on[i] << ",";
     if ( !((i+1)%10)){
        cout << endl;
     }
     // Count the number off 
     if ( !on[i] ) {
       off++;
     }
  } 
  cout << endl;
  cout << "Number of light switches still on: " << LIGHT-off << endl;
  cout << "Total sets: " << cnt << ", Off: " << off << ", On: " << LIGHT-off << endl;
  return 0;
}

Output:
./a.out
 Number of Gremlins? 3
Num grem: 3
 Gremlin favorite number?
2
3
5
0,1,0,0,0,0,0,1,0,0,
0,1,0,1,0,0,0,1,0,1,
0,0,0,1,0,0,0,0,0,1,
0,1,0,0,0,0,0,1,0,0,
0,1,0,1,0,0,0,1,0,1,
0,0,0,1,0,0,0,0,0,1,
0,1,0,0,0,0,0,1,0,0,
0,1,0,1,0,0,0,1,0,1,
0,0,0,1,0,0,0,0,0,1,
0,1,0,0,0,0,0,1,0,0,

Number of light switches still on: 26
Total sets: 104, Off: 74, On: 26

Notice the Total sets, that would be the number if you didn't account for the double counting.
Last edited on
Topic archived. No new replies allowed.