zoukankan      html  css  js  c++  java
  • Uva11059 最大乘积

    Uva11059 最大乘积

    题目描述:

    输入n个元素组成的序列S,你需要找出一个乘积最大的连续子序列。如果这个最大的乘积不是正数,应输出0(表示无解)。

    思路:

    记录起点和终点,两遍循环找到这个最大的值。这里子序列的最小长度为1。

    代码:
    #include <iostream>
    #include <limits.h>
    #define LL long long
    using namespace std;
    const int maxn = 20;
    int nums[maxn];
    
    LL solve(int nums[], int i, int j){
    	LL res = 1;
    	while(i <= j){
    		res = res * (LL)nums[i];
    		++i;
    	}
    	return res;
    }
    
    int main(){
    	//freopen("uva11059_in.txt", "r", stdin);
    	//freopen("uva11059_out.txt", "w", stdout);
    	int n; int kase = 0;
    	while(cin >> n){
    		++kase;
    		LL max = INT_MIN;
    		LL res;
    		for(int i = 0; i < n; ++i){
    			scanf("%d", &nums[i]);
    		}
    		for(int i = 0; i < n; ++i){
    			for(int j = i; j < n; ++j){
    				res = solve(nums, i, j);
    				if(res > max) {max = res;}  
    			}
    		}
    		if(max < 0) printf("Case #%d: The maximum product is 0.
    ", kase);
    		else printf("Case #%d: The maximum product is %lld.
    ", kase, max);
    		printf("
    ");
    	}
    } 
    
  • 相关阅读:
    为上次写的框架加了一个辅助功能
    复制文件夹下所有文件
    进制之间的相互转换
    c# 修改appConfig文件节点
    GUID
    太悲哀了
    poj2411 Mondriaan's Dream
    poj3311 Hie with the Pie
    HDU3001 Travelling
    luogu p2622关灯问题II
  • 原文地址:https://www.cnblogs.com/patrolli/p/11370253.html
Copyright © 2011-2022 走看看