UVa 11059 Maximum Product 例题(7-2)

    xiaoxiao2024-12-20  2

    思路:枚举起点和终点,同时对比最大值

    代码:

    <span style="font-size:14px;">#include<iostream> #include<cstdio> #include<algorithm> using namespace std; long long n,kase = 0,num[25]; void solve() { long long maxn; for(int i =0;i<n;i++) cin>>num[i]; maxn = num[0]; for(int i = 0;i<n;i++) { maxn = max(maxn,num[i]); long long sum = num[i],tmp = 0; for(int j = i+1;j<n;j++) { sum *= num[j]; maxn = max(maxn,sum); } } if (maxn<0) maxn = 0; cout<<"Case #"<<kase<<": The maximum product is "<<maxn<<"."<<endl; } int main() { //freopen("std.in","r",stdin); //freopen("std.out","w",stdout); while(cin>>n&&n) { kase++; solve(); cout<<endl; } }</span>

    转载请注明原文地址: https://ju.6miu.com/read-1294803.html
    最新回复(0)