CIELAB - Editorial

#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 = power
10;
}
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.

1 Like

gave answer on the question…

1 Like

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.

[quote=“dinu_maan5, post:33, topic:943”]
#include
using namespace std;

int main() {
long long a,b; cin>>a>>b;
int diff=(a-b);
int last_digit=(a-b)%10;
if(last_digit==0 && last_digit==1)
cout<<diff+1<<endl;
else cout<<diff-1<<endl;
return 0;
}
sorry dude previous mistake but now could you tell me mistake in my code. it’s o/p is wrong answer

Sorry for late reply, I was busy somewhere.
It should be

if(last_digit == 9){
diff -= 1;
}
else{
diff += 1;
}

because when last digit is 9 and if you add 1 to it then the number of digits will get changes which we are not allowed to do.

The answer can be 1001, 1100, 1010

What’s wrong with this?

#include
#include
using namespace std;

int main()
{
int number1, number2, dec=1, diff;
cin >> number1 >> number2;

int sub = number1-number2;
int mod = sub % 10;
if(mod == 0){
    mod=mod+1;
    diff = sub + mod;
}
else{
    diff = sub - mod;
}
if(diff == 0){
    diff = diff+1;
}
cout << diff;

}

@amit1_k2011 Hey there if you consider test case :

a = 2 , b = 1

diff = a - b = 1
mod = 1

And according to your logic it will first go to else block as mod != 0
so in that case  
diff = diff - mod = 0

Now you have put if condition for diff == 0 according to this : 

diff = diff + 1 = 1

So finally you are printing this diff which is 1 and our original difference 
was also same. So your logic fails here.

This is how you should debug your program , if your program is of less then 60 lines then debug it by going line by line. You will definitely get the problem this way.

Thankyou

#include<bits/stdc++.h>
using namespace std;
int main() {
int A,B;
cin>>A>>B;
int res=A-B;
if(res%10==0) {
res++;
cout<<res;
}
else if(res%10==9) {
res–;
cout<<res;
}
else {
res++;
cout<<res;
}
return 0;
}

Try this one broo

Its not working why?
#include
using namespace std;

int main()
{
int a,b;
cin>>a>>b;
int m=a-b;
int ch=m%10;m=m/10;
for(int i=0;i<10;i++)
{
if(ch==i)
continue;
else
{
m=m*10+i;break;}
}
cout<<m;
return 0;
}