# 杭电ACM1003,1004,1005 java解答

1269人阅读 评论(0)

## 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]);
}
}
}
}


1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：8616次
• 积分：214
• 等级：
• 排名：千里之外
• 原创：13篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章分类
文章存档
评论排行