qq_32575047 于 2016.02.21 17:02 提问

#include
#include
/** 本题要求计算给定的一系列正整数中奇数的和。

8 7 4 3 70 5 6 101 -1

116
int main()
{
int a[80];
int i,c = 0;
int sum=0;
for(i=0; ; i++)
{
scanf("%d ",&a[i]);
c++;
if(a[i]==0||a[i]<0)
{
break;

``````    }

}
for(i=0; i<=c; i++)
{
if(a[i]%2!=0)
{
sum += a[i];
}

}
printf("%d",sum);
return 0;
``````

}

5个回答

caozhy      2016.02.21 17:23

`````` int main()
{
int a[80];
int i = 0,c = 0;
int sum=0;
do
{
scanf("%d",&a[i]);
c++;
} while (a[i++]>0);
for(i=0; i<c-1; i++)
{
if(a[i]%2!=0)
{
sum += a[i];
}
}
printf("%d",sum);
return 0;
}
``````
caozhy      2016.02.21 17:23

8 7 4 3 70 5 6 101 -1
116Press any key to continue

Firas   2016.02.21 19:33

``````for(i=0; i<=c; i++)
``````

``````i<c
``````

``````#include <stdio.h>
int main() {
int sum = 0, input = 0;
while (1) {
scanf("%d", &input);
if (input <= 0) break;
if (input & 1) sum += input;
}
printf("\d", sum);
}
``````
qq_32575047 恩，这个方法真的不错，谢谢

mengyin521   2016.02.21 22:51

baidu_31886191   2016.02.21 18:52

#include "stdafx.h"
#include
#include
#include
#include"iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a[80];
int i, c = 0;
int number;
int sum = 0;
for (i = 0;; i++)
{
cin >> number;
a[i] = number;
c++;
if (a[i] == 0 || a[i]<0)
{
break;
}

``````}
for (i = 0; i <c-1; i++)
{
if (a[i] % 2 != 0)
{
sum += a[i];
}

}
printf("%d", sum);
return 0;
``````

}