Ciel and A-B Problem | CodeChef Solution

Hello coders, today we are going to solve Ciel and A-B Problem CodeChef Solution whose Problem Code is CIELAB.

Ciel and A-B Problem

Contents

Task

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 AB (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 Format

An input contains 2 integers A and B.

Output Format

Print a wrong answer of AB. 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.

Solution – Ciel and A-B Problem

C++

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int a,b;cin>>a>>b;
	int ans=abs(b-a);
	int temp=ans%10;
	if(temp==9) temp=7;
	int t=ans/10;
	cout<<t*10+temp+1<<endl;
}

Python

a,b = map(int,input().split())
x=str(a-b)
if x[0]=='4':
  print(int('3'+x[1:]))
else:
  print(int('4'+x[1:]))

Java

import java.util.*;
public class Main
{
	
	
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt();
		int b=sc.nextInt();
		int temp=0,count=0;
		temp=a-b;
		while(temp!=0){
			temp=temp/10;
			count++;
		}
		temp=a-b;
		int[] arr=new int[count];
		 for(int i=count-1;i>=0;i--){
			 if(temp>0){
				 arr[i]=temp%10;
				 temp=temp/10;
			 }
		 }
		 switch(arr[0]){
			 case 1:
				 arr[0]=2;
				 break;
			 case 2:
				 arr[0]=3;
				 break;
		     case 3:
				 arr[0]=4;
				 break;
			 case 4:
				 arr[0]=5;
				 break;
			 case 5:
				 arr[0]=6;
				 break;
			 case 6:
				 arr[0]=7;
				 break;
			 case 7:
				 arr[0]=8;
				 break;
			 case 8:
				 arr[0]=9;
				 break;
			 case 9:
				 arr[0]=1;
				 break;
			}
				  
			for(int t:arr){
				System.out.print(t);
			}
		}
	
	
}

Disclaimer: The above Problem (Ciel and A-B Problem) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.