杭电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.

我的思路是用max来保存当前最大的和值,sum一直累加,一旦sum<0了,则清空sum(sum=0),记录下此时的位置,从当前位置继续累加,最后得到的max就是最大值,位置可以用变量也一起记录下来。

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;iint n=s.nextInt();
            int sum=0,max=-1001;
            int start=0,end=0,z=0;
            for(int j=0;jint a=s.nextInt();
                sum=sum+a;
                if(maxif(sum<0){
                    sum=0;
                    z=j+1;
                }
            }
            System.out.println("Case "+(i+1)+":");
            System.out.println(max+" "+(start+1)+" "+(end+1));
            if(i1)
                System.out.println();
        }
    }
}

注意:最后一个输出就不要换行了,不然会出现表达错误,无法通过。最近本人做这个ACM基本上每次都出现Prensentation Error。对于这个我也是无奈,各种隐藏的输出规定,却没有统一的标准,只能多尝试一下啦。不过出现Prensentation Error也可以肯定自己的程序是对的了。

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 color=new ArrayList();
            int number[]=new int[100];
            int k=0;
            for(int i=0;iint judge=color.indexOf(str);
                if(judge!=-1)
                    number[judge]++;
                else{
                    color.add(k,str);
                    number[k]++;
                    k++;
                }
            }
            int max=0,maxIndex=0;
            for(int i=0;iif(maxout.println(color.get(maxIndex));
        }
    }
}

小提示:类名都用Main。

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.

要注意它给定的n的范围很大,如果直接这样计算的话在规定的结果内是出不来结果的,即使你知道计算的技巧:
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

不过我们肯定可以肯定这是一个周期序列,(f(n-1),f(n))的最大周期是50,这是因为注意到f(n)只能取0-6中间的整数值,{(f(n-1),f(n))}这个集合里面最多只有49个元素。而f(n+1)由(f(n-1),f(n)决定取值。不过要值得注意的是A=0或B=0时的特殊情况,可能会对编写程序有一定影响。

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

忘记这里自己一开始出现Presentation是怎么回事了,不过最后还是弄好了,就不再纠结这个问题了。

你可能感兴趣的:(Acm)