Do I need to explicitly handle negative numbers or zero when summing squared digits?

前端 未结 9 1209
無奈伤痛
無奈伤痛 2021-01-30 01:51

I recently had a test in my class. One of the problems was the following:

Given a number n, write a function in C/C++ that returns the su

9条回答
  •  北海茫月
    2021-01-30 02:22

    The problem statement is confusing, but the numerical example clarifies the meaning of the sum of the digits of the number squared. Here is an improved version:

    Write a function in the common subset of C and C++ that takes an integer n in the range [-107, 107] and returns the sum of the squares of the digits of its representation in base 10. Example: if n is 123, your function should return 14 (12 + 22 + 32 = 14).

    The function that you wrote is fine except for 2 details:

    • 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.

    Here is a modified version:

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

    The teacher's answer has multiple flaws:

    • type int may have an insufficient range of values.
    • there is no need to special case the value 0.
    • negating negative values is unnecessary and may have undefined behavior for n = INT_MIN.

    Given the extra constraints in the problem statement (C99 and range of values for n), only the first flaw is an issue. The extra code still produces the correct answers.

    You should get a good mark in this test, but the explanation is required in a written test to show your understanding of the issues for negative n, otherwise the teacher may assume that you were unaware and just got lucky. In an oral exam, you would have gotten a question and your answer would have nailed it.

提交回复
热议问题