hdu oj从1000-1001-1002
来源:互联网 发布:多邻国提醒连接网络 编辑:程序博客网 时间:2024/06/11 08:33
自己要开始学习java,用hdu一边做一边记录下。
hdu1000
很简单直接上代码。
import java.util.*;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); } }}
hdu1001
计算从1到n的累加。有俩种简单的方法。一种直接用for循环从1到n,num= i+num;一种是n * (n+1)/2。因为1+2+3+4……..+(n-3)+(n-2)+(n-1)+n你可以反着写n+(n-1)+(n-2)+(n-3)+…..3+2+1;这时候呢上下相加除以2还是一样的,但可以看做是n倍的(n+1)再除以2;但是,这里要注意要求You may assume the result will be in the range of 32-bit signed integer. 一个在32位有符号整数的范围内。所以n*( n+1)可能会超出32位,所以可以加一个判断,if(n%2 == 0)判断是不是偶数,如果是偶数的话可以改为n/2 * (n+1),如果是奇数的话(n+1)/2 * n。
import java.util.*;public class Main { public static void main(String args[]) { Scanner in = new Scanner(System.in); while(in.hasNextInt()) { int sum; int n = in.nextInt(); if(n%2 == 0) { sum = n/2*(n+1); } else { sum = (n+1)/2*n; } System.out.println(sum); System.out.println(); } }}
hdu 1002
大整数相加,想当年,在C语言的学习中,我们常常被灌输,int的范围不能超过2^32如果你的操作系统是64位的还能去到2^64,而long则是固定2^32,由于我是用java的,所以不得不提一下java中特别好用的BigInteger,这里直接用它就可以解决。
import java.math.BigInteger;import java.util.Scanner;public class Main { public static void main(String[] args) { BigInteger a, b; int T; Scanner in = new Scanner(System.in); T = in.nextInt(); for (int i = 1; i <= T; ++i) { System.out.println("Case" + " " + i + ":"); a = in.nextBigInteger(); b = in.nextBigInteger(); if (i < T) { System.out.println(a + " + " + b + " = " + a.add(b) ); System.out.println(); } else { System.out.println(a + " + " + b + " = " + a.add(b)); } } }}
0 0
- hdu oj从1000-1001-1002
- HDU ACM OJ 1000 1001 1002答案(JAVA)
- HDU OJ 1001 Sum Problem
- hdu-oj 1002 A + B Problem II
- Hdu oj 1002 A + B Problem II
- HDU OJ 1002 A + B Problem II
- HDU OJ 1000 A + B Problem
- Hdu oj 2112 HDU Today
- Hdu Oj 2112 HDU Today
- HDU OJ 题目分类
- HDU OJ分类
- hdu oj 题目分类
- hdu OJ题目分类
- hdu oj 2000
- hdu oj 2001
- hdu oj 2002
- hdu oj 2003
- hdu oj 2004
- interrupt、interrupted 、isInterrupted 区别
- 51nod 1089 最长回文子串 V2(Manacher算法)
- UVa658 It's not a Bug, it's a Feature!
- 时间复杂度和空间复杂度
- 51nod 1090 3个数和为0
- hdu oj从1000-1001-1002
- JMM主内存与工作内存交互
- 常用开发工具的API文档大全
- NOIP2012提高组 同余方程
- Linux Shell 通配符、元字符、转义符使用实例介绍
- 用pandas分析百万电影数据
- 51nod 1091 线段的重叠
- ACdream 1084寒假安排 (算术基本定理)
- toolbar整理