例如数组元素的值依次为: 1 , 8 , 2 , 3 , 11 , 6 ;则奇数值之和 15 、偶数值之和 16 。函数原型: void findsum( int *pa , int n, int *podd, int *peven ) 。参数说明: pa 是整型数组的首地址, n 是数组元素个数(元素个数不大于100),指针 podd 、 peven 保存奇数之和、偶数之和的整型变量的地址。返回值:无返回值。预设代码findsum_48.cview plaincopy to clipboardprint?/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */#include <stdio.h>void findsum( int *pa, int n, int *podd, int *peven );main( ){int i, n, a[100], odd, even;scanf( "%d",&n );for( i=0; i<n; i++ )scanf("%d",&a[i] );findsum( a, n, &odd, &even );printf("odd=%d,even=%d\n", odd, even );}/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
2 回答
蓝山帝景
TA贡献1843条经验 获得超7个赞
#include <stdio.h>
void findsum( int *pa, int n, int *podd, int *peven );
int main( )
{
int i, n, a[100], odd, even;
scanf( "%d",&n );
for( i=0; i<n; i++ )
scanf("%d",&a[i] );
findsum( a, n, &odd, &even );
printf("odd=%d,even=%d\n", odd, even );
}
void findsum( int *pa, int n, int *podd, int *peven )
{
int i;
*podd=0;
*peven=0;
for(i=0;i<n;i++)
{
if(pa[i]%2)
*podd+=pa[i];
else
*peven+=pa[i];
}
}
潇潇雨雨
TA贡献1833条经验 获得超4个赞
void findsum( int *pa, int n, int *podd, int *peven )
{
int i = 0;
int even = 0, odd = 0;
while (i < n)
{
if (*(pa+i) % 2 == 0)
{
even += *(pa+i);
}
else
{
odd += *(pa+i);
}
++i;
}
*podd = odd;
*peven = even;
}
添加回答
举报
0/150
提交
取消
