题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2006
题目大意:给你几个数,求奇数的乘积和
解题思路:
很水,不需要数组的,一个变量 x 就行
代码:
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int sum; 6 int n; 7 int a[100]; 8 while(cin >> n) 9 { 10 sum = 1; 11 for(int i = 0; i < n; i ++) 12 { 13 cin >> a[i]; 14 if(a[i] % 2 != 0) 15 sum *= a[i]; 16 } 17 cout << sum << endl; 18 19 } 20 }
优化:
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int sum; 6 int n; 7 int x; 8 while(cin >> n) 9 { 10 sum = 1; 11 for(int i = 0; i < n; i ++) 12 { 13 cin >> x; 14 if(x % 2 != 0) 15 sum *=x; 16 } 17 cout << sum << endl; 18 19 } 20 }
原文地址:https://www.cnblogs.com/mimangdewo-a1/p/9360639.html
时间: 2024-10-01 00:37:57