#can someone tell me what i did wrong in this?

1 messages · Page 1 of 1 (latest)

covert plover
#

#include <iostream>
using namespace std;

int main()
{
int n, t;
cin >> n >> t;
int a[1000];
int max = 0;
int pos;
for (int i = 0; i < n; i ++)
{
cin >> a[i];
}
for (int j = 0; j < t; j ++)
{
for (int p = 0; p < n; p ++)
{
if (a[p] > max)
{
max = a[p];
pos = p;
}
}
for (int k = 0; k < n; k ++)
{
if (k == pos)
{
a[k] = 0;
}
else
{
a[k] += max / (n-1);
}
int f = max % (n-1);
{
if (pos <= f - 1)
{
for (int x = 0; x < f + 1; x ++)
{
if (x != pos)
{
a[x] += 1;
}
}
}
else
{
for (int y = 0; y < f; y ++)
{
a[y] += 1;
}
}
}
}
pos = pos + 1;
cout << pos << endl;
}
}

so basically first i grabed an array and i made the initial array and wrote a code to find the maximum and named the position and used the remainders to do it and repeated. but however the result is not correct and i dont know why im wrong. can someone help me?

old hemlockBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

old hemlockBOT
#
#include <iostream>
using namespace std;

int main() {
  int n, t;
  cin >> n >> t;
  int a[1000];
  int max = 0;
  int pos;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int j = 0; j < t; j++) {
    for (int p = 0; p < n; p++) {
      if (a[p] > max) {
        max = a[p];
        pos = p;
      }
    }
    for (int k = 0; k < n; k++) {
      if (k == pos) {
        a[k] = 0;
      } else {
        a[k] += max / (n - 1);
      }
      int f = max % (n - 1);
      {
        if (pos <= f - 1) {
          for (int x = 0; x < f + 1; x++) {
            if (x != pos) {
              a[x] += 1;
            }
          }
        } else {
          for (int y = 0; y < f; y++) {
            a[y] += 1;
          }
        }
      }
    }
    pos = pos + 1;
    cout << pos << endl;
  }
}

so basically first i grabed an array and i made the initial array and wrote a code to find the maximum and named the position and used the remainders to do it and repeated. but however the result is not correct and i dont know why im wrong. can someone help me?

Lord_David_Kim
#
Gustavo
Please Do Not Send Screenshots!

They're hard to read and prevent copying and pasting.

covert plover
stray berry
#

It's fine, never mind

#

I thought it was screenshots of code

covert plover
#

oh lol

#

is there some errors or smth wrong in my code?

west pawn
#

i mean yes, your code doesn't match expected output

#

but it's impossible to reason about your code without proper variable names

covert plover
#

wdym?

west pawn
#

k, f, y, pos, n, t

#

these aren't meaningful

covert plover
#

oh i just did random variables

west pawn
#

without good variable names, it's hard to follow what your code is doing

covert plover
#

yeah like pos is for position

west pawn
#

sure, but position of what

covert plover
#

of the maximum number of the arrays

#

like the number we cancel out and devide it

#

and other variables i made it to do the "for"

west pawn
#

I mean, first thing I'd do is go give your code some meaningful variable names and such. It's just hard to follow, so it's hard to help you (and it will have a cognitive load on you as well, trying to follow what you wrote)

covert plover
#

ok ill first name my variables again

#

but i have a question about variables

#

when we use multiple whiles or fors in the code do we need to make all the variables different?

stray berry
west pawn
#

If they are nested, it's recommended to not make the inner loop have the same variables as the outer. If they're adjacent loops, it's fine to use the same names

stray berry
#

^^

covert plover
#

ok thx

west pawn
#
for (int i = 0; i < n; ++i) {
  for (int j = 0; j < n; ++j) {};
}

for (int i = 0; i < n; ++i) {

}
#

something like that. note how adjacent loops have the same variable name, but the nested loop has a different variable name

covert plover
#

ok thx

#

#include <iostream>
using namespace std;

int main() {
int n, t;
cin >> n >> t;
int a[1000];
int max = 0;
int pos;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < t; i++) {
for (int p = 0; p < n; p++) {
if (a[p] > max) {
max = a[p];
pos = p;
}
}
for (int q = 0; q < n; q++) {
if (q == pos) {
a[q] = 0;
} else {
a[q] += max / (n - 1);
}
int remainder = max % (n - 1);
{
if (pos <= remainder - 1) {
for (int x = 0; x < remainder + 1; x++) {
if (x != pos) {
a[x] += 1;
}
}
} else {
for (int y = 0; y < remainder; y++) {
a[y] += 1;
}
}
}
}
pos = pos + 1;
cout << pos << endl;
}
}

old hemlockBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
stray berry
#

format it so it's easier to read

covert plover
#

ok

west pawn
#

that helps some, but for those deeply nested loops, what does each index variable mean?

covert plover
stray berry
west pawn
#

The bot gives you the markup to use

covert plover
# west pawn that helps some, but for those deeply nested loops, what does each index variabl...

n, t is from the problem, i is for making the initial array, and for the next loop for i is for the t outputs, p is for to find the maximum of the array and pos is the position of it, q is for adding the numbers to the arrays due to the cancellation of the maximum and the remainder is for the left numbers after you devide and like adding into the first numbers and x and y is for the cases when we add those remainders

west pawn
#

sure, but your code should tell that story. you shouldn't need to explain it to me 😉

covert plover
#
#include <iostream>
using namespace std;

int main() {
  int n, t;
  cin >> n >> t;
  int a[1000];
  int max = 0;
  int pos;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < t; i++) {
    for (int p = 0; p < n; p++) {
      if (a[p] > max) {
        max = a[p];
        pos = p;
      }
    }
    for (int q = 0; q < n; q++) {
      if (q == pos) {
        a[q] = 0;
      } else {
        a[q] += max / (n - 1);
      }
      int remainder = max % (n - 1);
      {
        if (pos <= remainder - 1) {
          for (int x = 0; x < remainder + 1; x++) {
            if (x != pos) {
              a[x] += 1;
            }
          }
        } else {
          for (int y = 0; y < remainder; y++) {
            a[y] += 1;
          }
        }
      }
    }
    pos = pos + 1;
    cout << pos << endl;
  }
}
covert plover
#

do u tknow what i did wrong in this?

west pawn
#

no a clue yet. still trying to follow the code lol

covert plover
#

ok lol

west pawn
#

what happens if the card power can't be divided equally among the remaining cards?

covert plover
#

so for that thats the reason i made the remainder variav\ble

#

to first find the remainder when you devide and add those to the front

#

as the problem said to add the ramainding amount of numbers to the front

west pawn
#

how are you keeping track of the number of cards removed?

covert plover
#

oh in the problem it said that we dont remove the numbers and we just make it 0

#

and when we make the maximun 0 i used

west pawn
#

Sure, but you've still gotta know how many you've made 0 to make sure you're dividing the points up correctly

covert plover
#
for (int q = 0; q < n; q++) {
      if (q == pos) {
        a[q] = 0;
west pawn
#

yeah, it just looks to me like you're not adjusting your redistribution of points correctly

covert plover
west pawn
#

on the first iteration, you distribute to n - 1 cards
on the second iteration, you distribute to n - 2 cards
and so on

#

the way you have it now, you're giving points back to cards you've made 0

covert plover
#

wait

west pawn
#

I'm not 100% sure that's the problem, but it looks like a problem

covert plover
#

i think the problem says to just devide by n-1

west pawn
#

oh okay, so you're giving points back to existing cards

#

interesting

covert plover
#

so shoudn't we just distribute only n-1/

west pawn
#

hmm, something here just feels overcomplicated

stray berry
#

you should print out the numbers before and after changing them

covert plover
#

yeah i think i wrote the code too complicated

west pawn
#

it also probably doesn't help that it's almost 2am and I need sleep KEKW

stray berry
#

I have a solution but I would rather not just have you copy and paste it

covert plover
#

is my stragety right?

#

like the overall code

stray berry
#

yeah

stray berry
#

I see you're in eastern time zone lol, it's about to turn 1 for me

covert plover
#

its 2am to me rn

west pawn
#

as soon as i get descriptor set allocation working for something, yeah, i'm going to sleep

west pawn
#

i've got exactly what i want to do 🙂

covert plover
#
#include <iostream>
using namespace std;

int main() {
  int n, t;
  cin >> n >> t;
  int a[1000], b[1000];
  int max = 0;
  int pos;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < t; i++) {
    for (int p = 0; p < n; p++) {
      if (a[p] > max) {
        max = a[p];
        pos = p;
      }
    }
    for (int q = 0; q < n; q++) {
      if (q == pos) {
        a[q] = 0;
      } else {
        a[q] += max / (n - 1);
      }
      int remainder = max % (n - 1);
      {
        if (pos <= remainder - 1) {
          for (int x = 0; x < remainder + 1; x++) {
            if (x != pos) {
              a[x] += 1;
            }
          }
        } else {
          for (int y = 0; y < remainder; y++) {
            a[y] += 1;
          }
        }
      }
    }
    b[i] = pos + 1;
    cout << b[i] << endl;
  }
}
#

so i changed it but it doesnt work still

#

wait can u ell me like another stragetie?

west pawn
#

Have you tried debugging the test case?

covert plover
#

i think like mine is too complicated

covert plover
stray berry
covert plover
west pawn
#

Your IDE should have a debugger

stray berry
#

This is the correct output

11 2 4 9 
0 6 8 12 
1
0 6 8 12 
4 10 12 0 
4
4 10 12 0 
8 14 0 4 
3
8 14 0 4 
13 0 5 8 
2
13 0 5 8 
0 5 9 12 
1
#

the two lines of nums are just for testing

stray berry
#

but youre right it would help

covert plover
#

yeah idk what that is i think

stray berry
#

its okay, just try the cout statements for now

covert plover
#

ok

west pawn
covert plover
#

is there a debugger in this?

#

is the bottom box thing a debugger?

stray berry
#

i wouldnt use codeblocks

#

and i am not sure if it has one

#

oh it probably does actually

west pawn
#

codeblocks should

covert plover
#

yeah i think it has

west pawn
#

is this something school is requiring you to use?

stray berry
#

Debugger tab at the bottom

stray berry
covert plover
#

mine shows this

#

maybe like i should change my whole code lol ooffff

stray berry
#

lol, does n go down one?

covert plover
#

wdym go down one?

stray berry
#

post the current code

stray berry
covert plover
#
#include <iostream>
using namespace std;

int main() {
  int n, t;
  cin >> n >> t;
  int a[1000], b[1000];
  int max = 0;
  int pos;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < t; i++) {
        for (int index = 0; index < n; ++index) {
    std::cout << a[index] << ' ';
}
std::cout << '\n';
    for (int p = 0; p < n; p++) {
      if (a[p] > max) {
        max = a[p];
        pos = p;
      }
    }
    for (int q = 0; q < n; q++) {
      if (q == pos) {
        a[q] = 0;
      } else {
        a[q] += max / (n - 1);
      }
      int remainder = max % (n - 1);
      {
        if (pos <= remainder - 1) {
          for (int x = 0; x < remainder + 1; x++) {
            if (x != pos) {
              a[x] += 1;
            }
          }
        } else {
          for (int y = 0; y < remainder; y++) {
            a[y] += 1;
          }
        }
      }
    }
    b[i] = pos + 1;
    cout << b[i] << endl;
    for (int index = 0; index < n; ++index) {
    std::cout << a[index] << ' ';
}
std::cout << '\n';
  }
}
stray berry
#

thats why

covert plover
#

oh

stray berry
#

can you try
4 5
11
2
4
9

covert plover
#

let me try again

#

4 5
11 2 4 9
11 2 4 9
1
0 9 11 12
0 9 11 12
4
4 13 15 0
4 13 15 0
3
9 18 0 5
9 18 0 5
2
15 0 6 11
15 0 6 11
2
21 0 12 17

#

got this

#

hmm mine is rlly weird

stray berry
covert plover
#

yeah

stray berry
#

do you see why your code produces 0 9 11 12 in the second iteration?

covert plover
#

idk why i got 0 9 11 12 like idk what is wrong with my code.

stray berry
#

should be 0 6 8 12

covert plover
#

so i found the maximum and devided into n-1 and added the remainder to the front

stray berry
#

I will say, your second for loop is over-complicated for what is required

covert plover
#

yeah... i think so too

#

oh wait

#

fuck i found out

#

lol

#

thxthx

stray berry
#

Nice

covert plover
#

it was a rlly silly mistake from the variables lol

stray berry
#

yeah you kind of gave yourself a hard time with the names tbh

covert plover
#

yeah but like how do u organize variables easily?

stray berry
#

does it run thru all the test cases?

covert plover
stray berry
stray berry
covert plover
#

yeah i got it

#

thx

#

btw how did u do it?

#

i think mine is too complicated and i have no idea how to make i more simple

stray berry
#

oh here ill send it

#
#include <iostream>
using namespace std;

int main() {
    int n, t;
    cin >> n >> t;
    int a[1000];
    int max, pos, count;

    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < t; ++i) {
        max = 0;

        for (int j = 0; j < n; ++j) {
            if (a[j] > max) {
                max = a[j];
                pos = j;
            }
        }
        a[pos] = 0;

        count = (max % (n - 1));
        for (int j = 0; j < n; ++j) {
            if (j == pos)
                continue;
            if (count) {
                ++a[j];
                --count;
            }
            a[j] += (max / (n - 1));
        }

        std::cout << pos + 1 << '\n';
    }
}
plain cedarBOT
#
Program Output
1
4
3
2
1
covert plover
#

what is the count for?

stray berry
#

count's for adding one to the cards

covert plover
#

oh its remainder

stray berry
#

making sure to add one to only the correct amount

covert plover
#

count = (max % (n - 1));

stray berry
covert plover
#

at here do we need to do the perentheses before max and after n-1?

covert plover
stray berry
covert plover
#

ok

stray berry
#

helps me a bit to not mess stuff up yk

covert plover
#

lol

stray berry
#

but not needed ofc

covert plover
stray berry
#

but now u will remember!

covert plover
#

lol thx very much today for helping me!

stray berry
#

yep am glad i was able to help

#

took me a bit to figure out the problem

covert plover
#

lol

stray berry
#

feel free to do !solved once ur done

covert plover
#

ok