UVa 11059 Maximum Product(简单枚举7.1)使用longlong,输出格式%lld

这题数据最大18位,应该用Long long 粗心在几个地方(函数返回值,中间比较值max pro)用了Int,提交了好几次

#include<stdio.h>

long long func(int *a,int head,int rear){//一开始用了int
    long long pro=a[head];//注意这里初值不能赋值为1 2\n 0 0这个通不过
    if(head==rear)
    return a[head];
    else
    for(int i=head+1;i<=rear;i++)
    pro*=a[i];
    return pro;
}

int main(){
    //freopen("test.out","w",stdout);
    int n,count=1;
    long long pro;//一开始用int 细心啊
    while(scanf("%d",&n)!=EOF){
        int a[n];
        long long max=0;
        for(int i=0;i<n;i++){
            scanf("%d",a+i);
        }
        for(int head=0;head<n;head++){
            for(int rear=head;rear<n;rear++){//bug?
            pro=func(a,head,rear);
            if(max<pro){
            max=pro;
            //printf("%d %d %d\n",head,rear,max);
            }
            }
        }

        if(max<0)
        max=0;
        printf("Case #%d: The maximum product is %lld.\n\n",count++,max);
    }
    return 0;
} 
时间: 2024-10-08 04:52:08

UVa 11059 Maximum Product(简单枚举7.1)使用longlong,输出格式%lld的相关文章

UVA 11059 Maximum Product(枚举start+end)

题目大意: 就是说给你n个数字,然后求出连续序列的最大和.如果ans为负数就输出0,要么就输出这个ans. 解题思路: 其实我们只需要枚举起点和终点,然后考虑所有的情况就可以了,有点类似于求解最大连续和的问题. 唯一的不同就是每次都要初始化t = 1, t*=a[j].注意long long.因为最多有18个数字,且每个数字的 最大值为10,那么他们的乘积是不会超过10^18的. 代码: # include<cstdio> # include<iostream> # include

UVa 11059 Maximum Product

题意:给出n个数组成的序列,求乘积最大的连续子序列 看的紫书,因为n最大为18,每个数最大为10,所以10^18用long long 能够存下, 直接枚举起点和终点找最大值就可以了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include&l

UVA OJ-11095 Maximum Product(暴力求解法)

Given a sequence of integers S = {S1, S2, ..., Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you should consider 0 as the value of the maximum pro

UVA 10976 Fractions Again?! 简单枚举题

#include<stdio.h> struct pairs{ int x,y; }; struct pairs pairs[10000]; int judge(int k,int i){ if((k*i)%(i-k)==0) return 1; else return 0; } int main(){ int k,count; while(scanf("%d",&k)!=EOF){ count=0; for(int i=k+1;i<=2*k;i++){ if

uva 10976 Fractions Again(简单枚举)

10976 Fractions Again It is easy to see that for every fraction in the form 1 k (k > 0), we can always find two positive integers x and y, x ≥ y, such that: 1 k = 1 x + 1 y Now our question is: can you write a program that counts how many such pairs

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

UVa 725 Division --- 简单枚举

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666 /* UVa 725 Division --- 简单枚举 */ #include <cstdio> #include <cstring> bool used[10]; /* 判断传进来的两个数是否满足条件 */ bool judge(int a, i

UVA - 10167 - Birthday Cake (简单枚举)

思路:简单枚举 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int x[105], y[105]; int main() { int A, B, N; while(scanf("%d", &N), N) { for(int

UVA - 108 - Maximum Sum (简单贪心)

UVA - 108 Maximum Sum Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension.