I want to view index, but the data stored in the array is displayed.

Asked 1 years ago, Updated 1 years ago, 229 views

https://atcoder.jp/contests/abc068/tasks/abc068_b

In the excerpt code below, I would like to display the index in max, but the data stored in the array will be displayed instead of the index.
How should I correct it to display the index?

I am using Cpp on Paiza.io.

test values
Input: 100
Output: 64

現在 Currently, 8 is output.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    int max = 0;
    for(int j=1;j<=n;j++){
        if(max<data[j])max=j;
    }

    cout<<max<<endl;

    return 0;


# include <bits/stdc++.h>

using namespace std;

int getDivisionCount(inti){
    int count = 0;
    while(i%2==0){
        i/=2;
        count++;
    }
    return count;
}

int main() {
    intn;
    cin>>n;
    
    int data [100];
    
    for(inti=1;i<=n;i++){
        data[i] = getDivisionCount(i);
    }
    
    int max = 0;
    for(int j=1;j<=n;j++){
        if(max<data[j])max=j;
    }
    
    cout<<max<<endl;
    
    return 0;
}

c++

2023-01-18 13:32

2 Answers

if(max<data[j])max=j;

What value do you assume max is a variable to handle?

Comparing data[j], max appears to be for handling data values, but
j is substituted, not the data value, but the
By the way, it's obviously strange.

I don't know if it should be a data value or an index, but I think we should deal with the object as expected.
If you want to handle something else, why don't you set up a separate variable for that?


2023-01-18 15:10

data is an array of getDivisionCount(), so look for the maximum value (max) in that array.The answer is multiplied by max.

#include<bits/stdc++.h>

using namespace std;

int getDivisionCount(inti){
    int count = 0;
    while(i%2==0){
        i/=2;
        count++;
    }
    return count;
}

int main() {
    intn;
    cin>>n;
    
    int data [100];
    
    for(inti=1;i<=n;i++){
        data[i] = getDivisionCount(i);
    }
    
    int max = 0;
    for(int j=1;j<=n;j++){
        if(max<data[j])max=data[j];
    }
    
    cout<<power(2,max)<<endl;
    
    return 0;
}


2023-01-18 19:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.