Byang's Additions

What’s the problem in my code?

#include<stdio.h>
#include<string.h>

int main(){
    char a[8],b[8];
    scanf("%s",a);
    scanf("%s",b);
    int l1=strlen(a);
    l1--;
    int l2=strlen(b);
    l2--;
    if(l1<l2){

    }
    else{
        l1=l2;
    }

    for(;l1>=0;l1--){
        if(((a[l1]-'0')+(b[l1]-'0'))>9){
            printf("Yes\n");
            return 0;
        }
    }
    printf("No\n");
    return 0;
}

@Burn_Fireblaze I think the clue for what is wrong is some here in this topic already. :slight_smile:

Check out the existing discussions.

:astonished::rage::rage::rage::rage::rage::rage::smiling_imp::smiling_imp::imp::imp::imp::japanese_ogre::japanese_ogre::japanese_ogre::japanese_ogre::japanese_goblin::japanese_goblin::japanese_goblin::japanese_goblin::japanese_goblin::japanese_goblin::japanese_goblin:

Try some other (more precise) way of counting the number of digits.

now got ac. I just forgot to add 1 with log10()

1 Like
#include<bits/stdc++.h>
using namespace std;
int main(){
    char j,k;
    string c,d;
    int a,b,i,x,y=0,count=0;
    cin>>c>>d;
    if(c.length()>d.length()){
        x=c.length();
        y=c.length()-d.length();
    }
    else{
        x=d.length();
        y=d.length()-c.length();
    }
    for(i=y;i<x;i++){
        j=c[i];
        k=d[i];
        a=j-'0';
        b=k-'0';
        if(a+b>=10){
            count++;
        }

    }
    if(count>0){
        cout<<"Yes";
    }
    else{
        cout<<"No";
    }
    return 0;
}

getting wrong answer in 2nd last test case

Your code produce wrong answer for input value 1238 7 is should print YES but it is printing NO
Let’s see why you’re getting wrong answer.
In your code for this input value x = 4 and y = 3. Then you’re starting the loop from y which is equal to 3. then you’re checking the third position of d which is empty because string d = "7" which means that d[0] = '7' and d[3] = ' ' so when you’re adding c[3]+d[3] that can give you random value like -60 so, that will never become bigger than 10 and count will not increase and you will get wrong answer.
The correct approach to this problem is first reverse the two string.
this way reverse(c.begin(), c.end()
and for the second string reverse(c.begin(), c.end()
then run the loop from 0 to min(c.length(), d.length())
I hope this will work. Let us know if there is any problem.

1 Like

it worked! Thanks for helping me out :slight_smile:

You’re Welcome :smiley:

1 Like
 num1,num2=input().split()
x=len(num1)
y=len(num2)
if x<y:
	m=x-1
else:
	m=y-1
while m>0:
	m=m-1
	carry=int(num1[m])+int(num2[m])
	if carry>9:
		checker=1
		break
	else:
		checker=0
if checker==1:
	print("Yes")
else:
	print("No")

it shows wrong answer at 5th test case…but why?

in line 4-7

if x<y:
	m=x-1
else:
	m=y-1

you are doing len - 1 here, right? which means m is the index of the last character of the strings.

then again in line 8 - 9

while m>0:
	m=m-1

you are again doing m = m - 1. So, Suppose if len = 2, then m becomes 0 here. means that you will miss m = 1 (the last character).

You are literally forgetting to check the last character of the string here. Your code will fail for simpliest possible inputs or inputs where byang fails to do the sum of the first numbers from the left.
Inputs like these,

# Input 1
5 5
# Input 2
126 459

Thank’s bro i change it while m>=0 now it has been accepted

1 Like
n=True
a,b=map(int, input().split())
a=str(a)
b=str(b)
for i in range(1,len(str(min(a,b)))+1):
    if int(a[-i])+int(b[-i])>=10:
        n=False
        break
print('Yes' if n==False else 'No')

It says ‘Runtime error’ in the 5th test.

Well, first of all, The first three lines are unnecessary. the split() method returns a list of strings itself. So, Just try writing this should do,

a, b = map(str, input().split()) # or simply,
a, b = input().split()

However, if you try to convert them into integer first, you will face RE. Python cannot handle such big integers. So, you must handle them as strings.

The Error looks somewhat like this,

>>> bigNum = 10 ** 10000 # 10^10000
>>> str = str( bigNum )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

Sorry @touhidur vaiya
Couldn’t relate. When I input the same codes in my python shell. The output is something like this-

>>> bigNum=10**10000
>>> str=str(bigNum)
>>> print(str)

Squeezed text(126 lines)

where clicking on the last line would show me 10^10000.

Also, I changed my code as below-

n=True
a,b=map(str, input().split())
for i in range(1,len(min(a,b))+1):
    if int(a[-i])+int(b[-i])>=10:
        n=False
        break
print('Yes' if n==False else 'No')

Still didn’t work

The Problem is in line 3,

len(min(a,b))

I think it should be like this,

min(len(a), len(b))
1 Like

It worked. Thank you.

num1, num2 = input().split()
num1 = list(map(int, num1))
num2 = list(map(int, num2))
result = []
for i in range(len(num1)):
try:
a = num1[i]
b = num2[i]
except IndexError:
break
else:
if a+b < 10:
result.append(False)
else:
result.append(True)

if any(result):
print(“Yes”)
else:
print(“No”

this code failed in test 6

My code gets wrong answer only for test case 7, I’ve done everything but still could not solve it, any and all help is welcome, thank you.

a,b = map(str,input().split())
n = min(len(a),len(b))
A = list(a)
A = a[::-1]
B = list(b)
B = b[::-1]
if int(A[0]) + int(B[0]) > 9:
—print(“Yes”)
—quit()
for i in range(n-1):
—if int(A[i]) + int(B[i]) > 9:
------print(“Yes”)
------quit()
print(“No”)