数字总和程序没有给出c中负数的正确答案

时间:2016-06-14 03:31:39

标签: c

    printf("Sum Digit Program\n");
int n,re, sum = 0;
    printf("Enter an integer n="); scanf("%d", &n);
while(n){
    re = n % 10;
    sum = sum + re;
    n = n / 10;
}
printf("Sum digit = %d", sum);
return 0;
}

我尝试这个并且它适用于正整数,但是当我输入一个负整数时,如:-323 => -8 它应该是-3 + 2 + 3 = 2而不是-3-2-3 = -8 我尝试使用abs功能,但它仍然无法正常工作

5 个答案:

答案 0 :(得分:1)

好吧,您可以使用条件运算符来存储sign之类的int sign = (n >= 0 ? 1 : -1);值,如下所示 -

#include <stdio.h>
#include <stdlib.h>
/*
 * @brief   Logic for returning sum of digits
 */
int digi_sum(int n)
{
    int sign = (n >= 0 ? 1 : -1);
    int sum = 0;
    n *= sign;
    while (n)
    {
        if (n < 10)
            sum += (sign * (n % 10));
        else
            sum += n % 10;
        n /= 10;
        printf("Sum: %d, n: %d\n", sum, n);
    }
    printf("sum: %d, n: %d\n", sum, n);
    return sum;
}
/*
 * @brief   Driver function
 */
int main(int argc, char *argv[])
{
    int num = -323;
    printf("Sum: %d\n", digi_sum(num));
    return 0;
}

我们的想法是将数字的符号存储到一个单独的变量中,并在n < 10时使用它。

答案 1 :(得分:1)

OP几乎拥有它。只需将MSDigit视为已签名即可。所有其他数字,请使用abs(rem)。适用于INT_MIN

printf("Sum Digit Program\n");
int sum = 0;
printf("Enter an integer n="); 
scanf("%d", &n);
while (n) {
  int re = n % 10;
  n = n / 10;
  sum += n ? abs(re) : re; // At MSDigit when n==0
}
printf("Sum digit = %d", sum);

答案 2 :(得分:0)

你可以在循环的第一行添加一个条件,以确保sum到目前为止n < 10是正数,之后如果有while(n){ if (abs(n) < 10) { sum = abs(sum); } re = n % 10; sum = sum + re; n = n / 10; } 它将减去最小数字太。然后你的循环应该是这样的:

# -*- mode: python -*-

from kivy.deps import sdl2, glew

block_cipher = None

a = Analysis(['..\\WordCalc\\main.py'],
             pathex=['E:\\projects\\kivy\\calc\\app'],
             binaries=None,
             datas=None,
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)

exe = EXE(pyz, Tree('..\\WordCalc\\'),
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
          name='WordCalc',
          debug=False,
          strip=False,
          upx=True,
          console=False, icon='..\\WordCalc\\icon.ico')

答案 3 :(得分:0)

我认为您需要第一个号码的前提条件。 用if if else。 解决了 我更改了输出以查看值

include<stdio.h>

int main(void)
{
 int re,n;
 int sum =0 ;

 printf("Sum Digit Program \n");
 printf("Enter an integer n= "); 
 scanf("%d", &n);

 while(n)
 {
  if (abs(n) < 10) {
        sum = abs(sum);
  }       
 re= (n % 10);
 sum = sum + re; 
 n= n / 10; 

 printf ("\n  re =  %d , n=  %d \n",  re, n); 
 }   
 printf ("\n  sum=  %d \n",sum);
return 0;
}

答案 4 :(得分:0)

使用 n = abs(n / 10)代替 n = n / 10

#include <stdio.h>
#include <math.h>
main()
{
printf("Sum Digit Program\n");
int n,re, sum = 0;
printf("Enter an integer n="); scanf("%d", &n);
while(n)
    {
    re = n % 10;
    sum = sum + re;
    n =abs(n/10);
    }
printf("Sum digit = %d", sum);
return 0;
}