【问题标题】:If I enter negative number I should get the output by ignoring negative answer?如果我输入负数,我应该通过忽略否定答案来获得输出?
【发布时间】:2020-11-14 14:35:12
【问题描述】:

如果用户输入一个负数,你可以忽略它的符号并考虑它对应的正值。如果用户输入零,那么程序应该终止。

#include <stdio.h>

int main() {
    long input;
    long reverse = 0;
    long temp;
    printf("Enter the input number: ");
    scanf("%ld", &input); //Read input from user
    temp = input;         //Save input to another variable
    while (temp > 0) {
        // extract last digit from temp and add it as last digit in reverse
        // by moving existing digits one position towards left
        reverse = reverse * 10 + (temp % 10);
        temp = temp / 10; // removes last digit and move other digits to right
    }
    if (input == reverse) // check if input and reverse are same
        printf("Number is a palindrome\n");
    else
        printf("Number is not palindrome\n");
    return 0;
}

输出是这样的

Enter an integer number: 12321
12321 is a Palindrome.
Enter an integer number: 2312
2312 is not a Palindrome.
Enter an integer number: -45566554
45566554 is a Palindrome.
Enter an integer number: t
Enter an integer number: 36298100189263
36298100189263 is a Palindrome.
Enter an integer number: 362981089263
362981089263 is not a Palindrome.
Enter an integer number: 0
Goodbye! 

【问题讨论】:

  • 只需分配input = abs(input)
  • 大概这是学校作业或类似的事情。在这种情况下,您应该如何处理负数取决于分配的内容。当然,字符串“-121”不是回文,因为它与其相反的“121-”不同。但是,如果作业说要忽略该符号,那么您应该忽略该符号。错误的定义是程序与其规范的偏差。所以要知道你的程序应该做什么,我们需要知道它的规范是什么。

标签: c algorithm


【解决方案1】:

如果你想忽略一个数字的符号,将它移到另一个数字并强制符号为正:

temp = (input < 0) ? -input : input;

这是简洁的三元版本,但如果您愿意,可以恢复为更经典的版本:

if (input < 0) {
    temp = -input;
} else {
    temp = input;
}

【讨论】:

    【解决方案2】:

    您可以测试该数字是否为负并取其相反。只有一种特殊情况,LONG_MIN,可能没有积极的对应物。使用无符号算术可以优雅地处理这种特殊情况:

    #include <stdio.h>
    
    int main() {
        long input;
        unsigned long uinput;
        unsigned long reverse = 0;
        unsigned long temp;
        printf("Enter the input number: ");
        if (scanf("%ld", &input) != 1) // Read input from user
            return 1;
        if (input >= 0) {
            uinput = input;
        } else {
            uinput = -(unsigned long)input;
        }
        temp = uinput;
        while (temp > 0) {
            // extract last digit from temp and add it as last digit in reverse
            // by moving existing digits one position towards left
            reverse = reverse * 10 + temp % 10;
            temp = temp / 10; // removes last digit and move other digits to right
        }
        if (uinput == reverse) // check if input and reverse are same
            printf("Number is a palindrome\n");
        else
            printf("Number is not palindrome\n");
        return 0;
    }
    

    【讨论】:

    • LONG_MIN 的情况下,-input 在分配给uinput 之前溢出。当然scanf("%ld",…) 可能会在此之前溢出,因为输入值在long 中无法表示。
    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 2022-12-07
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多