赛马网ACM试题(原杭电oj ACM)java版答案(1000,10001,1002)

赛马网ACM试题(原杭电OJ ACM试题)答案(java版)


 

 Author : Zhang Hailong 
 Date   : 2015-09-17  
 HomePage : http://
 Email  : [email protected]


 

杭电OJ:http://acm.hdu.edu.cn

赛码网:http://www.acmcoder.com


    突然手痒,来做一下acm试题练练手,由于最近在学java,顺便练一下java编程。但是对于ACM训练,c会更好,因为c的时间效率更高一些,这方面比java有优势。其实调调小程序就像品茶一样也挺有意思的(怎么闻到一股屌丝气息)。

    最近也在找工作阶段,对于新兴的在线比赛,在线程序测试略有感触,这是一个大趋势,也是互联网公司招聘的一个优势吧,不过诸多问题还有待改善,这里不详述。对于计算机专业出身,编程是基础,想要进阶,就先积累点滴吧。

注意:提交的java代码的类名都必须为Main



第1000题:A+B Problem


Problem Description
Calculate A + B.

Input
Each line will contain two integers A and B. Process to end of file.
Output
For each case, output A + B in one line.
Sample Input
1 1
Sample Output
2

题目解析:

要求每行输入两个数,计算两数的和并输出,这里是要循环的输入,程序自动输出!


代码:

import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		while(in.hasNextInt())
		{
			int a = in.nextInt();
			int b = in.nextInt();
			System.out.println(a+b);
		}
	}
}



第1001题:Sum Problem

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
题目解析:

输入一个数n,计算1+2+3+...+n的值。


代码:

import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int a=0, sum = 0;
		while(input.hasNextInt())
		{
			a = input.nextInt();
			for(int i=1; i<=a; i++)
			{
				sum += i;
			}
			System.out.println(sum);
			System.out.println();
			sum = 0;
		}
	}
}



第1002题:A+B Proble Ⅱ

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


题目解析:

输入的第一行:T为实例的数量,再输入T行的例子,每行有2个正整数(他们很大,所以不能用32位的整数来表示),要求计算两个数的和并输出,保证输入的每个整数的长度不超过1000。


代码:

import java.math.BigInteger;
import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		for(int i=0;i

 

代码解析:

使用BigInteger进行大于32位的数表示,使用BigInteger.add()进行加法运算。如果使用int就会测试错误。在代码中写"\n"时测试错误,换成"\r\n"时则运行成功。这里要特别注意输出格式,每个实例之间要换行,最后一个实例不用换行,所以加了一个if判断,不然会报错。


网上其他代码:

import java.math.BigDecimal; 
import java.util.Scanner; 
public class Main{
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in); 
		String temp1=null; 
		String temp2=null; 
		String result=null; 
		int i;
		int a=scanner.nextInt(); 
		for(i=0;i 

代码解析:

在网上搜索的代码,使用BigDecimal进行计算,BigDecimal主要用于大于32位的浮点数运算。也能测试通过。



你可能感兴趣的:(赛马网ACM试题(原杭电oj ACM)java版答案(1000,10001,1002))