基本大数问题
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"); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现