JAVA BigInteger(大数类)HDU 1002 1042
2016-03-10 16:28
591 查看
总结一下用到的大数类的基本方法:
1.大数加法:
add ( BigInteger val )
2.大数乘法:
multiply ( BigInteger val )
3.大数除法:
divide ( BigInteger val )
4.大数取余:
mod ( BigInteger val )
5.取相反数:
negate ( )
6.求幂
pow( int number )
HDU 1002:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
代码如下:
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
BigInteger a,b,sum;
int T,index;
T=cin.nextInt();
index=0;
while(T>0)
{
T--;
index++;
a=cin.nextBigInteger();
b=cin.nextBigInteger();
sum=a.add(b);
System.out.println("Case " + index + ":");
System.out.println(a + " + " + b + " = " + sum);
if(T!=0)
System.out.println();
}
}
}
HDU 1042
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042
代码如下:
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i,n;
BigInteger Sum,ad,temp;
Scanner cin= new Scanner(System.in);
while(cin.hasNext())
{
n=cin.nextInt();
Sum=new BigInteger("1");
ad=new BigInteger("1");
temp=new BigInteger("1");
for(i=2;i<=n;i++)
{
ad=ad.add(temp);
Sum=Sum.multiply(ad);
}
System.out.println(Sum);
}
}
}
1.大数加法:
add ( BigInteger val )
2.大数乘法:
multiply ( BigInteger val )
3.大数除法:
divide ( BigInteger val )
4.大数取余:
mod ( BigInteger val )
5.取相反数:
negate ( )
6.求幂
pow( int number )
HDU 1002:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
代码如下:
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
BigInteger a,b,sum;
int T,index;
T=cin.nextInt();
index=0;
while(T>0)
{
T--;
index++;
a=cin.nextBigInteger();
b=cin.nextBigInteger();
sum=a.add(b);
System.out.println("Case " + index + ":");
System.out.println(a + " + " + b + " = " + sum);
if(T!=0)
System.out.println();
}
}
}
HDU 1042
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042
代码如下:
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i,n;
BigInteger Sum,ad,temp;
Scanner cin= new Scanner(System.in);
while(cin.hasNext())
{
n=cin.nextInt();
Sum=new BigInteger("1");
ad=new BigInteger("1");
temp=new BigInteger("1");
for(i=2;i<=n;i++)
{
ad=ad.add(temp);
Sum=Sum.multiply(ad);
}
System.out.println(Sum);
}
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树