簡體   English   中英

我的代碼檢查問題 CIELAB 的解決方案有什么問題

[英]What is wrong with my solution for code check problem CIELAB

我正在做一個簡單類別的代碼廚師練習問題https://www.codechef.com/problems/CIELAB 但我的解決方案不起作用。 提交屏幕顯示:狀態錯誤答案

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

提前致謝。

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

是錯的。 您將結果拆分為最后一位數字和其他數字。 如果最后一位是 1,則將其更改為 2。如果不是 1,則減 1。這意味着,如果結果為 30,則減 1。新結果為 29。兩位數不同。 那不正確。 你可以用

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