CRDGAME - Editorial

#include <bits/stdc++.h>
using namespace std;
int main()
{
  long long int t;
  cin>>t;
  while(t--){

    long long int num;
    cin>>num;
    long long int final_c=0,final_m=0;
    long long int points_c,points_m,sum1=0,sum2=0;

    while(num--){
      long long int c,m;
      cin>>c>>m;

      if(c < 10){
        points_c = c;
      }

      if(m < 10){
        points_m = m;
      }

      if(c >= 10){
        long long int temp=c;
        while(temp > 0){
          sum1 = sum1 + temp % 10;
          temp=temp/10;
        }
        points_c = sum1;             
      }




      if(m >= 10){
        long long int t=m; 
        while(t > 0){
          sum2 = sum2 + t % 10;
          t=t/10;
        }
        points_m = sum2;
      }





      if(points_c > points_m)
      final_c++;
      else
      if(points_c < points_m)
      final_m++;
      else
      if(points_c == points_m){
        final_m++;final_c++;
      }
    }
    



    if(final_m > final_c){
      cout<<"1 "<<final_m;
    }
    else
    if( final_c > final_m){
      cout<<"0 "<<final_c;
    }
    else
    if(final_c==final_m){
      cout<<"2 "<<final_m;
    }
  }
  return 0;
}

//This is my code. I checked the given testcases, it gave right output. But my code was not accepted . Please let me know what went wrong//

You must initialise sum1 and sum2 back to 0 at the end of every round.

t = int(input())
c, m =[], []
a, b = [], []
for i in range(t):
	N = int(input())
	for num in range(N):
		A, B = list(input().split())
		A.strip(' ').split(), B.strip(' ').split()
		
		for num in A:
			num = int(num)
			a.append(num)
		for nums in B:
			nums = int(nums)
			b.append(nums)
		if sum(a) > sum(b):
			c.append(1)
		else:
			m.append(1)
		a.clear()
		b.clear()

	if sum(c) > sum(m):
		print(0, sum(c))
	if sum(m) > sum(c):
		print(1, sum(m))
	elif sum(c) == sum(m):
		print(2, sum(c))
	c.clear()
	m.clear()`

// This is my solution, I don’t know why this is not accepted? In sample and custom inputs cases, it works perfectly fine. Can somebody tell me where I made mistake?

No, for finding the sum of digits algorithm will take O(log10(n))[base 10] ,which will be 9 in this case. So overall time will be 1000.100.log10(n)

Here, you have to compare sum of digits on cards , not the sum of numbers on the cards.

#include<bits/stdc++.h>
using namespace std;
int sum(int a){
    int x;
    while(a>0){
        x=x+a%10;
        a=a/10;
    }
    return x;
}
void sol(){
    int n,aot,bot;
    cin >>n;
    int apt=0,bpt=0;
    int a,b;
    while(n>0){
    cin >> a >> b;
    if(a>9){
        aot+=sum(a);
    }
    if(b>9){
        bot+=sum(b);
    }
    else{
        bot=b;
    }
    if(aot>bot){
        apt++;
    }
    else{
        bpt++;
    }
    n--;
    }
    if(apt>bpt){
        cout<< "0 " <<apt<<endl;
    }
    else if(bpt>apt){
        cout << "1 "<<bpt<<endl;
    }
    else if(apt==bpt){
        cout<< "2 "<<apt<<endl;
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   int t;
    cin >> t;
    while (t>0){
        sol();
        t--;
    }


}

can anyone please help me why the code is throwing wrong answer?

Here is the link to corrected submission. They were many mistakes. Notice the changes.

These are the mistakes in your code

  1. You did not write else condition for if(a>9).
    2.You did not write the condition for draw round.
  2. You are adding the function to aot and bot instead assign the to aot and bot .
    i did the changes here

My code showing WA. It generate correct answer for sample test case. Can anyone please point out what’s wrong with this code.

    /* package codechef; // don't place package name! */

import java.io.IOException;
import java.util.Scanner;
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Scanner; 
import java.util.StringTokenizer; 

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
static long sum(long N) {
	long sum = 0;
	while(N % 10 > 0) {
		sum += N % 10;
		N = N/10;
	}
	return sum;
}

public static void main (String[] args) throws java.lang.Exception
		{
			Scanner sc = new Scanner(System.in);
			int t = 0;
			if(sc.hasNextInt()){
			 t = sc.nextInt();
			}
			while( t-- > 0) {
				int rounds = sc.nextInt();
				int chef_count = 0;
				int morty_count = 0;
				for(int i = 0; i < rounds; i++){
					long chef_card, morty_card = 0;
				    chef_card = sc.nextLong();
				    morty_card = sc.nextLong();
				    if(sum(chef_card) > sum(morty_card))
				        chef_count++;
				    else if(sum(morty_card) > sum(chef_card)) 
				        morty_count++;
				    else {
				    	chef_count++;
				    	morty_count++;
				    }
				}
				if(chef_count > morty_count){
				    System.out.println("0 "+chef_count);
				}
				else if(morty_count > chef_count){
				    System.out.println("1 "+morty_count);
				}
				else
				    System.out.println("2 "+chef_count);
				
			}
		}
	}

It goes wrong there. It should be while(N>0). Your code will go wrong for N=100 or any number having the digit 0 in it.

1 Like

What’s Wrong in my code

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

long long findSum(long long n){
	
	long long sum=0;
	while(n>0){
		sum+=n%10;
		n=n/10;
	}
	return sum;
}
long long  sumDigits(long long  no) 
 { 
   return no == 0 ? 0 : no%10 + sumDigits(no/10) ; 
 } 


int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		long long a,b;
		long long x1=0,x2=0;
		for(int i=0;i<n;i++){
			cin>>a>>b;
			long long y1=sumDigits(a);
			long long y2=sumDigits(b);
			if(y1>y2){
				x1++;
			}
			else if(y2>y1){
				x2++;
			}
		}
		if(x1==x2){
			cout<<"2 "<<x1<<endl;
		}
		else if(x1>x2){
			cout<<"0 "<<x1<<endl;
		}
		else{
			cout<<"1 "<<x2<<endl;
		}
	
	}
	return 0;
}

Haven’t checked the whole code yet but seems like when both win the game, you are not increasing their scores.

You must add one point to each of their scores if y1==y2

/* Name of the class has to be "Main" only if the class is public. */
import java.util.*;
import java.util.Scanner;
import java.lang.*;
import java.io.*;
class Solution{
    public static void main (String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int chef=0;
        int morty=0;
        int h = scan.nextInt();
        for(int p=0;p<h;p++) {
        int n = scan.nextInt();
        int[][] b= new int[n][2];
        for(int i=0;i<n;i++){            
            for(int j=0;j<2;j++){
                b[i][j] = scan.nextInt(); 
            }
        }
        
        
        for(int i=0;i<n;i++){
            for(int j=0;j<2;j++){
                int k = b[i][j]/10;
                int l = b[i][j]%10;
                b[i][j]=k+l;
            }
        }
        
        for(int i=0;i<n;i++){
            if(b[i][0]>b[i][1])
            chef++;
            else
            morty++;
            
        }
        if(chef>morty) {
        	System.out.println(0+" "+chef);
        }
        else if(chef<morty) {
        System.out.println(1+" "+morty);
        }
        else if(chef == morty) {
        	System.out.println(2+" "+chef);
        }
        chef=0;
        morty=0;
        }
        scan.close();
    }
}

//why my code is not working

I don’t know java but conditional statements are wrong.

I used the same code on another editor and it gave me the same output but cant figure out why its giving me error here.

b[i][0]==b[i][1]

This case is not covered.
The way you are calculating sum of digits of numbers is also wrong.

Video Solution:

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

int compare(long long int a,long long int b){
    
    long long int sum1=0,sum2=0;
    
    while(a>0 && b>0){
        sum1+=a%10;
        sum2+=b%10;
        a=a/10;
        b=b/10;
    }
    
    if(a){
        while(a){
        sum1+=a%10;
        a=a/10;
        }
    }
    else if(b){
        while(b){
        sum2+=b%10;
        b=b/10;
        }
    }
    if(sum1>sum2) return 10;
    else if(sum1<sum2) return 20;
    else if(sum1==sum2) return 30;
}

int main() {
	long long int t;
	cin>>t;
	
	while(t--){
	   long long int n;
	   cin>>n;
	   long long int chef[n];
	   long long int monty[n];
	    
	    for(long long int i=0;i<n;i++){
	        cin>>chef[i];
	        cin>>monty[i];
	    }
	    
	   long long int chefpoints=0;
	   long long int montypoints=0;
	   for(long long int i=0;i<n;i++){
	       int decision=compare(chef[i],monty[i]);
	       if(decision==10){
	           chefpoints+=1;
	       }else if (decision==20){
	           montypoints+=1;
	       }else{
	           montypoints=1;
	           chefpoints=1;
	       }
	   } 
	   
	   if(chefpoints>montypoints){
	       cout<<"0"<<" "<<chefpoints<<endl;
	       
	   }else if(chefpoints<montypoints){
	       cout<<"1"<<" "<<montypoints<<endl;
	       
	   }else{
	       cout<<"2"<<" "<<(chefpoints||montypoints)<<endl;
	   }
	    
	}
	
	return 0;
}

//this is my soln why it is giving wrong ans please tell me

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

int compare(long long int a,long long int b){
    
    long long int sum1=0,sum2=0;
    
    while(a>0 && b>0){
        sum1+=a%10;
        sum2+=b%10;
        a=a/10;
        b=b/10;
    }
    
    if(a){
        while(a){
        sum1+=a%10;
        a=a/10;
        }
    }
    else if(b){
        while(b){
        sum2+=b%10;
        b=b/10;
        }
    }
    if(sum1>sum2) return 10;
    else if(sum1<sum2) return 20;
    else if(sum1==sum2) return 30;
}

int main() {
    long long int t;
    cin>>t;
    
    while(t--){
       long long int n;
       cin>>n;
       long long int chef[n];
       long long int monty[n];
        
        for(long long int i=0;i<n;i++){
            cin>>chef[i];
            cin>>monty[i];
        }
        
       long long int chefpoints=0;
       long long int montypoints=0;
       for(long long int i=0;i<n;i++){
           int decision=compare(chef[i],monty[i]);
           if(decision==10){
               chefpoints+=1;
           }else if (decision==20){
               montypoints+=1;
           }else{
               montypoints+=1;
               chefpoints+=1;
           }
       } 
       
       if(chefpoints>montypoints){
           cout<<"0"<<" "<<chefpoints<<endl;
           
       }else if(chefpoints<montypoints){
           cout<<"1"<<" "<<montypoints<<endl;
           
       }else{
           cout<<"2"<<" "<<(chefpoints)<<endl;
       }
        
    }
    
    return 0;
}

If you prefer link
first mistake-

montypoints=1;
chefpoints=1;

second mistake-

chefpoints||montypoints

Notice the changes in code attached above.

1 Like