6道题学会JAVA解大数问题
来源:互联网 发布:利达消防报警主机编程 编辑:程序博客网 时间:2024/06/07 14:37
大数阶乘
时间限制:3000 ms | 内存限制:65535 KB
难度:3
- 描述
- 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?
- 输入
- 输入一个整数m(0<m<=5000)
- 输出
- 输出m的阶乘,并在输出结束之后输入一个换行符
- 样例输入
50
- 样例输出
30414093201713378043612608166064768844377641568960512000000000000
- 来源
- 经典题目
- 上传者
- 张云聪
import java.math.BigInteger; import java.util.*; public class Main { public static void main(String args[]){ Scanner cin = new Scanner(System.in); long num = cin.nextLong(); BigInteger ans = new BigInteger("1"); while(num > 0){ ans = ans.multiply(BigInteger.valueOf(num)); num -= 1; } System.out.println(ans); cin.close(); } }
棋盘覆盖
时间限制:3000 ms | 内存限制:65535 KB
难度:3
- 描述
在一个2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图1(k=2时),现用一缺角的2×2方格(图2为其中缺右下角的一个),去覆盖2k×2k未被覆盖过的方格,求需要类似图2方格总的个数s。如k=1时,s=1;k=2时,s=5
- 输入
- 第一行m表示有m组测试数据;
每一组测试数据的第一行有一个整数数k; - 输出
- 输出所需个数s;
- 样例输入
3123
- 样例输出
1521
- 来源
- 《算法设计》题
- 上传者
- 李剑锋
import java.math.BigInteger; import java.util.*; public class Main { public static void main(String args[]){ Scanner cin = new Scanner(System.in); int loop = cin.nextInt(); while(loop-- > 0){ int k = cin.nextInt(); BigInteger ans = new BigInteger("4"); ans = ans.pow(k).subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)); System.out.println(ans); } } }
比大小
时间限制:3000 ms | 内存限制:65535 KB
难度:2
- 描述
给你两个很大的数,你能不能判断出他们两个数的大小呢?
比如123456789123456789要大于-123456
- 输入
- 每组测试数据占一行,输入两个不超过1000位的10进制整数a,b
数据保证输入的a,b没有前缀的0。
如果输入0 0表示输入结束。测试数据组数不超过10组 - 输出
- 如果a>b则输出“a>b”,如果a<b则输出“a<b”,如果相等则输出“a==b”。
- 样例输入
111111111111111111111111111 88888888888888888888-1111111111111111111111111 222222220 0
- 样例输出
a>ba<b
- 上传者
- 张云聪
import java.math.BigInteger; import java.util.*; public class Main { public static void main(String args[]){ Scanner cin = new Scanner(System.in); BigInteger a, b; while(true){ a = cin.nextBigInteger(); b = cin.nextBigInteger(); if(a.equals(BigInteger.valueOf(0)) && b.equals(BigInteger.valueOf(0))){ break; } int ans = a.compareTo(b); if(ans == 0){ System.out.println("a==b"); } else if(ans < 0){ System.out.println("a<b"); } else{ System.out.println("a>b"); } } } }
A+B Problem II
时间限制:3000 ms | 内存限制:65535 KB
难度:3
- 描述
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
A,B must be positive.
- 输入
- 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.
- 输出
- 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.
- 样例输入
21 2112233445566778899 998877665544332211
- 样例输出
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
- 来源
- 经典题目
- 上传者
- 张云聪
import java.math.BigInteger; import java.util.*; public class Main { public static void main(String args[]){ Scanner cin = new Scanner(System.in); BigInteger a, b, ans; int loop = cin.nextInt(), i = 0; while(i ++ < loop){ a = cin.nextBigInteger(); b = cin.nextBigInteger(); ans = a.add(b); System.out.println("Case " + i + ":"); System.out.println(a + " + " + b + " = " + ans); } } }
某种序列
时间限制:3000 ms | 内存限制:65535 KB
难度:4
- 描述
- 数列A满足An = An-1 + An-2 + An-3, n >= 3
编写程序,给定A0, A1 和 A2, 计算A99- 输入
- 输入包含多行数据
每行数据包含3个整数A0, A1, A2 (0 <= A0, A1, A2 <= 100000000)
数据以EOF结束 - 输出
- 对于输入的每一行输出A99的值
- 样例输入
1 1 1
- 样例输出
69087442470169316923566147
- 来源
- 水题比赛中较不水的
- 上传者
- hzyqazasdf
import java.math.BigInteger; import java.util.*; public class Main { public static void main(String args[]){ Scanner cin = new Scanner(System.in); BigInteger ans[] = new BigInteger[100]; while(cin.hasNext()){ for(int i = 0; i < 3; i ++){ ans[i] = cin.nextBigInteger(); } for(int i = 3; i < 100; i ++){ ans[i] = ans[i - 1].add(ans[i - 2].add(ans[i - 3])); } System.out.println(ans[99]); }
高精度幂
时间限制:3000 ms | 内存限制:65535 KB
难度:2
- 描述
对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。
现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 < =n <= 25。- 输入
- 输入有多行,每行有两个数R和n,空格分开。R的数字位数不超过10位。
- 输出
- 对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0 。如果输出是整数,不要输出小数点。
- 样例输入
95.123 120.4321 205.1234 156.7592 998.999 101.0100 12
- 样例输出
548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201
- 来源
- POJ
- 上传者
- iphxer
import java.math.BigDecimal; import java.util.*; public class Main { public static void main(String args[]){ Scanner cin = new Scanner(System.in); BigDecimal a; int b; while(cin.hasNext()){ a = cin.nextBigDecimal(); b = cin.nextInt(); String ans = a.pow(b).stripTrailingZeros().toPlainString(); // 整数去掉小数点和后面的0 if(ans.startsWith("0")){ <span style="white-space:pre"> </span>//去掉前导0 ans = ans.substring(1); } System.out.println(ans); } } }
1 0
- 6道题学会JAVA解大数问题
- java大数问题
- Java大数问题
- 大数类问题(java)
- java实现大数相加问题
- java竞赛-大数阶乘问题
- 大数相加问题(java版 )
- java大数开根号问题
- 打印100的阶乘(大数问题,Java好解)
- 1005 大数加法(java写大数问题真有优势)
- 【大数问题】大数阶乘
- 算法题-大数相乘问题
- 算法题-大数相乘问题
- java BigDecimal 处理大数问题很方便
- 《用java来解决大数问题总结》
- java 关于大数的一些问题
- 大数相乘问题(Java实现)
- 大数相乘问题(java版)
- 【poj1007】 DNA Sorting
- POJ 1964 City Game
- socket.io
- C#中的Where和Lambda表达式
- SQLServer 导出数据到web发布
- 6道题学会JAVA解大数问题
- Linux内核驱动之DDR3
- iOS CoreAnimation动画系列教程(一):CABasicAnimation
- Scala类型系统编程
- Java中使用Jedis操作Redis
- SpringAOP:目标类如何向切面类中传参
- js判断是否安装app,安装打开app
- 尚学堂hadoop大数据培训Hadoop安装配置
- 表(算法分析与设计笔记)