杭电ACM 1003-1004

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 207825    Accepted Submission(s): 48632


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.
 

Sample Input
   
   
   
   
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
 

Sample Output
   
   
   
   
Case 1: 14 1 4 Case 2:

7 1 6

import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int m=N;
        
        while(N-->0){
            int temp=1,max=-1001,sum=0,num,start=1,end=1;
            int numCount=sc.nextInt();
            for(int i=1;i<=numCount;i++){
                num=sc.nextInt();
                sum += num;
                if(sum>max){
                    max=sum;
                    start=temp;
                    end=i;
                }
                if(sum<0){
                    sum=0;
                    temp=i+1;
                }
            }//for
            System.out.println("Case "+(m-N)+":");
            System.out.println(max+" "+start+" "+end);
            if(N!=0)
                System.out.println("");
        }
    }

}

C++代码:

#include<iostream>
#include<string>
using namespace std;

int main(){
    int num,T;
    scanf("%d",&num);
    T=num;

    while(num--){
       int count,number,sum=0,temp=1,max=-1001,i,start,end;
       scanf("%d",&count);
       for(i=1;i<=count;i++){
           scanf("%d",&number);
           sum += number;
           if(sum > max){
              max=sum;
              start=temp;
              end=i;
           }
           if(sum < 0){
              sum=0;
              temp=i+1;
           }//if
       }//for
       printf("Case %d:\n",T-num);
       printf("%d %d %d\n",max,start,end);
       if(num)
           printf("\n");
    }
    return 0;
}


Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 102591    Accepted Submission(s): 39358


Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.
 

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
 

Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 

Sample Input
      
      
      
      
5 green red blue red red 3 pink orange pink 0
 

Sample Output
      
      
      
      
red pink
#include<iostream>
#include<string>
using namespace std;

int main(){
    int num;
    char strP[1000][16];
    int count[1000];
    int j,i;

    while(scanf("%d",&num)&& num!=0){
        for(i=0;i<1000;i++)
            count[i]=1;
        for(j=0;j<num;j++){
            cin>>strP[j];                 
        }
        for(i=0;i<num-1;i++){
            for(j=i+1;j<num;j++)
                if(!strcmp(strP[i],strP[j]))
                    count[i]++;
        }
        int max=0,sub;
        for(i=0;i<num;i++){
            if(count[i]>max){
                max=count[i];
                sub=i;
            }
        }
        printf("%s\n",strP[sub]);
    }
    return 0;
}



你可能感兴趣的:(ACM,杭电)