Java & C++ 大数计算
Java--大数计算,妈妈再也不用担心我的学习了 .
BigInteger
英文API:
http://docs.oracle.com/javase/8/docs/api/
中文API:
http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
import java.math.BigInteger;
import java.util.Scanner; //hdu 1002----A + B Problem II public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i = 1;
while (n-- != 0) {
BigInteger a = new BigInteger(sc.next());
BigInteger b = new BigInteger(sc.next());
System.out.println("Case " + (i++) + ":");
System.out.println(a + " + " + b + " = " + a.add(b));
if (n != 0)
System.out.println();
}
}
}
import java.math.BigInteger;
import java.util.Scanner; //hdu 1042 N! public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
BigInteger a = new BigInteger(sc.next()); if (a.compareTo(new BigInteger("0")) == 0) {
// 如果是0 返回1
System.out.println("1");
continue;
} BigInteger sum = a;
// 举例 求 9! 这是 sum=a=9 BigInteger b = new BigInteger("1"); while (a.compareTo(b) != 0) {
// while a不等于1
// 第一次循环sum=9 sum=sum*8(a=a-1)a=8
// 第二次循环 sum=72,sum=sum*7(a=a-1)a=7
// 第三次循环 sum=504,sum=sum*6(a=a-1)a=6
// .....等于1时退出
sum = sum.multiply(a = a.subtract(b));
}
System.out.println(sum);
}
}
}
-C++
string add(string str1,string str2)
{
string str;
int len1=str1.length();
int len2=str2.length();
if(len1<len2) {
for(int i=; i<=len2-len1; i++)
str1=""+str1;
} else {
for(int i=; i<len1-len2; i++) {
str2=""+str2;
}
}
len1=str1.length();
int cf=;
int tem;
for(int i=len1-; i>=; i--) {
tem=str1[i]-''+str2[i]-''+cf;
cf=tem/;
tem%=;
str=char(tem+'')+str;
}
if(cf!=)str=char(cf+'')+str;
return str;
}
Java & C++ 大数计算的更多相关文章
- Java的大数计算BigNumber
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- HDU 5686 斐波那契数列、Java求大数
原题:http://acm.hdu.edu.cn/showproblem.php?pid=5686 当我们要求f[n]时,可以考虑为前n-1个1的情况有加了一个1. 此时有两种情况:当不适用第n个1进 ...
- 【Java】Float计算不准确
大家可能都遇到过,float在计算某些值时,会有不准确的情况. 比如如下情况: > 计算不准确 package com.nicchagil.study.java.demo.No10float计算 ...
- Java BigInteger(大数,ACM比赛专用)
用c或者C++处理大数比较麻烦,于是决心学习一下JAVA中大数运算. 先下载一个eclipse,具体的用法去问度娘吧 JAVA中有两个类BigInteger和BigDecimal分别表示大整数类和大浮 ...
- Java的大数操作分为BigInteger和BigDecimal
Java的大数操作分为BigInteger和BigDecimal,但这两给类是分开使用的,有时候在编程的时候显得略微繁琐,现在编写了一个将二者合二为一的大数操作类. 大数操作类代码如下: 1 pack ...
- JAVA解决大数
主题链接:CLICK HERE~ 有了Java求解大数变得如此简单,以后再也不用操心大数模板了.哦啦啦啦. import java.math.BigInteger; import java.math. ...
- 用Java进行大数处理(BigInteger)-hdu1042
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 题目描述: 代码实现: import java.util.Scanner; import jav ...
- Java闰年的计算,Calendar的用法
Java闰年的计算,Calendar的用法 代码如下: package com.aaa.zuoye; import java.text.ParseException; import java.util ...
- Java基础-时间复杂度计算方式
Java基础-时间复杂度计算方式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 时间复杂度通常是衡量算法的优劣的,衡量算法的时间严格来讲是很难衡量的,由于不同的机器性能不用环境 ...
随机推荐
- 客户端缓存(Client Cache)
通常在服务器端大家都已经做了很多缓存的工作,ASP.NET CACHE也好MemeryCache也好却总是忽略了客户端缓存. 因为大家都知道不管哪个client都会缓存已经访问过的站点,但是浏览器缓存 ...
- Boost_udp错误
注意一点:当我们不同PC机间进行通信的时候,IP和端口号是不一样的.之前遇到的问题是,boost_system_error,这是因为我们在写程序的时候,发送和接收绑定了同一个端口,导致程序出错. ...
- WebSocket 服务器4
Java Websocket实例 Websocket 2015-04-11 14:11:54 发布 您的评价: 4.4 收藏 6收藏 介绍 现很多网站为了实现即时通讯,所用 ...
- 优化Hibernate所鼓励的7大措施
优化Hibernate所鼓励的7大措施: 1.尽量使用many-to-one,避免使用单项one-to-many2.灵活使用单向one-to-many3.不用一对一,使用多对一代替一对一4.配置对象缓 ...
- LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)
题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...
- 做中学learning by doing——个人感想20155312张竞予
做中学learning by doing--个人感想 概要 阅读五篇博客后的感触 我的个人技能 个人经验的类比与总结 c语言学习情况 java学习目标及规划 公文写作能力 我的感触 仔细阅读了老师公众 ...
- Jenkins进阶系列之——05FTP publisher plugin插件
说明:这个插件可以将构建的产物(例如:Jar)发布到FTP中去. 官方说明:FTP publisher plugin 安装步骤: 系统管理→管理插件→可选插件→Artifact Uploaders→F ...
- [C#]exchange发送,收件箱操作类
最近项目中需要用到exchange的操作,就参照msdn弄了一个简单的操作类.目前先实现了,发送邮件和拉取收件箱的功能,其他的以后在慢慢的添加. using Microsoft.Exchange.We ...
- Linq之Linq to Objects
目录 写在前面 系列文章 linq to objects 总结 写在前面 上篇文章介绍了linq的延迟加载特性的相关内容,从这篇文章开始将陆续介绍linq to Objects,linq to xml ...
- 第十八课:js样式操作需要注意的问题
样式分为,外部样式(<link />),内部样式(<style></style>),行内样式(style:).再加上一个important对选择器权重的干扰. 大体 ...