parth_2003's blog

By parth_2003, history, 8 months ago, In English

So I was solving Question Chess Tournament 1569B rated 1000. when I submit the code then the output on the given test cases are showing wrong, Although I checked them on my compiler, and the code was working fine. Here is the code,

include<bits/stdc++.h>

using namespace std;

int main(){ int t; cin >> t; while(t--){ int n; cin >> n; vector v(n); int countO=0; int countT=0; for(int i=0; i<n; i++){ cin >> v[i]; if(v[i] == 1){ countO++; } else{ countT++; } } if(countT == 2 || countT == 1){ cout << "NO" << endl; continue; }

vector<vector<char> > f(n, vector<char>(n, '='));
for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
       if(i == j){
         f[i][j] = 'X';
       }
       else if(v[i] == 1 && v[j] == 2){
         f[i][j] = '+';
       }
       else if(v[i] == 1 && v[j] == 1){
         f[i][j] = '=';
       }
    }   
}

vector<int> type2;
for(int i=0; i<n; i++){
    if(v[i] == 2){
       type2.push_back(i);
    }
}
for(int i=0; i<type2.size(); i++){
    int curr = type2[i];
    int next = type2[i+1 % (type2.size())];
    f[curr][next] = '+';
    f[next][curr] = '-';
}

cout << "YES" << endl;  
for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
       cout << f[i][j];  
    }
    cout << endl;
}

} }

  • Vote: I like it
  • -19
  • Vote: I do not like it

»
8 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

You have to fix this line of code:

int next = type2[i+1 % (type2.size())];

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    can you please tell the reason for this? is it because type2.size() is size_t datatype?

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      % operator has higher priority than +, so it's actually translated as type2[i + (1 % (type2.size())], which is unintended and gets out of the vector's range when i + 1 == type2.size().

»
8 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

deleted

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

265461929 Can someone explain why it says expected: '1005355647', found: '1005355648' even though when I run the same testcase on my compiler it gives 1005355647 as output. Pretty confused TBH.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    its because of the sqrt() that you are using i guess????

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      It gives different outputs in different compilers? I ran it on online GDB, the same code... it gives the correct output, try to run on your own device. 1 77921270569329490 377318254283917957

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

No.