杭电ACM1003,1004,1005 java解答

原创 2015年11月20日 23:50:52

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是怎么回事了,不过最后还是弄好了,就不再纠结这个问题了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

杭电ACM 1004 JAVA

题目自己上杭电ACM网站上查,百度一搜就出来了。1004号题目比较简单,本人为了熟练一下JAVA特地写了2个数据结构,浪费了一些时间,其实JAVA类库中自带有,大家可以自己调用这样更方便。 AC的版...

hdu1003 最大连续子序和

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...
  • shahdza
  • shahdza
  • 2011年04月05日 14:08
  • 8234

HDU1003(最大连续子序列和DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 解题思路: d[i] = max( d[ i - 1] + a[i] , a[i])...

杭电ACM1003

杭电ACM1003其实就是简单的子串序列和为最大值的问题,这里采用动态规划法解决这个问题,代码如下:#include using namespace std;int main() { int ...

杭电1003的解决方法

今天试了一下1003,发现不是很难,结果我错了! 我用了一种最直接的方法,结果超时了,于是我便去寻找更简洁的方法。结果还真找到了,于是自己编写了这个程序,结果当然AC啦!这个程序的算法很巧妙,值得学...

杭电1003 maxsum--解题报告

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1003 最常用的办法就是双重循环,但是绝对会超时,暴力方法在ACM上基本上都行不通。刚开始我也...

hdu 1003 java

这是个什么题呢?我给忘了分类了,哈哈哈哈,好久没动咯。。。。动态规划也行,数字逻辑也行。。。反正就是优化时间,模拟肯定超时。。。 主要想明白两点: 1.从1加到n为负,即sum(1,n) 2.从1加到...

2015杭电新生赛1005ACM组队安排【排列组合】【智商题】

ACM组队安排 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su...

杭电ACM 1005:Number Sequence

原创作品 转载请注明出处http://blog.csdn.net/always2015/article/details/45741011刚开始这题看起来比较容易,因为直接用递归的话很容易搞定。但是提交...

杭电ACM 1003

} #include#include# includeint main(){ int r = 0,l = 0,i= 0 ,j = 0,num = 0,n;// l用来记录最大左范围r 右 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:杭电ACM1003,1004,1005 java解答
举报原因:
原因补充:

(最多只允许输入30个字)