You are not logged in. Please login at www.codechef.com to post your questions!

×

CIELAB - Editorial

0
1

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.

This question is marked "community wiki".

asked 22 Nov '12, 16:23

admin's gravatar image

0★admin ♦♦
19.8k350498541
accept rate: 35%


I'm not getting why my code is wrong.

include <stdlib.h>

include <time.h>

include <stdio.h>

include<ctype.h>

include<iostream>

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

link

answered 16 Jan '13, 00:04

aditgupta100's gravatar image

2★aditgupta100
1
accept rate: 0%

include<iostream>

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;

}

link

answered 04 Feb '14, 19:42

gaurav_786's gravatar image

2★gaurav_786
211
accept rate: 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); } }

link

answered 18 Dec '15, 13:50

saksham30's gravatar image

2★saksham30
1
accept rate: 0%

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.

link

answered 11 Feb '16, 11:37

goldenvulture's gravatar image

0★goldenvulture
1
accept rate: 0%

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.

(16 May '18, 16:38) rajput19994★

include <iostream>

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.

link

answered 14 Jul '16, 10:57

santosh1995's gravatar image

2★santosh1995
1
accept rate: 0%

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

link

answered 08 Aug '16, 22:07

hajimekun's gravatar image

0★hajimekun
1
accept rate: 0%

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

link

answered 09 Sep '16, 18:07

manoj_1234's gravatar image

2★manoj_1234
1
accept rate: 0%

For java,with what name to save the program

link

answered 13 Sep '16, 19:23

yash990's gravatar image

0★yash990
1
accept rate: 0%

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!!

link

answered 24 Oct '16, 23:36

raisaurabh777's gravatar image

0★raisaurabh777
11
accept rate: 0%

edited 25 Oct '16, 00:25

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?

link

answered 09 Jan '17, 12:05

prahalad96's gravatar image

0★prahalad96
1
accept rate: 0%

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)

link

answered 01 Jun '17, 00:45

askwhat's gravatar image

2★askwhat
1
accept rate: 0%

You have a nice post please do share such post

link

answered 01 Jun '17, 10:55

priyamisra's gravatar image

0★priyamisra
-5
accept rate: 0%

Thanks sharing this question

link

answered 01 Jun '17, 10:57

priyamisra's gravatar image

0★priyamisra
-5
accept rate: 0%

It is very nice

link

answered 01 Jun '17, 10:59

priyamisra's gravatar image

0★priyamisra
-5
accept rate: 0%

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.

link

answered 13 Jul '17, 22:06

azazello's gravatar image

0★azazello
32
accept rate: 0%

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

link

answered 25 Jul '17, 14:30

synyst3r's gravatar image

2★synyst3r
1
accept rate: 0%

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?

link

answered 27 Aug '17, 13:19

pb2108's gravatar image

0★pb2108
1
accept rate: 0%

#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.

link

answered 07 Sep '17, 03:56

shivan111's gravatar image

2★shivan111
101
accept rate: 0%

edited 07 Sep '17, 03:57

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

answered 16 Mar '18, 18:54

the_red_sky's gravatar image

2★the_red_sky
1
accept rate: 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(); }

link
This answer is marked "community wiki".

answered 26 May '18, 19:13

mastermind_99's gravatar image

0★mastermind_99
1
accept rate: 0%

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

link

answered 31 May '18, 23:57

bhavana_t's gravatar image

1★bhavana_t
1
accept rate: 0%

gave answer on the question...

(01 Jun '18, 00:46) l_returns5★

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 ?

link

answered 04 Jan, 09:28

naveen_777's gravatar image

2★naveen_777
1
accept rate: 0%

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)

link

answered 20 Feb, 04:34

oussamaepau's gravatar image

0★oussamaepau
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,719
×3,770
×10
×6

question asked: 22 Nov '12, 16:23

question was seen: 12,863 times

last updated: 20 Feb, 04:34