Я проверил учебник и увидел, что я пытаюсь реализовать ту же идею (учебник. Проблема h).
Код: Выделить всё
#include
using namespace std;
int max_32 = 0x80000000,max_31 = 0x40000000,max_30 = 0x20000000;
//i have pretty much done similar to the tutotial soution for this problem
//slight different implementation
int arr[200020];
int bit_p[32];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,n,p;
long long k;
int sol;
cin >> t;
while(t--){
cin >> n >> k;
//this resets the count of all bist to zero
for (int i = 0; i < 32; i++)
{
bit_p[i]=0;
}
//read on integer at a time
for (int i = 0; i < n; i++)
{
cin >> arr[i];
p=max_31;
//check from MSB 2 to the last bit and if any is set increase the bit count by one
for (int j = 1; j < 32; j++)
{
if (p&arr[i])
{
bit_p[j]++;
}
p>>=1;
}
//
}
//the bits are counted correctly i verified them
//you can verify by un commenting the below section
// for (int i = 0; i < 32; i++)
// {
// cout > t;
while(t--){
cin >> n >> k;
//this resets the count of all bist to zero
for (int i = 0; i < 32; i++)
{
bit_p[i]=0;
}
//
//read on integer at a time
for (int i = 0; i < n; i++)
{
cin >> arr;
p=max_31;
//check from MSB 2 to the last bit and if any is set increase the bit count by one
for (int j = 1; j < 32; j++)
{
if (p&arr)
{
bit_p[j]++;
}
p>>=1;
}
//
}
//the bits are counted correctly i verified them
//you can verify by un commenting the below section
// for (int i = 0; i < 32; i++)
// {
// cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79640279/my-c-code-behaviour-is-confusing-what-is-happening[/url]