基本大数问题

 1 //N!
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #define ll long long 
 6 #define  M 100000000
 7 #define  N  50000//根据数据量选择
 8 using namespace std;
 9 ll n;
10 ll  a[N];//要用ll
11 void solve(ll n)
12 {
13     memset(a,0,sizeof(a));
14     a[0]=1;
15     ll m=0;
16     for(ll i=1;i<=n;i++)
17     {
18         for(ll j=0;j<=m;j++){
19             a[j]*=i;//可能会超int
20             if(j>=1&&a[j-1]>=M){
21                 a[j]+=a[j-1]/M;
22                 a[j-1]%=M;
23             }
24             if(a[m]>=M) m++;
25         }
26     }
27     printf("%lld",a[m]);
28     for(int i=m-1;i>=0;i--){
29         printf("%08lld",a[i]);
30     }
31     printf("\n");
32 }
33 int main()
34 {
35     while(~scanf("%lld",&n)){
36         solve(n);
37     }
38     return 0;
39 }

 

 

 1 //N的阶乘
 2 import java.math.BigInteger;
 3 import java.util.Scanner;
 4 public class d {
 5      static BigInteger x=BigInteger.valueOf(1);//要静态变量
 6     public static BigInteger getTac(BigInteger a)
 7     {
 8         if(a.compareTo(x) <= 0)//0!==1!==1
 9             return x;
10         else
11             return a.multiply(getTac(a.subtract(x)));
12     }
13     public static void main(String[] args) {
14       
15         Scanner cin = new Scanner(System.in);
16         BigInteger m;
17         while(cin.hasNext())
18         {
19             m = cin.nextBigInteger();
20             m = getTac(m);
21             System.out.println(m);
22         }
23 
24     }
25 }        

 

 

 1 //2^n大数
 2 //C++
 3 #include<iostream>
 4 #include<cstdio>
 5 #include<cstring>
 6 #define  M 100000000
 7 #define  N  500
 8 using namespace std;
 9 int a[N],t;
10 int n;
11 void solve(int n)
12 {
13     memset(a,0,sizeof(a));//会对下次的运算造成影响
14     a[0]=1;
15     int m=0;
16     for(int i=1;i<=n;i++)
17     {
18         for(int j=0;j<=m;j++)
19         {
20             a[j]<<=1;
21             if(j>=1&&a[j-1]>=M){
22                 a[j]+=a[j-1]/M;
23                 a[j-1]%=M;
24             }
25             if(a[m]>=M) m++;
26         }
27     }
28     printf("%d",a[m]);
29     for(int i=m-1;i>=0;i--){
30         printf("%08d",a[i]);
31     }
32     printf("\n");
33 }
34 int main()
35 {
36     scanf("%d",&t);
37     while(t--)
38     {
39         scanf("%d",&n);
40         solve(n);
41     }
42     return 0;
43 }

 

 

 1 //2^n
 2 import  java.io.*;
 3 import  java.util.*;
 4 import  java.math.*;
 5 public class Main{
 6     static Scanner cin = new Scanner(System.in);//写在main外面要静态
 7     public static void main(String args[]){
 8         int t;
 9         t=cin.nextInt();
10         while(t>0){
11             t=t-1;
12             int n;
13             n=cin.nextInt();
14             BigInteger ans=BigInteger.valueOf(2);
15             ans=ans.pow(n);
16             System.out.println(ans);
17         }
18     }
19 }

 

 

//基础的java运算
import java.util.Scanner;
import java.math.BigInteger;
  public class Main{
    static Scanner cin=new Scanner(System.in);
    public static void main(String arg[]){
    BigInteger a,b;
    a=cin.nextBigInteger();
    b=cin.nextBigInteger();
    System.out.println("a+b :"+a.add(b));//+
    System.out.println(a.subtract(b)); // -
    System.out.println(a.multiply(b)); //*
    System.out.println(a.divide(b));  // /
    System.out.println(a.remainder(b)); // %
    if(a.equals(b)){
        System.out.println("YES");
    }
    else{
        System.out.println("NO");
    }
    if(a.compareTo(b)==0){
        System.out.println("a==b");
    }
    else if(a.compareTo(b)==-1){
        System.out.println("a<b");
    }
    else  if(a.compareTo(b)==1){
        System.out.println("a>b");
    }
}
}

 

posted on 2018-08-22 22:54  cltt  阅读(158)  评论(0编辑  收藏  举报

导航