C中偶数和奇数位数的总和

时间:2015-01-08 08:50:22

标签: c

我需要在整数中添加偶数和奇数位数。 Say,设数= 1234567.偶数位数之和= 2 + 4 + 6 = 12奇数位数之和= 1 + 3 + 5 + 7 = 16 感谢。

我目前的代码是:

int returnsum(int num) {
    while(num) {
        rem=num%10;
        sum=sum+rem;
        num=num/10);
    }
    while(sum) {
        a=sum%10;
        arr[i]=a:
        sum=sum/10; i++;
    }
    for(i=0;a[i];i+=2) {
        summ=summ+a[i];
    }
    return summ;
}

void main()
{
  int n,m,oddSum=0,evenSum=0;
  printf("Please insert the number for the program:");
  scanf("%d",&n);
  while (n!=0) {
   oddSum += n % 10;
   n /= 10;
   evenSum += n % 10;
   n /=10;
  }
 printf("Sum of digits in even  places:%d\n",evenSum);
 printf("Sum of digits in odd  places:%d\n",oddSum);
}

6 个答案:

答案 0 :(得分:2)

以下是您的问题的解决方案:

void main()
{
 int n,m,oddSum=0,evenSum=0;
 printf("Please insert the number for the program:");
 scanf("%d",&n);
 int flag=0;
 int counter=1;
 while (n!=0) {
   if(counter%2==0)
   {
     evenSum += n % 10;
     n /= 10;
   }
   else
   {
     oddSum += n % 10;
     n /= 10;
   }
   counter++;
 }

 if(counter%2==0)
 {
   int temp=oddSum;
   oddSum=evenSum;
   evenSum=temp;
 }
 printf("Sum of digits in even  places:%d\n",evenSum);
 printf("Sum of digits in odd  places:%d\n",oddSum);
}

答案 1 :(得分:0)

好的,让我们首先假设这个数字有一个偶数位数,所以倒数第二个和最后一个分别位于奇数和偶数位置。

然后,可以使用number % 10检索最后一位数,使用(number / 10) % 10检索最后一位数。

所以,知道这一点,你可以简单地循环数字,添加这些值并除以一百,直到得到一个小于十的数字。

如果该数字为零,那么您对具有偶数位数的原始数据的假设是正确的,您可以退出。

如果它是一个非零(1..9),那么你的假设是错误的,你应该将偶数和奇数之和换成日期,然后将最后一位数加到奇数和。

伪代码是这样的:

def getEvenOdd (num):
    even = 0
    odd = 0

    while num > 9:
        even = even + (num % 10)
        odd = odd + ((num / 10) % 10)
        num = num / 100

    if num > 0:
        temp = even
        even = odd
        odd = temp
        odd = odd +  num

    return (even,odd)

print getEvenOdd(1234567)

其输出类似于(12,16)


伪代码看起来像初学者的Python并不是偶然的,因为那种语言是完美的伪代码语言,只要你不要搞砸更复杂的角落。它

答案 2 :(得分:0)

检查以下代码:

#include <stdio.h>

int main(void){
char p[20];
int i=0,even=0,odd=0,n;
scanf("%d",&n);
sprintf(p,"%d",n);
while(p[i] != '\0')
{
    if(i%2 == 0)
    odd+= (p[i] - '0');
    else
    even+= (p[i] - '0');

    i++;
}
printf("%d\n",even);
printf("%d\n",odd);
return 0;
}

答案 3 :(得分:0)

我的五美分。:)

#include <stdio.h>

typedef struct pair
{
    unsigned int odd;
    unsigned int even;
} pair_t;   

pair_t GetSums( unsigned int x )
{
    const unsigned int Base = 10;
    pair_t sums = { 0u, 0u };
    size_t n = 0;

    do
    {
        if ( ++n & 1 ) sums.odd += x % Base;
        else sums.even += x % Base;
    } while ( x /= Base );

    if ( ( n & 1 )  == 0 )
    {
        unsigned int tmp = sums.odd;
        sums.odd = sums.even;
        sums.even = tmp; 
    }

    return sums;
}

int main(void) 
{
    while ( 1 )
    {
        unsigned int x;
        pair_t sums;

        printf( "\nEnter a non-negative number (0 - exit): " );
        if ( scanf( "%u", &x ) != 1 || x == 0 ) break;

        sums = GetSums( x );

        printf( "\nSum of digits in odd  positions is %u\n", sums.odd );            
        printf( "Sum of digits in even positions is %u\n", sums.even );         

    }

    return 0;
}

输出可能看起来像

Enter a non-negative number (0 - exit): 1234567

Sum of digits in odd  positions is 16
Sum of digits in even positions is 12

Enter a non-negative number (0 - exit): 123456

Sum of digits in odd  positions is 9
Sum of digits in even positions is 12

Enter a non-negative number (0 - exit): 0

答案 4 :(得分:0)

要对每个其他数字求和,只需在每次迭代中除以100:

int sumodd(int num) {
    int sum = 0, rem;
    while(num) {
        rem=num%10;
        sum=sum+rem;
        num=num/100);
    }
    return sum;
}

由于我们已经有一个可以相加其他数字的函数,我们可以重复使用它:

int sumeven(int num) {
    return sumodd(num/10);
}

答案 5 :(得分:-1)

#include<stdio.h>
void main()
{
    int a[10],n,j=0,x=0,y=0;

    for(int i=0;i<10;i++)
        a[i]=0;
    scanf("%d",&n);
    while(n)
    {
        a[j]=n%10;
        ++j;
        n/=10;
    }
    for(int i=0;i<10;i+=2)
        x+=a[i];
    for(int i=1;i<10;i+=2)
        y+=a[i];
    printf("%d\n",y);
    printf("%d",x); 
}