大整数问题的Java 解法

输入一个整数n(1<=n<=20),输出n 的阶乘:
import java.io.*;
import java.util.*;
import java.math.*;
class Main
{
<span style="white-space:pre">	</span>public static void main(String[] args)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">	</span>BigInteger n=new BigInteger("1");
<span style="white-space:pre">	</span>Scanner cin=new Scanner(System.in);
<span style="white-space:pre">	</span>while(cin.hasNext()){
<span style="white-space:pre">		</span>n=cin.nextBigInteger();
<span style="white-space:pre">		</span>BigInteger mul=new BigInteger("1");
<span style="white-space:pre">		</span>while(n.compareTo(BigInteger.valueOf(1))>=0&&(n.compareTo(BigInteger.valueOf(20)))<=0){
<span style="white-space:pre">			</span>mul=mul.multiply(n);
<span style="white-space:pre">			</span>n=n.subtract(BigInteger. valueOf (1));
<span style="white-space:pre">			</span>if(n.compareTo(BigInteger.valueOf(0))==0)
<span style="white-space:pre">				</span>System.out.println(mul);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
}
将BigInteger 的数转为2 进制:
public class Main {
<span style="white-space:pre">	</span>public static void main(String[] args) {
<span style="white-space:pre">		</span>System.out.println(change("3",10,2));
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>//num 要转换的数 from 源数的进制 to 要转换成的进制
<span style="white-space:pre">	</span>private static String change(String num,int from, int to){
<span style="white-space:pre">		</span>return new java.math.BigInteger(num, from).toString(to);
<span style="white-space:pre">	</span>}
}
对于Java,你需要实现一个名为Main 的类,其中包含一个静态方法main,如下所示://对于OJ
import java.util.*;
public class Main {
<span style="white-space:pre">	</span>public static void main(String args[]) {
<span style="white-space:pre">		</span>Scanner in = new Scanner(System.in);
<span style="white-space:pre">		</span>int a = in.nextInt();
<span style="white-space:pre">		</span>int b = in.nextInt();
<span style="white-space:pre">		</span>System.out.println(a + b);
<span style="white-space:pre">	</span>}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值