One of the members of the delegation came forward on behalf of the others and, without any explanation, selected the people who were in the even position and formed a new line with them. Then, in the new queue, as before, he selected the people who were in the even position and continued this process until only one person remained.
The first line is a natural number (n) that determines the number of rows . In the next (n) line, each line contains a natural number (m) that specifies the number of people in the corresponding row.
1 <= n <= 1000
1 <= m <= 100000000
The output of your program should include (n) lines, in each line, the last person's position number is printed.
io examples :
i :
1
5
o :
4
------------
i :
5
11
10
19
1
7
o :
8
8
16
1
4
---------------------
i think it should be solved like this
1 = numbers that theyre going to enter / row
5 = entered number / people in the row
1
2 --- 1
3
4 --- 2 ---- last person that stands in even position
5
cout << 4 ; cuz he was the 4th person in the original line
#include <iostream>
#include <cmath>
usingnamespace std;
#define A 1000
int main()
{
int n,m,c,h;
int Y[A];
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> m;
for (int j = 1; j <= m; j++)
{
h =pow(2,j);
if (h <= m)
{
c = h;
}
else
{
Y[i] = c;
c = 1;
break;
}
}
}
for (int i = 1; i <= n; i++)
{
cout << Y[i] << endl;
}
}
i just have one last question , is there a more sufficient/shorter way to write this that could reduce execution time ?
#include <iostream>
#include <cmath>
usingnamespace std;
#define A 1000
int nextLowerPowerOf2(int);
int main()
{
int n,m,c,h;
int Y[A];
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> m;
Y[i] = nextLowerPowerOf2(m);
}
for (int i = 1; i <= n; i++)
{
cout << Y[i] << endl;
}
}
int nextLowerPowerOf2(int m)
{
int ans = 1;
while (m /= 2) ans *= 2;
return ans;
}
#include <iostream>
#include <sstream>
usingnamespace std;
int nextLowerPowerOf2( int m )
{
int ans = 1;
while ( m /= 2 ) ans *= 2;
return ans;
}
int main()
{
stringstream in( "5 \n""11\n""10\n""19\n""1 \n""7 \n" );
int m, n;
in >> n;
while( n-- )
{
in >> m;
cout << nextLowerPowerOf2(m) << '\n';
}
}