简体   繁体   中英

What is wrong with my solution for code check problem CIELAB

I am doing a code chef practice prblem https://www.codechef.com/problems/CIELAB in easy category. But my solution is not working. The submit screen is showing : Status wrong Answer

#include <iostream>

using namespace std;

int input ();
int difference (int, int);
int calculateWrongAns (int);

int main()
{
    int num1;
    int num2;
    num1 = input();
    num2 = input();

    int actualAns = difference(num1, num2);
    int res = calculateWrongAns(actualAns);
    cout << res;
    return 0;
}

int input () {
    int x;
    cin >> x;
    return x;
}

int difference (int x, int y) {
    if (x > y) {
        return x - y;
    } else if (x < y) {
        return y - x;
    } else {
        return 0;
    }
}

int calculateWrongAns (int actualAns) {
    int lastNumber = actualAns % 10;
    int otherNumbers = actualAns / 10;
    int res;
    if (otherNumbers != 0) {
        res = (otherNumbers * 10) + (lastNumber == 1 ? 2 : lastNumber -     1);
    } else {
        res = lastNumber == 1 ? 2 : lastNumber - 1;
    }

    return res;
} 

Thanks in advance.

The line

res = lastNumber == 1 ? 2 : lastNumber - 1;

is wrong. You are splitting the result in last digit and other digits. If the last digit is 1, you are changing it to 2. If it not 1, you are subtracting 1. That means, if the result is 30, you are subtracting 1. The new result is 29. Two digits differ. That's not correct. You could use

int calculateWrongAns (int actualAns) {
    return (actualAns % 10) == 0 ? actualAns + 1 : actualAns - 1;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM