水题不水(java篇)

杭电题目


1001 http://acm.hdu.edu.cn/showproblem.php?pid=1001

sum problem

Sum Problem

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 116339    Accepted Submission(s): 26781

Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).

In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
Input
The input will consist of a series of integers n, one integer per line.
Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
Sample Input
  
  
1 100
Sample Output
  
  
1 5050
Author
DOOM III

用java写System.out.println(sum+"\n");结果是PE,改用System.out.println(sum);System.out.println();就ac了,试问这两种有何不同,为何PE?

代码如下:

import java.util.Scanner;

class Main
{
	public static void main(String[] args)
	{
		Scanner keyIn = new Scanner(System.in);
		while(keyIn.hasNext())
		{
			int n = keyIn.nextInt();
			int sum = 0;
			int i;
			for(i =1;i<=n;i++)
			{
				sum+=i;
			}
		//	System.out.println(sum+"\n");
                        System.out.println(sum);
                        System.out.println();
	
		}
	}
}
楼主将会追查到底!尽请期待--

-------------------------------------------------------------------我是分割线-------------------------------------------------------------------------


1002 http://acm.hdu.edu.cn/showproblem.php?pid=1002

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 81988    Accepted Submission(s): 15431

Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
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 consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 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 is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
  
  
2 1 2 112233445566778899 998877665544332211
 

Sample Output
  
  
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
 
Author
Ignatius.L
代码如下:
import java.util.Scanner;
import java.math.BigInteger;

public class Main
{
	public static void main(String[] args)
	{
		Scanner keyIn = new Scanner(System.in);
		int count = 0;
		while(keyIn.hasNextBigInteger())
		{
			count = keyIn.nextInt();
			for(int i =0;i<count;i++)
			{
			BigInteger a= keyIn.nextBigInteger();
			BigInteger b= keyIn.nextBigInteger();
			BigInteger sum;
			sum = a.add(b);
			System.out.println("Case "+(i+1)+":");
			System.out.println(a+" + "+b+" = "+sum);
			if(i!=count-1)
			  System.out.println();
		 }
		}
	}
}
注意的是:

public void println(Object x)
这里用到的是BigInteger类,java自带高精度类,输入一个BigInteger用Scanner::nextBigInteger()这函数,返回一个BigInteger数据,再在用BigInteger::add函数,传进一个BigInteger
类型的数据,返回一个BigInteger数据,最后如何输出这个对象呢?
System.out提供的函数里面没有BigInteger类型的参数啊,其实是这样的,用System.out.println()在输出一个对象的时候会默认调用toString方法,哦哦,不是,类名+@+实例散列表的
16进制表示,这恐怕是将对象和字符串相加时候的操作,用System.out.println()答应一个对象的时候,实际上是先调用String::valueOf(Object obj),将返回对象的字符串表示
然后的行为如同先调用 print(String),OK就是这样了。
其次注意java.math不同于Math,Math类是java.lang包里面的,里面包含了三角函数,反三角函数,指数反法和力气一些方法,max,min,abs,round(四舍⑤入),random()产生0-1的双精度
浮点数,而math包里面包含了类似BigInteger的一些类!
最后注意是两个输出结果之间加上空格,最后一个数据输出的时候不要空格!今天晚上就写到这,看电影啦!


-------------------------------------------------------------------我是分割线------------------------------------------------------------------------- 

杭电1003  http://acm.hdu.edu.cn/showproblem.php?pid=1003

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 57691    Accepted Submission(s): 13079
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.
Sample Input
   
   
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
 

Sample Output
   
   
Case 1: 14 1 4 Case 2: 7 1 6
Author
Ignatius.L
代码如下
import java.util.Scanner;

class Main
{
    public static void main(String args[])
    {
        Scanner keyIn = new Scanner(System.in);
        while(keyIn.hasNext())
        {
        int iCase = keyIn.nextInt();
        for(int i =1;i<=iCase;i++)
        {
            int n = keyIn.nextInt();
            int sum =0;
            int data;
            int start=0;
            int end=0;
            int tempStart =0;
            int tempEnd = 0;
            int max = -1001;
            for(int j=0;j<n;j++)
            {
                data = keyIn.nextInt();
                if(sum>=0)
                {
                    sum+=data;
                    tempEnd=j;
                }
                else
                {
                    sum=data;
                    tempStart=j;
                }
                
                if(max<sum)
                {
                    max = sum;
                    start = tempStart;
                    end = tempEnd;    
                }
            }
            System.out.println("Case "+i+":");
            if(start>end)
                end=start;
            System.out.println(max+" "+(start+1)+" "+(end+1));
            if(i!=iCase)
          System.out.println();
        }
   }
    }
}
这题实际上就是在求最大子序列和
求解这问题有O(N^3),O(N^2),和O(N)三种时间复杂度,我上面代码当然是采用O(N)这种复杂度的方法咯
下面分别讲一下这三种方法,前两种略带,第三种详解
O(N^3)这中方法就是采用暴力法咯,把所有情况都列出来!假设我们要求i--j这段下标的序列和,i从1-n,j也从1-n,有因为求i--j求和要便利一遍,最好情况是只遍历一遍
最坏是遍历n次,折中也就是n/2,所以是O(n^3);
但是我们知道,i肯定要小于j的咯,所以我们就把j从i--n循环就可以了!这可以小小优化一下,
第三种方法就是动态规划了
f(x+1)=f(x)>=0?f(x)+a[i]:a[i];用我代码中变量表示就是sum=sum+data>=0?sum+data;data;这怎么理解呢?其实不难理解,就是如果当你的和小于0事舍去,如果sum是个负数
是个拖累data,那么我为什么还要带上sum呢?data独立不是更好的嘛!所以这就建立了方程式,OK我提交!OJ对你说"wrong answer",well well全场必须hold住,怎么回事呢?
自己写了几组测试数据,我们存在一个极端情况就是,要是所有的数据都是负数呢?那我们不是把所有数据都给丢掉了,所以必须注意这一组测试数据,如果所有的数据都是负
数,那么tempEnd就是只会静茹一次,是0,那么最后start会比end大, if(start>end)  end=start;这样就OK了,题目说了最小数值是-1000,所以我们max出事定义是位-1001
就好了


杭电 1004 http://acm.hdu.edu.cn/showproblem.php?pid=1004

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31645    Accepted Submission(s): 10479


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.
 
Sample Input
    
    
5 green red blue red red 3 pink orange pink 0
Sample Output
    
    
red pink
 

Author
WU, Jiazhi
 
Source

Recommend
JGShining
代码如下:
import java.util.ArrayList;
import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
         
         Scanner keyIn = new Scanner(System.in);
         while(keyIn.hasNextInt())
         {
         ArrayList arraylist = new ArrayList();
         int iCount = keyIn.nextInt();
         if(iCount==0)
            break;
         String temp;
         for(int i =0;i<iCount;i++)
         {
             temp = keyIn.next();
             arraylist.add(temp);
         }
         int index=0;
         int max = 0;
        // System.out.println(arraylist);
         for(int j=0;j<iCount;j++)
         {
             int num=0;
             for(int k=0;k<iCount;k++)
             {
                 //System.out.println(arraylist.get(j).equals(arraylist.get(k)));
                 if(arraylist.get(j).equals(arraylist.get(k)))
                 {
                     num++;
                 }
             }
         //    System.out.print(num);
             if(num>max)
             {
                 index =j;
                 max = num;
             }
         }
         System.out.println(arraylist.get(index));
         arraylist.clear();
        }
    }
}



这题没什么技巧的,就是暴力啦!依序取出数组中的元素,然后和和整个数zu遍历咯!下午刚看集合类,先来用用ArrayList类,这个类的特点就是可以动态增加,无需事先声明
其实这题还有更简单,主要是学习ArrayList类,首先我们接受输入字符串用什么,我试着用Scanner::next()来获取输入字符串查找并返回来自此扫描器的下一个完整标记,然
后调用add方法,把元素加入,之后通过双重循环来搜索某个元素在数组中的次数,当记录的次数num大于max时候,就记录当下的索引(保存在index)里面,最后通过get方法来
获取元素!这里提醒读者注意一下,楼猪在判断两个元素是否相等的时候使用了等于号来判断两个元素是否相等,这是C++养成的习惯,在java里面==比较的是元素的同一性,
也就是比较两个元素是否比较具有相同的地址值,这显然不是楼猪的本意,应该调用String::equals方法来比较两个元素值是否相等,这是java不同于C++的方面,哎,又刷了一
道!


1005 http://acm.hdu.edu.cn/showproblem.php?pid=1005

Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43022    Accepted Submission(s): 9373


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.
 
Sample Input
    
    
1 1 3 1 2 10 0 0 0

Sample Output
    
    
2 5
 
Author
CHEN, Shunbao

Source

Recommend
JGShining

这道题主要是找循环节,至于他妈的怎么找出选环节的过程我还需要好好理解一下!暂且搁置在这里
代码如下:
import java.util.Scanner;

class Sequence
{
	public static void main(String args[])
	{
		Scanner keyIn = new Scanner(System.in);
		int a,b,n;
		while(keyIn.hasNext())
		{
		a = keyIn.nextInt();
		b = keyIn.nextInt();
		n = keyIn.nextInt();
		if(a==0&&b==0&&n==0)
		   break;
		int f[]=new int[1009];
		f[1]=f[2]=1;
		for(int j =3;j<1009;j++)
		{
			f[j]=(a*f[j-1]+b*f[j-2])%7;
		}
		System.out.println(f[n%1008]);
	  }
	}
}

本人傻逼了,我直接用递归做,刚开始咯,虽然我知道可能会递归太深,但还是是是咯,果然犯傻了,看看其他人怎么做咯,找规律题目,就是知道循环接了,也没心情做了,
直接就提交了,AC,表示毫无兴奋的感觉,虽说是水体,因为规律不是俺找地,这也告诉我们如果怕递归太深,可以采用打表的方法来写出函数值,这些调用次数太平凡了,所以
就做出一个表来咯!一个小小习惯,声明数组的时候傻逼了int f[1009],以为自己还在写c++程序,java注意还要为数组分配尸体 int f[]=new int[1009];OK又是十一点多了

hdu1008
http://acm.hdu.edu.cn/showproblem.php?pid=1008

Elevator

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17199    Accepted Submission(s): 9176


Problem Description
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
 
Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.
 
Output
Print the total time on a single line for each test case.
 
Sample Input
    
    
1 2 3 2 3 1 0

Sample Output
    
    
17 41
 
Author
ZHENG, Jianqiang
 
Source

Recommend
JGShining
代码如下,这道题目其实很简单,就是一个简单的运算,题目理解清楚,是按照指定顺序去停!
import java.util.*;
class Main
{
	public static void main(String args[])
	{
		Scanner keyIn = new Scanner(System.in);
		int iStop;
		while(keyIn.hasNextInt())
		{
		iStop = keyIn.nextInt();
		if(iStop==0)
		  break;
		int floor=0;
		int lastFloor = 0;
		int timeCount = 0;
		for(int i =0;i<iStop;i++)
		{
			floor = keyIn.nextInt();
			if(floor>lastFloor)
			   timeCount+=6*(floor-lastFloor);
			else
			   timeCount+=4*(lastFloor-floor);
			lastFloor = floor;
		}
		timeCount+=iStop*5;
		System.out.println(timeCount);
	}
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值