-1

why this code below have a different output, would you like to explain it ?

input : 10 3 2 4 2 1 1 1 2 1 3 9 3 9 11 9 12 9 1000 8 11

Code #1 :

#include <bits/stdc++.h>
using namespace std;

#define ll long long 

int main(){
    ll t , limak_max , bob_max , limak_total = 0, bob_total = 0;
    
    cin >> t;
    
    while(t--){
        cin >> limak_max >> bob_max;
        
        int i = 1;
        
        while(1){
            if(i&1){
                limak_total += i;
            }
            
              else{
                bob_total += i;
               }
            
            if(limak_total > limak_max){
                cout << "Bob" << '\n';
                break;
            }
            else if(bob_total > bob_max){
                cout << "Limak" << '\n';
                break;
            }
            
            i++;
        }
    }
}

Code #2 :

#include <bits/stdc++.h>
using namespace std;

#define ll long long 

int main(){
    ll t , limak_max , bob_max , limak_total , bob_total;
    
    cin >> t;
    
    while(t--){
        cin >> limak_max >> bob_max;
        
         limak_total = 0;
         bob_total= 0;
        int i = 1;
        
        while(1){
            if(i&1){
                limak_total += i;
            }
            
              else{
                bob_total += i;
               }
            
            if(limak_total > limak_max){
                cout << "Bob" << '\n';
                break;
            }
            else if(bob_total > bob_max){
                cout << "Limak" << '\n';
                break;
            }
            
            i++;
        }
    }
}
Nathan Pierson
  • 5,461
  • 1
  • 12
  • 30
  • 1
    https://stackoverflow.com/editing-help – Yunnosch Jun 05 '22 at 08:46
  • 1
    In the first version, you set `limak_total` and `bob_total` to `0` once, outside your `while(t--)` loop. In the second version, you reset them to `0` at the beginning of each iteration of the loop. – Nathan Pierson Jun 05 '22 at 08:47
  • You do know that they are not the same. Are you aware of the formal difference and can you describe it? What makes you think that the difference which is there should be irrelevant for the output? – Yunnosch Jun 05 '22 at 08:49
  • 2
    It looks like you are trying to learn C++ from one of those competitive programming sites. I advise against it. They only reach bad habits ([one](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [two](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) [three](https://stackoverflow.com/questions/67649609/using-preprocessing-directive-define-for-long-long)) and sloppy techniques. Get yourself a good reputable C++ book. – n. m. could be an AI Jun 05 '22 at 08:49
  • Seems obvious to me (exactly as Nathan says). Seems the real question is why you think different code should produce the same output. – john Jun 05 '22 at 08:50
  • 1
    There are sites which pursue to teach you coding, a book is not necessarily needed. But those competitive sites do NOT try to teach, they only allow you to test what you already learned. – Yunnosch Jun 05 '22 at 08:51

1 Answers1

1

Well, just look at where the two codes differ. For Code #2 there are these two lines:

limak_total = 0;
bob_total = 0;

Code #1 does not contain these two lines.

In between different iterations of your while(t--), which I'm assuming to be your different testcases, Code #1 does not reset limak_total and bob_total, which you use when determining your output. Code #2 does.

Therefore they have different outputs.

Ryan Zhang
  • 1,856
  • 9
  • 19