CIELAB - Editorial

PROBLEM LINKS

Practice
Contest

DIFFICULTY

EASY

EXPLANATION

This is the easiest problem in the set. This problem can be solved various methods. For instance, if A - B mod 10 = 9, print A - B - 1, otherwise print A - B + 1. Be careful not to print 0 if A - B = 1. Your answer must be a positive integer.

SETTER’S SOLUTION

Can be found here.

TESTER’S SOLUTION

Can be found here.

3 Likes

I’m not getting why my code is wrong.
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include<ctype.h>
#include
using namespace std;
int length(int n)
{int r,c=0;
while(n>0)
{
r=n%10;
c++;
n=n/10;
}
return c;
}
int main()
{
int a,b,n,s[10001],r,i,j,num;
cin>>a;
cin>>b;
num=a-b;
n=num;
i=0;
if(n!=0)
{
while(n>0)
{
r=n%10;
s[i]=r;
n=n/10;
i++;
}
while(1)
{
srand( (unsigned)time(NULL) );
j=(rand()%10)-1;
if(s[0]!=j)
s[0]=j;
break;
}
for(i=(length(num)-1);i>=0;i–)
cout<<s[i];
}
else
cout<<1;
system(“PAUSE”);
return 0;
}

#include
#include<conio.h>
using namespace std;

int main(){
int num;
int r;

int a;
int b;

	cin >> a;
	cin >> b;
	
	r = (a - b)%10;
	
	if(r <= 9 && r >= 1 ) {
	
	if(a - b - 1 == 0) {
		cout << "3" << endl;
		return 0;	
	}
	cout << a - b - 1 << endl;
	}
	else cout << (a - b) + 1 << endl;


getch();
return 0;

}

Why this code does not work??
class CielAB
{
public static void main(String args[])
{
Scanner inp=new Scanner(System.in);
String a=inp.nextLine();
String b[]=a.split(" ");
int a1=new Integer(b[0]);
int b1=new Integer(b[1]);
int c1=a1-b1;
if(c1%10==0)
c1=c1+1;
else
c1=c1-1;
System.out.println(c1);
}
}

The compiler really doesn’t take any other answer other than what is mentioned above.

Same problem can be solved as
if ((A-B)%10 ==0)
Output (A-B+1)
else
Output (A-B-1)

Please correct me if i am wrong.

#include
using namespace std;

int main()
{
int x,y,dif,flag=0,count1=0,count2=0,ans,d[5],an[5];
cout<<“enter the A and B:”;
cin>>x>>y;

if(x>y && x>0 && y>0 && x<10000 && y<10000)
{
    dif=x-y;
    int p=dif,q;
    while(p!=0)
    {
        p /=10;
        ++count1;
    }

    cout<<"\n enter your wrong answer=";
    cin>>ans;
    q=ans;
    while(q!=0)
    {

        q/=10;
        ++count2;
    }
    if(count1==count2)
    {
        for(int i=0;i<count1;i++)
        {
            d[i]=dif%10;
            dif=(dif-d[i])/10;
            an[i]=ans%10;
            ans=(ans-an[i])/10;

        }
       for(int i=0;i<count1;i++)
       {

           if(d[i]!=an[i])
           flag++;

       }
       if(flag==1)
        cout<<"\n wrong answer accepted";
       else
        cout<<"\n more than one digit error";

    }
    else{
        cout<<"\n no. of digit are difference";
    }

}
else{
cout<<"\n No are too large or too small";
}

return 0;

}

hi a am new on codechef and i solved the problem mentioned above in codeblock but it is not submitting here.

I have a question regarding the formula what does A-B mod 10 = 9 do? I’m a newbie here

3 Likes

#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t–)
{
int a,b,c,ans;
scanf("%d %d",&a,&b);
ans=fabs(a-b);
if((ans%10)==9)
ans=ans-1;
else
ans=ans+1;
printf("%d\n",ans);}
return 0;
}

For java,with what name to save the program

Following is my code in C#, but it says that answer is Wrong. I don’t understand why. Please Help.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeChef_CEILAB
{
class Program
{
    static void Main(string[] args)
    {
        string s = Console.ReadLine();
        string[] s1 = s.Split(' ');
        int A = int.Parse(s1[0]);
        int B = int.Parse(s1[1]);
        //Console.WriteLine("Enter First Number");
        //int A = int.Parse(Console.ReadLine());
        //Console.WriteLine("Enter Second Number");
        //int B = int.Parse(Console.ReadLine());
        var result = A - B;

        // First of all get the number of digits in result
        var ResCnt = digitCounter(result);
        var answer = result + 1;
        var AnsCnt = digitCounter(answer);
        if (ResCnt == AnsCnt)
            Console.WriteLine(answer);
        else
            Console.WriteLine(answer - 1);
    }

    static int digitCounter(int result)
    {
        var temp = result;
        int count = 1;
        while ((temp / 10) > 0)
        {
            count++;
            temp = temp / 10;
        }
        return count;
    }
}

}

Please let me know why the above code is wrong.
Thanks in advance!!

import java.io.*;
import java.util.Scanner;
import java.lang.Math;
class Ceil
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int difference = a-b;
int temp = difference;
int randomnumber = (difference%10);
while(randomnumber==(difference%10))
randomnumber = (int)((Math.random()*10));
int wrongnumber = 0;
//Changing only the last digit
wrongnumber = (((a-b)/10)*10)+randomnumber;
System.out.println(wrongnumber);
}
}
PS: Why is it not working?

Instead of changing first digit , i am changing last digit of resultant answer.
It is giving me Wrong Answer . Can you tell why?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/** Class for buffered reading int and double values */
class Reader
{
static BufferedReader reader;
static StringTokenizer tokenizer;

/** call this method to initialize reader for InputStream */
static void init(InputStream input) 
{
    reader = new BufferedReader(new InputStreamReader(input) );
    tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
    while ( ! tokenizer.hasMoreTokens() ) 
    {
        //TODO add check for eof if necessary
        tokenizer = new StringTokenizer(
               reader.readLine() );
    }
    return tokenizer.nextToken();
}

static int nextInt() throws IOException {
    return Integer.parseInt( next() );
}

static double nextDouble() throws IOException {
    return Double.parseDouble( next() );
}

}

class CIELAB
{
public static void main ( String[] args ) throws IOException
{
Reader.init(System.in);
int a = Reader.nextInt();
int b = Reader.nextInt();

	int res = a - b;
	int bb = res;
	boolean flag = false;
	
	while ( true )
	{
		int rem = bb%10;
		bb = bb/10;
		if ( bb >= 0 && bb <= 9 )
		{
			if ( rem == 9 )
			{
				flag = true;
			}
			break;
		}
	}
	if ( flag == true )
	{
		System.out.println(res-1);
	}
	else
	{
		System.out.println(res+1);
	}
}

}

Thanks in advance (Y)

You have a nice post please do share such post

Thanks sharing this question

It is very nice

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.

1 Like

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=x
10+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?

1 Like

#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;
}