[英]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.