Thanks sharing this question
Weird problem. I got an AC after some WA, I changed the approach but I have no idea what was wrong with the first approach. Maybe something’s wrong with how it tests solutions.
What is wrong here???
import java.util.;
public class Main
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int a,b,x,d;
a=sc.nextInt();
b=sc.nextInt();
x=a-b;
d=x%10;
x=x/10;
d=(d+1)%10;
x=x10+d;
System.out.println(x);
}
}
#include<stdio.h>
int main()
{
int a,b,ans,result;
scanf("%d",&a);
scanf("%d",&b);
result=a-b;
if(result%10==0)
ans=result+1;
else
ans=result-1;
printf("%d",ans);
}
why this is wrong?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
a-=b;
string s="";
s+=to_string(a);
if(s[0]==‘1’) s[0]=‘9’;
else s[0]=‘1’;
cout<<s;
return 0;
}
You guys have used way too much brain to solve this. I got an AC by using this logic.
Instead of changing the last digit, I tried to change the first one. This solution is giving me a wrong answer. Can someone please help me out?
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long a,b;
cin >> a >> b;
long long ans = a-b;
if(!ans)
cout << 1 << "\n";
else{
long long temp = ans;
long long c = -1;
while(temp>0){
temp /= 10;
c++;
}
if(c==0){
if(ans == 1)
cout << 2 << "\n";
else
cout << 1 << "\n";
}
else{
if(ans/pow(10,c) == 9)
cout << ans - pow(10,c) << "\n";
else
cout << ans + pow(10,c) << "\n";
}
}
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf(“enter the value of a and b:”);
scanf("%d%d",&a,&b);
if((a-b)%10==9)
if(a-b==1)
break;
printf(\n a-b-1);
else
printf(\n a-b+1);
getch();
}
/* I don’t understand what is wrong with this code? */
#include<stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
int temp = a-b;
temp = temp/10;
int result = 0;
int power = 10;
int k;
while(temp)
{
k = temp%10;
result = result + kpower;
temp = temp/10;
power = power10;
}
printf("%d\n",result);
return 0;
}
For example consider the two numbers to be 1020 and 20
the difference will give you 1000
printing 1000 will be wrong
printing less than 1000 is wrong (no of digits is getting reduced)
0100 0010 0001 are wrong answers, because they have 0 infront of them.
So, what should be the answer in this case ?
#python script, one liner:
a,b=(int(x) for x in input().split(’ '));print(a-b+1 if (a-b)%10<9 else a-b-1)
the question is absolutely correct.
In your approach when a-b is 1 it will print 0 as answer and in constraints it is clearly written that answer should be a positive number.
gave answer on the question…
Codechef is the file name which u need to use
I don’t know why this solution is wrong:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll a,b;
cin >> a >> b;
ll x = a - b;
ll y = x % 10;
x = x / 10;
y = (y + 1)%10;
x = x * 10 + y;
cout << x << endl;
return 0;
}
But the following is correct:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll a,b;
cin >> a >> b;
ll x = a - b;
ll y = x % 10;
x = x / 10;
y = (y + 1)%10;
if(y == 0)
y ++;
x = x * 10 + y;
cout << x << endl;
return 0;
}
Why we need to divide A-B% 10
with dividing we can also get A-b -1 or A-b +1
#include<stdio.h>
int digit(int x)
{
int c=0;
while(x>0)
{
c++;
x=x/10;
}
return c;
}
int main()
{
int a,b,x;
scanf("%d %d",&a,&b);
x=a-b;
if(x>0)
{
if(digit(x)<digit(x-1))
x=x+1;
else if(digit(x)<digit(x+1))
x=x-1;
else
x=x+1;
}
else if(x==0)
x=x+1;
printf("%d\n",x);
return 0;
}
why my code is not accepting .It is accepeted in my laptop compiler,Please help
it could be 1001 or 1100 or 1010
Continuing the discussion from CIELAB - Editorial:
#include
using namespace std;
int main() {
long long a,b; cin>>a>>b;
int last_digit=(a-b)%10;
if(last==0 && last==1)
cout<<diff+1<<endl;
else cout<<diff-1<<endl;
return 0;
}
could anyone tell me what is mistake in my code??
You have not defined the variable last in your code but used it in if statement. Even the variable ‘diff’ is not defined.