Java 大数加减乘除 hdu 5047
2015-07-12 16:35
447 查看
acm.hdu.edu.cn/showproblem.php?pid=5047
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1592 Accepted Submission(s): 622
Problem Description
Think about a plane:
● One straight line can divide a plane into two regions.
● Two lines can divide a plane into at most four regions.
● Three lines can divide a plane into at most seven regions.
● And so on...
Now we have some figure constructed with two parallel rays in the same direction, joined by two straight segments. It looks like a character “M”. You are given N such “M”s. What is the maximum number of regions that these “M”s can divide a plane ?
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/22/621d86dca9fc4bc938a1e1631f76d6bb.jpg)
Input
The first line of the input is T (1 ≤ T ≤ 100000), which stands for the number of test cases you need to solve.
Each case contains one single non-negative integer, indicating number of “M”s. (0 ≤ N ≤ 1012)
Output
For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then an integer that is the maximum number of regions N the “M” figures can divide.
Sample Input
Sample Output
Sawtooth
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1592 Accepted Submission(s): 622
Problem Description
Think about a plane:
● One straight line can divide a plane into two regions.
● Two lines can divide a plane into at most four regions.
● Three lines can divide a plane into at most seven regions.
● And so on...
Now we have some figure constructed with two parallel rays in the same direction, joined by two straight segments. It looks like a character “M”. You are given N such “M”s. What is the maximum number of regions that these “M”s can divide a plane ?
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/22/621d86dca9fc4bc938a1e1631f76d6bb.jpg)
Input
The first line of the input is T (1 ≤ T ≤ 100000), which stands for the number of test cases you need to solve.
Each case contains one single non-negative integer, indicating number of “M”s. (0 ≤ N ≤ 1012)
Output
For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then an integer that is the maximum number of regions N the “M” figures can divide.
Sample Input
2 1 2
Sample Output
Case #1: 2 Case #2: 19
import java.io.*; import java.math.*; public class Main{ public static BufferedReader cin=new BufferedReader(new InputStreamReader(System.in)); public static BufferedWriter cout=new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String []args) throws Exception{ int T=Integer.parseInt(cin.readLine());//读取T组数据 for(int nkase=1;nkase<=T;nkase++){ cout.write("Case #"+nkase+": "); BigInteger N=new BigInteger(cin.readLine());//输入 BigInteger ans=N.multiply/*乘法*/(N).multiply(BigInteger.valueOf(8)/*将int的 8转化为biginteger*/).subtract/*减法*/(N.multiply(BigInteger.valueOf(7))).add(BigInteger.valueOf(1)); cout.write(ans.toString());//转化成字符串 cout.newLine();//换行 } cout.flush(); cout.close();//关闭 } } 、、、、//m.divide(n)===m/n
相关文章推荐
- Java中的垃圾回收机制
- Java开发中的23种设计模式详解(转)
- srtuts,spring连续删除的问题
- java字符串缓冲池分析
- Java基础整理篇1-比较字符串是否有重复
- Java堆 新生代和老年代
- Java tutorial - collections
- Java学习笔记(一)——类成员变量默认值
- 新到的Mac配置Java开发环境
- 多线程-02,线程安全问题解决
- Manual close is not allowed over a Spring managed SqlSession
- java中的线程创建和使用
- Myeclipse快捷键
- Java多线程笔记
- MyEclipse中更换JDK及工程警告问题
- Java 嵌套类和内部类示例<三>
- Java 嵌套类和内部类示例<二>
- Java 嵌套类和内部类示例<一>
- [置顶] mac版本的eclipse开发环境(附上各个环境的下载链接)
- java并发6-小结