HDU 1003 : Max Sum

Problem Description

Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

C:

#include <stdio.h>  
int main()  
{  
    int z,n,max,sum;  
    int a,b,A,B,t;  
    scanf("%d",&z);  
    for(int k=1;k<=z;k++)  
    {  
        scanf("%d",&n);  
        sum = max = -1001;  
        for(int i=1;i<=n;i++)  
        {  
            scanf("%d",&t);  
            if(sum+t < t)  
                sum = t , a = b = i;      //a浪藻、b記錄當(dāng)前連續(xù)子序列的起始鲫懒、結(jié)束位置  
            else  
                sum += t , ++b;  
            if(max < sum)  
                max = sum , A = a , B = b;  
        }  
        printf("Case %d:\n%d %d %d\n",k,max,A,B);  
        if(k-z) puts("");  
    }  
    return 0;  
}  

C++:

#include <iostream>
#include <cstdio>

using namespace std;
const int inf = 0x7fffffff;

int main()
{
   int n, num, t, tt=1;
   bool first = true;

   scanf("%d", &t);
   while(t--)
   {
       scanf("%d", &n);
       int maxsum = -inf;
       int ans_s = 0, ans_e = 0;
       int cur = 1, sum = 0;

       for(int i=1; i<=n; i++)
       {
           scanf("%d", &num);
           sum += num;
           if(sum>maxsum)
           {
               maxsum = sum;
               ans_s = cur;
               ans_e = i;
           }
           if(sum<=0)
           {
               sum = 0;
               cur = i+1;
           }
       }
       if(first)
       {
           first = false;
           printf("Case %d:\n%d %d %d\n", tt++, maxsum, ans_s, ans_e);
       }
       else
       {
           printf("\nCase %d:\n%d %d %d\n", tt++, maxsum, ans_s, ans_e);
       }
   }
   return 0;
}

Java:

import java.util.Scanner;

public class Main {

    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        int T=s.nextInt();
        for(int i=0;i<T;i++){
            int n=s.nextInt();
            int sum=0,max=-1001;
            int start=0,end=0,z=0;
            for(int j=0;j<n;j++){
                int a=s.nextInt();
                sum=sum+a;
                if(max<sum){
                    max=sum;
                    end=j;
                    start=z;
                }
                if(sum<0){
                    sum=0;
                    z=j+1;
                }
            }
            System.out.println("Case "+(i+1)+":");
            System.out.println(max+" "+(start+1)+" "+(end+1));
            if(i<T-1)
                System.out.println();
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末伴网,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌盅粪,老刑警劉巖堪夭,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異惹想,居然都是意外死亡问词,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門嘀粱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)激挪,“玉大人,你說我怎么就攤上這事锋叨÷⒎郑” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵娃磺,是天一觀的道長(zhǎng)薄湿。 經(jīng)常有香客問我,道長(zhǎng)偷卧,這世上最難降的妖魔是什么豺瘤? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮听诸,結(jié)果婚禮上坐求,老公的妹妹穿的比我還像新娘。我一直安慰自己晌梨,他們只是感情好瞻赶,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布赛糟。 她就那樣靜靜地躺著,像睡著了一般砸逊。 火紅的嫁衣襯著肌膚如雪璧南。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天师逸,我揣著相機(jī)與錄音司倚,去河邊找鬼。 笑死篓像,一個(gè)胖子當(dāng)著我的面吹牛动知,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播员辩,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼盒粮,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了奠滑?” 一聲冷哼從身側(cè)響起丹皱,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎宋税,沒想到半個(gè)月后摊崭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡杰赛,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年呢簸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乏屯。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡根时,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出辰晕,到底是詐尸還是另有隱情蛤迎,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布伞芹,位于F島的核電站,受9級(jí)特大地震影響蝉娜,放射性物質(zhì)發(fā)生泄漏唱较。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一召川、第九天 我趴在偏房一處隱蔽的房頂上張望南缓。 院中可真熱鬧,春花似錦荧呐、人聲如沸汉形。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)概疆。三九已至逗威,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間岔冀,已是汗流浹背凯旭。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留使套,地道東北人罐呼。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像侦高,于是被迫代替她去往敵國(guó)和親嫉柴。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容