2058: Find the Section

2058: Find the Section


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K569128Standard
In this task you are asked to deal with a lot of floating point numbers. Find the section in a number table that the sum of the numbers in it is the maximum.

Input

The first line of each test case is an integer N (<=100000). And the following lines have N floating point numbers. The last case have the N equal to zero, which you should not process.

Output

For each test case print "Case #T:" in the first line, where the T starts from 1. And the second line the sentence "The section from N1(th) to N2(th) have the max sum S", which means the numbers in the table from N1(th) to N2(th) have the sum S and S is the maximum you can find. N1, N2 are integers, and S is rounded to the nearest 0.001.

Note: If two sections have the same max sum, find the longer one and the one appears first. The length of the section can be zero which have a sum of zero, and it is from 0(th) to 0(th).

Sample Input

5
1 3.1 -2 7.35 -4
10
-5 -4 -3 -2 -1 0 1 2 3 4
0

Sample Output

Case #1:
The section from 1(th) to 4(th) have the max sum 9.450
Case #2:
The section from 6(th) to 10(th) have the max sum 10.000

 

Problem Source: coldcpp

#include<stdio.h>
int main()
{
    int n;
    int i,j;
    int l=1;
    while(scanf("%d",&n)==1&&n)
    {

        double a,s=1<<31,max=1<<31;
        int ti=1,tj=1,temp=1;
        for(i=1;i<=n;i++)
        {
            scanf("%lf",&a);
            if(s>=0) s+=a;//包含0 用'='
            else s=a,temp=i;
            if(s>max) max=s,ti=temp,tj=i;
            else if(s==max&&(i-temp>tj-ti)) ti=temp,tj=i;
        }
        if(max<0) max=ti=tj=0;
        printf("Case #%d:/n",l++);
        printf("The section from %d(th) to %d(th) have the max sum %.3lf/n",ti,tj,max);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值