# CRDGAME - Editorial

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() {
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;
}


first mistake-

montypoints=1;
chefpoints=1;


second mistake-

chefpoints||montypoints


Notice the changes in code attached above.

1 Like

Thanks @jay_1048576 for correcting me. I couldn’t believe how i made this mistake :

Thankyou.

Can anyone point out the mistake in my code, it is giving correct output with the sample cases but not submitting.

#include
using namespace std;

int main() {
int t;
scanf("%d",&t);
while(t–)
{
int n,c_count=0,m_count=0;
scanf("%d",&n);
while(n–)
{
int a,b,temp=0;
scanf("%d %d",&a,&b);
while(a!=0)
{
temp+=a%10;
a/=10;
}
a=temp; temp=0;
while(b!=0)
{
temp+=b%10;
b/=10;
}
b=temp;
if(a>b){c_count++;}
else{m_count++;}
}
if(c_count>m_count){cout<<"0 "<<c_count;}
else if(m_count>c_count){cout<<"1 "<<m_count;}
else if(m_count==c_count){cout<<"2 "<<m_count;}
cout<<endl;
}
return 0;
}

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

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
System.out.println(“enter a Number for chef”);
while(sc.hasNext()){
int n=21;
int sum1=0;
while(n!=0){
sum1=sum1+n%10;
n=n/10;
}
System.out.println(sum1);
int k=20;
int sum2=0;
while(k!=0){
sum2=sum2+k%10;
k=k/10;
}
System.out.println(k);
if(sum1>sum2){
System.out.println(“chef wins the game”);
}
else{
System.out.println(“monty wins the game”);
}
}
}
}

can you guys tell me what is wrong here??

# Chef and Card Game

### Code is in PYTHON 3.6

test_cases = int(input())
for _ in range(test_cases):
rounds = int(input(""))
chef_points = 0
morty_points = 0
for _ in range(rounds):
chef, morty = input().split()
chef_pow = sum(map(int,list(chef)))
morty_pow = sum(map(int,list(morty)))
if chef_pow > morty_pow :
chef_points+=1
elif morty_pow > chef_pow:
morty_points+=1
else:
chef_points+=1
morty_points+=1
if chef_points > morty_points:
print(0,chef_points)
elif chef_points < morty_points:
print(1,morty_points)
else:
print(2,chef_points)


We’d have to guess at the original indentation, and we might get it wrong.

2 Likes

Got It !!! thanks
Now You can try it out
Thanks for the Help again

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

int main(){
int t;
cin >>t;
while(t--){

int n;
long long chef = 0,morty = 0;
cin >> n;
string a , b;

for(int i = 0; i<n; i++){
long long int A = 0, B = 0;
cin >> a >> b;

for(char x: a) A += x -'0';
for(char x: b) B += x -'0';

if(A>B) chef += 1;
else if(B>A) morty += 1;
else{
chef +=1;
morty +=1;
}
}

if(chef>morty) cout<<"0 "<<chef<<endl;
else if(chef<morty) cout<<"1 "<<morty<<endl;
else cout<<"2 " <<chef<<endl;
}
}



#include
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int n,chef=0,mory=0;
cin>>n;
for(int i=0;i<n;i++){
int a,b,n=0,f=0;
cin>>a>>b;
while(a!=0){
n=a%10;
a=a/10;
}
while(b!=0){
f=b%10;
b=b/10;
}
if(n>f) chef++;
else if(n<f) mory++;
else chef++,mory++;
}
if(chef==mory)
cout<<“2”<<" “<<chef<<endl;
else if(chef>mory)
cout<<“0”<<” “<<chef<<endl;
else
cout<<“1”<<” "<<mory<<endl;
}

return 0;
}

Help me why I’m not getting passed even if i checked my output shows right…!!