【ACM】P1000、P10001、P1002、P1003代码演示
2017-04-26 15:24
495 查看
(p1000) 问题描述(A+B Problem):
/*** * A + B Problem(解决A+B简单问题) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 658688 Accepted Submission(s): 205290 Problem Description Calculate A + B.//计算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 Author HDOJ *@author <a"283505495@qq.com">lxd</a> *@version 1.0 2017-4-26 下午1:53:07 *@fileName P1000.java */
代码演示:
package ac; import java.util.Scanner; public class P1000 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int a=sc.nextInt(); int b=sc.nextInt(); int sum=a+b; System.out.println(sum); } } }
运行结果:
p1001问题描述(Sum Problem):
/** * Sum Problem(求和问题) Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 470318 Accepted Submission(s): 118540 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.(求解1+....+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. 32位的整数 也许n的值特别大 You may assume the result will be in the range of 32-bit signed integer. Sample Input 1 100 Sample Output 1 5050 *@author <a"283505495@qq.com">lxd</a> *@version 1.0 2017-4-26 下午2:09:34 *@fileName p1001.java */
代码演示:
package ac; import java.util.Scanner; public class p1001 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); int sum=0; for(int i=1;i<=n;i++){ sum+=i; } System.out.println(sum); System.out.println(); } } }
运行结果:
p1002问题描述(A + B Problem II(A+B问题2 —–利用大数进行解决)):
/** * A + B Problem II(A+B问题2 -----利用大数进行解决) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 356144 Accepted Submission(s): 69087 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 (情况个数T) The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. (这两个A、B数可能非常大) Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, (比32位整数还要大) that means you should not process them by using 32-bit integer. (整数长度不要超过1000) 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. he 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 <a"283505495@qq.com">lxd</a> *@version 1.0 2017-4-26 下午2:16:06 *@fileName P1002.java */
代码演示:
package ac; import java.math.BigInteger; import java.util.Scanner; public class P1002 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); /** * 1、java.util.math中的BigInteger类,用来封装大数,可以对大数进行基本的操作。 */ int t=sc.nextInt(); for(int i=0;i<t;i++){ BigInteger a=new BigInteger(sc.next()); BigInteger b=new BigInteger(sc.next()); BigInteger sum=a.add(b); System.out.println("Case "+(i+1)+":"); /** * a、b、sum必须转换为toString */ System.out.println(a.toString()+" + "+b.toString()+" = "+sum.toString()); if(i!=t-1){ System.out.println(); } } } }
运行结果:
p1003问题描述(Max Sum(求一个无序序列的最大和)):
/** * Max Sum(求一个无序序列的最大和) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 242414 Accepted Submission(s): 57233 Problem Description 给定一个序列值,计算出此序列的最大和 Given a sequence a[1],a[2],a[3]......a , 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 <a"283505495@qq.com">lxd</a> *@version 1.0 2017-4-26 下午3:02:59 *@fileName P1003.java */
代码演示:
相关文章推荐
- 【ACM】P2011、P2012、P2013(*)、P2014代码演示
- 【ACM】P2020、P2021、P2022、P2023、P2024代码演示
- 【ACM】P2000、P2001、P2002、P2003、P2004、P2005代码演示
- 【ACM】P2025、P2026、P2027、P2028、P2029代码演示
- 【ACM】P2015(*)、P2016、P2017、P2018(*)、P2019代码演示
- 【ACM】P2006、P2007、P2008、P2009、P2010代码演示
- 【ACM】P2030、P2031、P2032、P2033、P2034代码演示
- 演示事件(Event)怎样使用以及怎样为用户控件添加一个事件(示例代码下载)
- XMLHttp的演示与代码示例
- 演示Session对象使用 范例(示例代码下载)
- [XNA研究强烈推荐]XNA开发雷电类游戏,源代码提供下载学习(提供代码和视频演示)
- ASP.NET Connections 大会的讲义和演示代码
- Interlocking Tile LOD算法演示(包括部分代码)
- 一个简单的态机模式工作流演示(2)(代码与实现)
- 演示事件(Event)怎样使用以及怎样为用户控件添加一个事件(示例代码下载)
- ACM代码总结
- SQL2005常见性能问题排错演示代码[收藏]
- 两首歌曲的汇编演示(发声代码)...
- 【原创】DOS下TSR程序的汇编演示代码2--黑屏保护程序
- 演示Session对象使用 范例(示例代码下载)