qfedu-basic-level/day8/homework/h5.cpp

30 lines
558 B
C++
Raw Permalink Normal View History

2023-06-24 10:01:33 +08:00
// 输入一个整数 n然后输入 n 个整数,输出其中所有奇数的和。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int sum = 0;
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 2 == 1)
sum += nums[n];
}
cout << "输入的整数中所有奇数的和为: " << sum << endl;
return 0;
}