我最近在班上进行了一次测试。其中一个问题如下:
给定一个数字n,用C/ c++编写一个函数,返回该数字的数字和的平方。(以下是重要的)。n的取值范围是[-(10^7),10^7]。示例:如果n = 123,函数应该返回14(1^2 + 2^2 + 3^2 = 14)。
这是我写的函数:
int sum_of_digits_squared(int n)
{
int s = 0, c;
while (n) {
c = n % 10;
s += (c * c);
n /= 10;
}
return s;
}
在我看来是这样的。所以现在测试回来了,我发现老师没有给我所有的分数,原因是我不明白。根据他的说法,为了使我的功能完整,我应该添加以下细节:
int sum_of_digits_squared(int n)
{
int s = 0, c;
if (n == 0) { //
return 0; //
} //
// THIS APPARENTLY SHOULD'VE
if (n < 0) { // BEEN IN THE FUNCTION FOR IT
n = n * (-1); // TO BE CORRECT
} //
while (n) {
c = n % 10;
s += (c * c);
n /= 10;
}
return s;
}
它的论点是数字n在[-(10^7),10^7]的范围内,所以它可以是一个负数。但是我不知道我自己版本的函数哪里失败了。如果我理解正确,while(n)的含义是while(n != 0),而不是while(n > 0),所以在我的函数版本中,数字n不会失败进入循环。这还是一样的。
Then, I tried both versions of the function on my computer at home and I got exactly the same answers for all the examples that I tried. So, sum_of_digits_squared(-123) is equal to sum_of_digits_squared(123) (which again, is equal to 14) (even without the detail that I apparently should've added). Indeed, if I try to print on the screen the digits of the number (from least to greatest in importance), in the 123 case I get 3 2 1 and in the -123 case I get -3 -2 -1 (which is actually kind of interesting). But in this problem it wouldn't matter since we square the digits.
那么,谁错了呢?
编辑:我的错,我忘了说明,也不知道这很重要。我们的类和测试中使用的C版本必须是C99或更新版本。所以我猜(通过阅读评论)我的版本无论如何都能得到正确答案。
问题陈述令人困惑,但数值示例澄清了数字平方的数字和的含义。以下是改进版:
在C和c++的公共子集中编写一个函数,该函数接受范围为[-107,107]的整数n,并返回以10为底的该表示的数字的平方和。例如:如果n是123,你的函数应该返回14(12 + 22 + 32 = 14)。
你写的函数很好,除了2个细节:
The argument should have type long to accommodate for all values in the specified range as type long is guaranteed by the C Standard to have at least 31 value bits, hence a range sufficient to represent all values in [-107, 107]. (Note that type int is sufficient for the return type, whose maximum value is 568.)
The behavior of % for negative operands is non-intuitive and its specification varied between the C99 Standard and previous editions. You should document why your approach is valid even for negative inputs.
以下是修改后的版本:
int sum_of_digits_squared(long n) {
int s = 0;
while (n != 0) {
/* Since integer division is defined to truncate toward 0
in C99 and C++98 and later, the remainder of this division
is positive for positive `n` and negative for negative
`n`, and its absolute value is the last digit of the
representation of `n` in base 10.
Squaring this value yields the expected result for both
positive and negative `c`. Dividing `n` by 10 effectively
drops the last digit in both cases.
The loop will not be entered for `n == 0`, producing the
correct result `s = 0`.
*/
int c = n % 10;
s += c * c;
n /= 10;
}
return s;
}
老师的回答有多个缺陷:
int类型的值范围可能不够。
不需要对值0进行特殊处理。
对负数求反是不必要的,对于n = INT_MIN可能有未定义的行为。
考虑到问题语句中的额外约束(C99和n的值范围),只有第一个缺陷是问题。额外的代码仍然产生正确的答案。
这次考试你应该能拿到好成绩,但是笔试时需要解释,以表明你对- n问题的理解,否则老师可能会认为你不知情,只是运气好。在口试中,你会得到一个问题,你的答案就能解决它。
总结一下评论中流传的讨论:
There is no good reason to test in advance for n == 0. The while(n) test will handle that case perfectly.
It's likely your teacher is still used to earlier times, when the result of % with negative operands was differently defined. On some old systems (including, notably, early Unix on a PDP-11, where Dennis Ritchie originally developed C), the result of a % b was always in the range [0 .. b-1], meaning that -123 % 10 was 7. On such a system, the test in advance for n < 0 would be necessary.
但第二颗子弹只适用于更早的时代。在C和c++标准的当前版本中,整数除法被定义为向0截断,因此即使n为负数,n % 10也保证会给出n的最后一位(可能是负数)。
因此,对于“while(n)的含义是什么?”这个问题的答案是“与while(n != 0)完全相同”,而对于“这段代码对于负n和正n是否都能正常工作?”的答案是“在任何现代的、符合标准的编译器下都能正常工作”。“那为什么老师要把它记下来?”这个问题的答案很可能是他们没有意识到1999年C语言和2010年左右c++发生了重大的语言重新定义。