2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
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 DescriptionContest 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.
InputInput 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.
OutputFor 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 Input5 green red blue red red 3 pink orange pink 0
Sample Outputred 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; }