Drunken Jailer problem III


I figure out that the number of square number will be unlocked cells.

but online judge did not accept my result.

i have been reading the problem over and over but could not find

why this code not accepted.

anyone tried this problem please give me advice which i don't figure from

the problem


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
#include <iostream>
using namespace std;
int main (int argc, const char * argv[])
{
    int n; 
    int p;
    int num=1;
    int count =0;
    int result=0;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin>> p; 
        
        while( true){
            
            
            result = num * num;
            if( result > p) break;
            num++;
            count++;
            
        }
        cout << count <<endl;
    }
    
    
}





2
5
2
100
10















A certain prison contains a long hall of n cells,
each right next to each other. Each cell has a
prisoner in it, and each cell is locked.

One night, the jailer gets bored and decides to play
a game. For round 1 of the game, he takes a drink of
whiskey, and then runs down the hall unlocking each
cell. For round 2, he takes a drink of whiskey, and
then runs down the hall locking every other cell
(cells 2, 4, 6, ...). For round 3, he takes a drink
of whiskey, and then runs down the hall. He visits
every third cell (cells 3, 6, 9, ...). If the cell is
locked, he unlocks it; if it is unlocked, he locks it.
He repeats this for n rounds, takes a final drink, and passes out.

Some number of prisoners, possibly zero, realizes that
their cells are unlocked and the jailer is incapacitated. They immediately escape.

Given the number of cells, determine how many prisoners
escape jail.

Input

The first line of input contains a single positive integer.
This is the number of lines that follow. Each of the following
lines contains a single integer between 5 and 100, inclusive,
which is the number of cells n.

Output

For each line, you must print out the number of prisoners that
escape when the prison has n cells.

Sample Input

2
5
100
Sample Output

2
10

Try with input

1
2
3
2
100
5


Should return

1
2
10
2


Don't think it will.
Thank you i solved it

this is my first tried code

i solved second one but firstone showing similar problem

i don't know how to solve this code..... can you take look

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void fillPrison(vector<int> &vec, int n){
    
    for(int i=0 ; i <= n; i++){
        if( i==0) vec.push_back(0);
        else{vec.push_back(1);}
        
    }
    
}

void print(vector<int> &vec){
    for(size_t i=0 ; i <vec.size() ; i++){
        
        cout << vec[i];
        
    }
    cout <<endl;
    
    
}

int check(vector<int> vec, int n){
    
    for(int i=1; i < vec.size() ; i++){
        
        if( vec[i] == n) return n;
    }
    
    
}

void game(vector<int> &vec ,int n){
    
    int num=1;
    int count =1;
    for( int i=1 ; i < vec.size() ; i++){
        num = n;
        num = num * count; 
        
        if( num < vec.size() ){
            
            for(int k=1 ; k < vec.size() ; k++){
                if( k == num) {
                    
                    //cout << k << " and " << vec[k] <<endl;
                    if( vec[k] == 1) vec[k] = 0;
                    else if ( vec[k] ==0 ) vec[k] =1;
                    
                }
                
            }
            count ++;
                 
        }
        
    }
    
}


void clean(vector<int> & vec){
    
    while(vec.size() != 0){
        
        vec.pop_back();
    }
}

int countzero(vector<int>& vec){
    
    
    //print(vec);
    int count =0; 
    for(int i=1 ; i <= vec.size(); i++){
        if( vec[i] == 0 ) {
            //cout << "here" <<endl;   
            count++;
        }
    }
    //cout <<"count:" <<count <<endl;
    return count;
}




int main (int argc, const char * argv[])
{
    
    int n;
    int cell;
    vector<int> prison;
    cin >> n;
   
    for( int i=0 ; i < n ; i++){
        //cout <<" cell " <<endl;
        cin >>  cell;
        //if( cell < 5 || cell >100) return 0;
        
        if( cell >0){
        fillPrison(prison,cell);
        for(int y=1 ; y <= prison.size(); y++){
            print(prison);
            game(prison,y);
            if( y == prison.size()){
                
            //cout <<"cleaning...."; 
                 cout << countzero(prison)-1 <<endl;
                clean(prison);
                //cout <<"size:"<<prison.size() <<endl;
            }
        }
        }else if( cell <=0) cout << 0 <<endl;
        //cout << countzero(prison) <<endl;
        }
    
    //cout << 
    //print(prison);
    
    
}



2
5
2
100
10







2
100
10
5
1


¿Could you obfuscate it a little more?
¿What does 2 5 2 100 10 mean?

You are stepping out of bounds in countzero()
2 <--number of line following
5 <-- input
2 <--output

100 <--input
10 <---output
Solving analytically:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

inline int integral_sqrt( int n ) // invariant: n > 0
{
    int r = 1 ;
    while( r*r <= n ) ++r ;
    return r-1 ;
}

int main()
{
    int num_lines ;
    if( std::cout << "# input lines? " && std::cin >> num_lines )
        for( int i = 0 ; i < num_lines ; ++i  )
        {
            int num_cells ;
            if( std::cout << "# cells? " && std::cin >> num_cells && num_cells > 0 )
                 std::cout << "# prisoners that escape: " << integral_sqrt(num_cells) << '\n' ;
        }
}


An analytical solution to this modified problem is a lot more interesting:
... He repeats this for m rounds (1 <= m <= n), takes a final drink, and passes out. ... Given the number of cells n, and the number of rounds m, determine how many prisoners
escape jail.

Hint: prime factorization.
Topic archived. No new replies allowed.