简体   繁体   English

我的代码检查问题 CIELAB 的解决方案有什么问题

[英]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.我正在做一个简单类别的代码厨师练习问题https://www.codechef.com/problems/CIELAB 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.如果最后一位是 1,则将其更改为 2。如果不是 1,则减 1。这意味着,如果结果为 30,则减 1。新结果为 29。两位数不同。 That's not correct.那不正确。 You could use你可以用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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