There are two players, each with a deck of N \leq 100 cards. Strength of a card is defined as sum of digits of number A_i \leq 10^9 written on that card. There will be N rounds. In each round, either player will draw the first card on their respective decks. The one with higher strength wins and get 1 point. If both have the same strength, both get 1 point. We have to report who wins after N rounds are over.
EXPLANATION:
We just simulate the process. We iterate over the cards one by one and see whose strength is more. We keep 2 variables to keep track of the score of each person.
In the end, we just compare the scores and output accordingly.
The only important part if finding the sum of digits. The following is a sample code to do that, in C++.
void digitSum(int num){
int sum = 0;
while(num > 0){// That means the number has no digits left.
sum += num %10; // This extracts the last digit.
num /= 10; // This Removes the last digit.
}
return sum;
}
#include<bits/stdc++.h>
using namespace std;
int less_10(long long a){
long long c=a;
long long l=11,p=0;
while(l>=10){
while(c!=0){
p += c%10;
c = c/10;
}
l=p;
c=p;
p=0;
}
//cout<<l<<"\n";
return l;
}
int main(){
int t,p=0;
cin>>t;
int ac=0,bc=1,dc=2;
while(t--){
int n;
cin>>n;
long long count=0,count1=0;
while(n--){
long long chef, morty;
long long a =0,b=0,flag=0,flag1=0;
cin>>chef>>morty;
if( chef>=10){
flag =1;
a = less_10(chef);
}
if (morty>=10){
flag1 =1;
b = less_10(morty);
}
if(flag==1 && flag1==1){
if (a>b)
count++;
else if (b>a)
count1++;
else {
count++;
count1++;
}
}
else if (flag ==0 && flag1==1){
if (chef>b)
count++;
else if (b>chef)
count1++ ;
else {
count++;
count1++;
}
}
else if (flag == 1 && flag1==0){
if (a>morty)
count++;
else if (a<morty)
count1++ ;
else {
count++;
count1++;
}
}
else {
if (chef>morty)
count++;
else if (chef<morty)
count1++;
else {
count++;
count1++;
}
}
}
if (count>count1)
cout<<ac<<" "<<count<<"\n";
else if (count1>count)
cout<<bc<<" "<<count1<<"\n";
else
cout<<dc<<" "<<count<<"\n";
}
}
// THis is my solution why it is giving wrong answer please tell me
I think you misunderstood the question. You are supposed to find the sum of digits of the number. What your program is finding is the sum of the digits until it becomes a single digit. Like for 99, the sum should be 18 but your function is returning 9.
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?
#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;
}
/* 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();
}
}