输入一个整数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>}
}
大整数问题的Java 解法
最新推荐文章于 2024-09-21 10:38:43 发布