UVA - 11059 Maximum Product-暴力枚举
生活随笔
收集整理的這篇文章主要介紹了
UVA - 11059 Maximum Product-暴力枚举
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
輸入n個元素組成的序列s,找出一個乘積最大的連續子序列,如果這個子序列不是整數,則輸出0.
解題思路:
枚舉起點和終點,把中間的數相乘,然后找到最大的結果。
代碼如下:
#include <iostream> using namespace std; typedef long long LL; const int N = 100; LL a[N];//開long longint main() {int cnt = 1;int n;while (cin >> n) {LL ans = 0;//放while里面,開long longfor (int i = 0; i < n; i++)cin >> a[i];for (int i = 1; i <= n; i++)for (int j = 0; j + i <= n; j++) {LL tmp = 1;for (int k = j; k < i + j; k++) {tmp *= a[k];}ans = max(ans, tmp);}printf("Case #%d: The maximum product is %lld.\n\n", cnt++, ans);//注意%lld,不要寫成%d}return 0; }總結
以上是生活随笔為你收集整理的UVA - 11059 Maximum Product-暴力枚举的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吃苏打饼干能减肥吗
- 下一篇: 高等数学上-赵立军-北京大学出版社-题解