C语言 C中偶数位和奇数位的数字总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27836116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sum of digits at Even and Odd Places in C
提问by Yagya
I need to add the digits on the even and odd places in an integer. Say, Let number = 1234567. Sum of even place digits = 2+4+6 = 12 Sum of odd place digits = 1+3+5+7 = 16
我需要在整数的偶数和奇数位置添加数字。假设数字 = 1234567。偶数位数字之和 = 2+4+6 = 12 奇数位数字之和 = 1+3+5+7 = 16
The code I currently have is:
我目前拥有的代码是:
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);
}
回答by sabbir.alam
Here is a solution for your problem:
这是您的问题的解决方案:
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);
}
回答by paxdiablo
Okay, let's first assume the number has an evennumber of digits, so that the second-last and last are at odd and even positions respectively.
好的,让我们首先假设该数字有偶数位数,因此倒数第二和最后一个分别位于奇数和偶数位置。
Then, the last digit can be retrieved with number % 10and the second last with (number / 10) % 10.
然后,可以使用 检索最后一位数字,使用 检索number % 10倒数第二个数字(number / 10) % 10。
So, knowing that, you can simply loop over the number, adding those values and dividing by a hundred until you get a number less than ten.
因此,知道这一点后,您可以简单地遍历该数字,将这些值相加并除以 100,直到得到一个小于 10 的数字。
If that number is zero, then your assumption about the original having an even number of digits was correct and you can exit.
如果该数字为零,那么您对原始数字为偶数的假设是正确的,您可以退出。
If it's a non-zero (1..9), then your assumption was wrong, you should swap the even and odd sums to date then add the final digit to the odd sum.
如果它是非零 ( 1..9),那么您的假设是错误的,您应该交换偶数和奇数的日期,然后将最后一位数字添加到奇数和。
The pseudo-code goes something like this:
伪代码是这样的:
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)
And the output of that would be something like (12,16).
其输出将类似于(12,16).
And it's no accident that pseudo-code looks like beginner's Python, since that language is the perfect pseudo-code language, provided you don't mess about with the more complex corners of it.
伪代码看起来像初学者的 Python 绝非偶然,因为该语言是完美的伪代码语言,前提是您不要弄乱它的更复杂的角落。
回答by Gopi
Check the below code:
检查以下代码:
#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] != '#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;
}
')
{
if(i%2 == 0)
odd+= (p[i] - '0');
else
even+= (p[i] - '0');
i++;
}
printf("%d\n",even);
printf("%d\n",odd);
return 0;
}
回答by Vlad from Moscow
My five cents.:)
我的五美分。:)
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
The output might look like
输出可能看起来像
int sumodd(int num) {
int sum = 0, rem;
while(num) {
rem=num%10;
sum=sum+rem;
num=num/100);
}
return sum;
}
回答by Klas Lindb?ck
To sum every other digit, simply divide by 100 in each iteration:
要对所有其他数字求和,只需在每次迭代中除以 100:
int sumeven(int num) {
return sumodd(num/10);
}
Since we already have a function that can sum every other digit, we can re-use it:
由于我们已经有了一个可以对所有其他数字求和的函数,我们可以重用它:
#include<stdio.h>
int main()
{
int n,odd=0,even=0;
scanf("%d",&n);
while(n)
{
odd+=n%10;
n/=10;
even+=n%10;
n/=10
}
printf("ODD SUM : %d\n",odd);
printf("EVEN SUM : %d\n",even);
}

