# 杭电ACM1003,1004,1005 java解答

## 1003 .

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.

``````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();
}
}
}
``````

## 1004 .

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.

``````import java.util.ArrayList;
import java.util.Scanner;

public class Main {

public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n;
while((n=Integer.parseInt(s.nextLine()))!=0){
ArrayList<String> color=new ArrayList<String>();
int number[]=new int[100];
int k=0;
for(int i=0;i<n;i++){
String str=s.nextLine();
int judge=color.indexOf(str);
if(judge!=-1)
number[judge]++;
else{
number[k]++;
k++;
}
}
int max=0,maxIndex=0;
for(int i=0;i<k;i++){
if(max<number[i]){
max=number[i];
maxIndex=i;
}
}
System.out.println(color.get(maxIndex));
}
}
}
``````

## 1005 .

Problem Description
A number sequence is defined as follows:

f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

Given A, B, and n, you are to calculate the value of f(n).

Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.

Output
For each test case, print the value of f(n) on a single line.

f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7

==>>

f(n) = ((A%7) * (f(n - 1)%7) +( B %7)* (f(n - 2)%7)) mod 7

``````import java.util.Scanner;

public class Main {

public static void main(String args[]){
Scanner s=new Scanner(System.in);
while(true){
String str=s.nextLine();
String strArray[]=str.split(" ");
int A=Integer.parseInt(strArray[0]);
int B=Integer.parseInt(strArray[1]);
int n=Integer.parseInt(strArray[2]);
A=A%7;
B=B%7;
int f[]=new int[60];
int period=1;
f[0]=f[1]=1;
f[2]=(A+B)%7;
if(A==0&&B==0&&n==0)
break;
if(A==0&&B==0){
if(n<=2)
System.out.println("1");
else
System.out.println("0");
}
else if(A!=0&&B==0){
if(n<=2)
System.out.println("1");
else{
for(int j=3;j<60;j++){
f[j]=(f[j-1]*A)%7;
if(f[j]==A){
period=j-2;
break;
}
}
System.out.println(f[(n-3)%period+2]);
}
}
else{
for(int j=3;j<=60;j++){
f[j]=(f[j-1]*A+f[j-2]*B)%7;
if(f[j]==1&&f[j-1]==1){
period=j-1;
break;
}
}
System.out.println(f[(n-1)%period]);
}
}
}
}
``````

• 本文已收录于以下专栏：

## 杭电ACM1003C做法

Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculat...
• SuperBeauty
• 2015年10月19日 08:48
• 771

## 杭电ACM 1003

} #include#include# includeint main(){ int r = 0,l = 0,i= 0 ,j = 0,num = 0,n;// l用来记录最大左范围r 右 ...
• fx397993401
• 2009年07月15日 17:28
• 7732

## hdu 1003 java

• ZhengGuangzhq
• 2014年01月22日 00:23
• 1293

## HDU ACM 1005 java版

• jinyongqing
• 2014年03月19日 16:05
• 3332

## 菜鸟上路，杭电OJ1003 最大子串问题

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s...
• ssdut_209
• 2016年05月20日 10:19
• 1248

## 杭电oj-1003-Max Sum

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum...
• yeyun666
• 2015年12月09日 14:39
• 801

## 子序列最大和（杭电acm1003）

Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate th...
• zhu_wei_jian
• 2012年03月25日 21:50
• 3652

## Java - 杭电1003 Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submissio...
• qq_34594236
• 2016年05月26日 21:39
• 716

## 杭电1003的解决方法

• sinat_35770194
• 2016年10月08日 23:55
• 788

## HDU 1003 解题报告

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...
• u013341274
• 2014年05月24日 19:09
• 2847

举报原因： 您举报文章：杭电ACM1003,1004,1005 java解答 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)