hdu 5504(枚举+找规律)
生活随笔
收集整理的這篇文章主要介紹了
hdu 5504(枚举+找规律)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5504
官方題解:
注意先特判0?的情況:如果讀入的數據有0?,那么去掉所有的0?且最后答案和0?取一個max。剩下的正數顯然全部乘起來比較優。對于負數的話,如果個數是奇數個我們就去掉絕對值最小的那一個,然后全部乘起來即可。 ?
這題坑很多,要討論的情況很多。。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std;typedef long long LL; const int maxn = 65; int n; LL a[maxn];bool cmp(int a,int b) {return a > b; }int main() {int t;scanf("%d",&t);while(t--){int i,zero = 0,pos = 0,neg = 0;scanf("%d",&n);for(i = 1; i <= n; i++){scanf("%lld",&a[i]);if(a[i] == 0) zero++;if(a[i] > 0) pos++;if(a[i] < 0) neg++;}if(n == 1 || zero == n) {printf("%lld\n",a[1]);continue;}sort(a+1,a+1+n,cmp);LL ans = 1;if(pos == 0){}for(i = 1; i <= n; i++){if(a[i] > 0)ans *= a[i];if(a[i] < 0) break;}int num = n - i + 1;if(num % 2 == 1) //奇數個負數{if(num == 1 && pos == 0) ans = a[n];for(int j = n; j > i; j--)ans *= a[j];}else //偶數個負數{for(int j = i; j <= n; j++)ans *= a[j];}if(zero && ans < 0) ans = 0;printf("%lld\n",ans);}return 0; }與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的hdu 5504(枚举+找规律)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【版本发布】Jeecg-P3 1.0 发
- 下一篇: Activiti工作流之事件监听详解-A