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

注意:最後一個輸出就不要換行了,不然會出現表達錯誤,無法通過。最近本人做這個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<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{
                    color.add(k,str);
                    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));
        }
    }
}

小提示:類名都用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是怎麼回事了,不過最後還是弄好了,就不再糾結這個問題了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章