Thursday, 10 November 2016

Ciel and A-B Problem

Problem Statement -
In Ciel's restaurant, a waiter is training. Since the waiter isn't good at arithmetic, sometimes he gives guests wrong change. Ciel gives him a simple problem. What is A-B (A minus B) ?
Surprisingly, his answer is wrong. To be more precise, his answer has exactly one wrong digit. Can you imagine this? Can you make the same mistake in this problem?

Input

An input contains 2 integers A and B.

Output

Print a wrong answer of A-B. Your answer must be a positive integer containing the same number of digits as the correct answer, and exactly one digit must differ from the correct answer. Leading zeros are not allowed. If there are multiple answers satisfying the above conditions, anyone will do.

Constraints

1 ≤ B < A ≤ 10000

Sample Input

5858 1234

Sample Output

1624

Output details

The correct answer of 5858-1234 is 4624. So, for instance, 2624, 4324, 4623, 4604 and 4629 will be accepted, but 0624, 624, 5858, 4624 and 04624 will be rejected.

Notes

The problem setter is also not good at arithmetic.

Problem link :Ciel


The idea : My idea is to change the last digit . Since it is the digit that is easiest to access. If the last digit is 2 change it to 1 by subtracting 1 from it. If it is not 2 change it to 2.

The Code:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
scanf("%d %d",&a,&b);
int diff=a-b;
if(diff%10==2) // checks if the last digit is 2
{
diff--;
}
else
{
diff=(diff/10)*10+2; // Changes it to 2
}
cout<<diff<<endl;
return 0;
}

 


 

6 comments:

  1. #include
    int main(){
    int i,j,k;
    scanf("%d%d",&i,&j);
    k = i-j;
    if(k%10!=9) k++; else k--;
    printf("%d\n",k);
    return 0;
    }
    Bro is me if(k%10!=9) k++; else k--; kiyu karre ??

    ReplyDelete
    Replies
    1. aap ka answer correct dikhaya but mera wrong dikha raha.. can you checkout once??
      #include
      using namespace std;
      int main()
      {
      int a,b,s;
      cin>>a>>b;
      s=a-b;
      if(s%10==0)
      cout<<s+1;
      else
      cout<<s-1;

      return 0;
      }

      Delete
  2. @Akshat bhatt because we want same number of digit as in orignal ans-->eg-->if we do 9999%10 we get 9 so if we plus 1 in 9999 we get 10000 in number of digit in 9999 is 4 and number of digit in 10000 is 5 which is not equal.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete