如果我输入负数,我应该通过忽略否定答案得到输出? 如果用户输入负数,你可以忽略它的符号,并考虑其相应的正值。如果用户输入零,则程序应终止 #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; }

如果我输入负数,我应该通过忽略否定答案得到输出? 如果用户输入负数,你可以忽略它的符号,并考虑其相应的正值。如果用户输入零,则程序应终止 #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; },c,algorithm,C,Algorithm,如果要忽略某个数字的符号,请将其移动到另一个数字并强制该符号为正: temp = (input < 0) ? -input : input; temp=(输入0){ //从temp中提取最后一位数字,并将其作为倒数第二位添加 //将现有数字向左移动一个位置 反向=反向*10+温度%10; temp=temp/10;//删除最后一个数字,并将其他数字向右移动 } if(uinput==reverse)//检查输入和反向是否相同 printf(“数字是回文\n”); 其他的 printf(“

如果要忽略某个数字的符号,请将其移动到另一个数字并强制该符号为正:

temp = (input < 0) ? -input : input;
temp=(输入<0)-输入:输入;
这是简洁的三元版本,但如果您愿意,您可以恢复到更经典的版本:

if (input < 0) {
    temp = -input;
} else {
    temp = input;
}
if(输入<0){
温度=-输入;
}否则{
温度=输入;
}

您可以测试数字是否为负数,并取其相反的值。只有一种特殊情况,
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;
}
#包括
int main(){
长输入;
无符号长输入;
无符号长反转=0;
无符号长温;
printf(“输入输入号码:”);
if(scanf(“%ld”,&input)!=1)//从用户处读取输入
返回1;
如果(输入>=0){
输入=输入;
}否则{
uinput=-(无符号长)输入;
}
温度=输出;
而(温度>0){
//从temp中提取最后一位数字,并将其作为倒数第二位添加
//将现有数字向左移动一个位置
反向=反向*10+温度%10;
temp=temp/10;//删除最后一个数字,并将其他数字向右移动
}
if(uinput==reverse)//检查输入和反向是否相同
printf(“数字是回文\n”);
其他的
printf(“数字不是回文\n”);
返回0;
}

只需分配
input=abs(input)
这大概是学校作业或类似作业。在这种情况下,你应该如何处理负数取决于赋值是什么。当然字符串“-121”不是回文,因为它与它的反面“121-”不同。但是,如果作业要求忽略该符号,那么您应该忽略该符号。bug的定义是程序偏离其规范。因此,要知道您的程序应该做什么,我们需要知道它的规范是什么。在
LONG\u MIN
情况下,
-input
在分配给
uinput
之前溢出。当然,
scanf(“%ld”,…)
在此之前可能会溢出,因为在
long
中无法表示输入值。
#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;
}