@anon98533376 can you please explain the logic behind!
In case of 1 and 99 if you swap the numbers it will either give 91 and 9 or 19 and 9. In case of 91 and 9 sum is 100 as in the case of 1 and 99. Hence maximum answer is 100. What your code is doing it is actually taking 01 and 99 and hence replacing the digits to 90 and 91.
Since the Constraints are small so why not just brute force?
#include <iostream>
using namespace std;
string A,B ;
void solve(){
cin >> A >> B ;
int ans =stoi(A)+stoi(B) ;
for(char &a :A)
for(char &b:B){
swap(a,b) ;
ans=max(ans,stoi(A)+stoi(B)) ;
swap(a,b) ;
}
cout<<ans<<"\n";
}
int main() {
int t ; cin >> t ; while(t--)solve();
return 0;
}
Yes this is quite easy for this problem
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin>>t;
while(t–)
{
int a,b,s1=0,s2=0;
cin>>a>>b;
int a1[2]={a/10,a%10},b1[2]={b/10,b%10};
if( a1[0]<b1[1]&&a1[0]!=0 )
s1+=(b1[1]+b1[0])*10+(a1[0]+a1[1]);
if( b1[0]<a1[1]&&b1[0]!=0 )
s2+=(a1[0]+a1[1])*10+(b1[1]+b1[0]);
cout<<max(s1,max(a+b,s2))<<endl;
}
return 0;
}
you can clearly see through it
we just have to avoid swapping 0 which is not there in single digits
otherwise we have to simply compare 1st digit of a number with second digit of another number
if(1st digit is smaller){ we swap and store the sum from this swapping }
this we do for both numbers and then finally check which swapping gave maximum sum
![]()
every one code is giving output 927 why?
1
917 9
997 +1=998 should be a answer?
this is not even a valid test case 917 > 99
may be, every other code only considering for maximum 2 decimal digits
ohk sorry missed that constraint
Why output of 1 99 is not 181 as sum of 91+90???
while output of 5 49 is 135 as sum of 95+40.
@hasinfarhan PLZ explain.
Can someone what is wrong with my code ->
#include <bits/stdc++.h>
int main(){
int t;
cin>>t;
while(t>0){
int A,B;
cin>>A>>B;
int Ac=A,Bc=B;
int y = A%10;
Ac=A/10;
int x = Ac%10;
int a = B%10;
Bc=B/10;
int b = Bc%10;
int ans=A+B;
if(A<10&&B<10){
ans = A+B;
}else if(A < 10 && B >= 10){
ans = max(ans, y + a*10 +b);
ans = max(ans, a + y*10 + b);
ans = max(ans,b + a*10 + y);
}else if(B<10 && A>= 10){
ans = max(ans, b + x*10 +y);
ans = max(ans, x + b*10 + y);
ans = max(ans,y + x*10 + b);
}else{
ans = max(ans, a*10 + y + x*10 + b);
ans = max(ans, b*10 + y + a*10 + x);
ans = max(ans, x*10 + a + y*10 + b);
ans = max(ans, x*10 + b + a*10 + y);
}
cout<<ans<<"\n";
t--;
}
return 0;
}
Check your answer for a=4 and b=5. Your code will give answer 54 but answer should be 9.
You are not taking care of cases in which one number is single digit and other number is double digit. Eg. 13 and 3 should give answer 34 but you code is giving answer 16.
thank you so much i got my mistake.
Thanks, I think I got it
Output of 5 49 is 63.
https://www.codechef.com/viewsolution/30851623
For which case is this giving wrong answer.
(I’m using Python)
Your code is giving wrong answer for case 18 and 37. Its answer should be 109 but your code gives answer 100. Reason behind is that your code changes number to 13 and 87 but actually it should change numbers to 78 and 31.