大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
大数加法:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <stdexcept>
#define max 1000
using namespace std;
struct bign//定义一个结构体表示大数
{
int dig[max];//由低位到高位存放大数的低位到高位的各位数值
int len;//保存大数的位数
bign(){//初始化函数
dig[max] = {0};
len = 0;
}
};
void s_to_array(string &s,bign &d)//字符串转数组
{
int s_len = (int)s.size();
for(int i = 0,j = s_len-1;j >= 0 && i < max;i++,j--)
{
if(s[j]>='0' && s[j]<='9')
d.dig[i] = (int)(s[j] - '0');
else
throw invalid_argument("无效的输入,只能由数字构成!!!");
}
d.len = s_len;
}
void add(bign &a,bign &b,bign &ans)//大数加法函数
{
int carry = 0;//进位
int re;
for(int i = 0;i < a.len || i < b.len;i++)
{
re = a.dig[i] + b.dig[i] + carry;
ans.dig[ans.len++] = re%10;//取个位
carry = re/10;
}
if(carry != 0)
{
ans.dig[ans.len++] = carry;
}
}
int main()
{
string num;
string num1;
while(cin >> num >> num1)
{
bign a,b;
bign ans;
try{
s_to_array(num,a);
s_to_array(num1,b);
}catch(invalid_argument err){
cout << err.what() << endl;
cout << "Please Try Again" << endl;
continue;
}
for(int i = a.len-1;i >= 0;i--)
{
cout << a.dig[i];
}
cout << endl;
cout << "+" << endl;
for(int i = b.len-1;i >= 0;i--)
{
cout << b.dig[i];
}
cout << endl;
cout << "=" << endl;
add(a,b,ans);
for(int i = ans.len-1;i >= 0;i--)
{
cout << ans.dig[i];
}
cout << endl;
}
return 0;
}
大数减法:
#include <stdio.h>
#include <string.h>
void sub(char s1[],char s2[],char t[])
{
int i,l1,l2,k;
l1 = strlen(s1);
l2 = strlen(s2);
t[l1] = '#include <stdio.h>
#include <string.h>
void sub(char s1[],char s2[],char t[])
{
int i,l1,l2,k;
l1 = strlen(s1);
l2 = strlen(s2);
t[l1] = '\0';
l1--;
for(i=l2-1;i>=0;i--,l1--)
{
if(s1[l1]-s2[i] >= 0)
{
t[l1] = s1[l1] - s2[i] + '0';
}
else
{
t[l1] = 10 + s1[l1] - s2[i] + '0';
s1[l1-1] = s1[l1-1] - 1;
}
}
k = l1;
while(s1[k]<0)
{
s1[k] += 10;
s1[k-1] -= 1;
k--;
}
while(l1>0)
{
t[l1] = s1[l1];
l1--;
}
loop:
if(t[0] == '0')
{
l1 = strlen(s1);
for(i = 0;i<l1-1;i++)
t[i] = t[i+1];
t[l1-1] = '\0';
goto loop;
}
if(strlen(t) == 0)
{
t[0] = '0';
t[1] = '\0';
}
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%s",c,t))
{
sub(c,t,sum);
printf("%s\n",sum);
}
return 0;
}
';
l1--;
for(i=l2-1;i>=0;i--,l1--)
{
if(s1[l1]-s2[i] >= 0)
{
t[l1] = s1[l1] - s2[i] + '0';
}
else
{
t[l1] = 10 + s1[l1] - s2[i] + '0';
s1[l1-1] = s1[l1-1] - 1;
}
}
k = l1;
while(s1[k]<0)
{
s1[k] += 10;
s1[k-1] -= 1;
k--;
}
while(l1>0)
{
t[l1] = s1[l1];
l1--;
}
loop:
if(t[0] == '0')
{
l1 = strlen(s1);
for(i = 0;i<l1-1;i++)
t[i] = t[i+1];
t[l1-1] = '#include <stdio.h>
#include <string.h>
void sub(char s1[],char s2[],char t[])
{
int i,l1,l2,k;
l1 = strlen(s1);
l2 = strlen(s2);
t[l1] = '\0';
l1--;
for(i=l2-1;i>=0;i--,l1--)
{
if(s1[l1]-s2[i] >= 0)
{
t[l1] = s1[l1] - s2[i] + '0';
}
else
{
t[l1] = 10 + s1[l1] - s2[i] + '0';
s1[l1-1] = s1[l1-1] - 1;
}
}
k = l1;
while(s1[k]<0)
{
s1[k] += 10;
s1[k-1] -= 1;
k--;
}
while(l1>0)
{
t[l1] = s1[l1];
l1--;
}
loop:
if(t[0] == '0')
{
l1 = strlen(s1);
for(i = 0;i<l1-1;i++)
t[i] = t[i+1];
t[l1-1] = '\0';
goto loop;
}
if(strlen(t) == 0)
{
t[0] = '0';
t[1] = '\0';
}
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%s",c,t))
{
sub(c,t,sum);
printf("%s\n",sum);
}
return 0;
}
';
goto loop;
}
if(strlen(t) == 0)
{
t[0] = '0';
t[1] = '#include <stdio.h>
#include <string.h>
void sub(char s1[],char s2[],char t[])
{
int i,l1,l2,k;
l1 = strlen(s1);
l2 = strlen(s2);
t[l1] = '\0';
l1--;
for(i=l2-1;i>=0;i--,l1--)
{
if(s1[l1]-s2[i] >= 0)
{
t[l1] = s1[l1] - s2[i] + '0';
}
else
{
t[l1] = 10 + s1[l1] - s2[i] + '0';
s1[l1-1] = s1[l1-1] - 1;
}
}
k = l1;
while(s1[k]<0)
{
s1[k] += 10;
s1[k-1] -= 1;
k--;
}
while(l1>0)
{
t[l1] = s1[l1];
l1--;
}
loop:
if(t[0] == '0')
{
l1 = strlen(s1);
for(i = 0;i<l1-1;i++)
t[i] = t[i+1];
t[l1-1] = '\0';
goto loop;
}
if(strlen(t) == 0)
{
t[0] = '0';
t[1] = '\0';
}
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%s",c,t))
{
sub(c,t,sum);
printf("%s\n",sum);
}
return 0;
}
';
}
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%s",c,t))
{
sub(c,t,sum);
printf("%s\n",sum);
}
return 0;
}
大数乘法:
#include<stdio.h>
#include<string.h>
void mult(char a[],char b[],char s[])
{
int i,j,k=0,alen,blen,sum=0,res[65][65]={0},flag=0;
char result[65];
alen = strlen(a);
blen = strlen(b);
for(i=0;i<alen;i++)
{
for(j=0;j<blen;j++)
res[i][j] = (a[i]-'0')*(b[j]-'0');
}
for(i=alen-1;i>=0;i--)
{
for(j=blen-1;j>=0;j--)
{
sum = sum + res[i+blen-j-1][j];
printf("res = %d\n",res[i+blen-j-1][j]);
}
result[k] = sum%10;
k++;
sum /= 10;
}
if(sum)
{
result[k] = sum;
k++;
}
for(i=0;i<k;i++)
{
result[i] += '0';
}
for(i=k-1;i>=0;i--)
{
s[i] = result[k-1-i];
}
s[k] = '#include<stdio.h>
#include<string.h>
void mult(char a[],char b[],char s[])
{
int i,j,k=0,alen,blen,sum=0,res[65][65]={0},flag=0;
char result[65];
alen = strlen(a);
blen = strlen(b);
for(i=0;i<alen;i++)
{
for(j=0;j<blen;j++)
res[i][j] = (a[i]-'0')*(b[j]-'0');
}
for(i=alen-1;i>=0;i--)
{
for(j=blen-1;j>=0;j--)
{
sum = sum + res[i+blen-j-1][j];
printf("res = %d\n",res[i+blen-j-1][j]);
}
result[k] = sum%10;
k++;
sum /= 10;
}
if(sum)
{
result[k] = sum;
k++;
}
for(i=0;i<k;i++)
{
result[i] += '0';
}
for(i=k-1;i>=0;i--)
{
s[i] = result[k-1-i];
}
s[k] = '\0';
while(1)
{
if(strlen(s) != strlen(a) && s[0] == '0')
strcpy(s,s+1);
else
break;
}
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%s",c,t))
{
mult(c,t,sum);
printf("%s\n",sum);
}
return 0;
}
';
while(1)
{
if(strlen(s) != strlen(a) && s[0] == '0')
strcpy(s,s+1);
else
break;
}
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%s",c,t))
{
mult(c,t,sum);
printf("%s\n",sum);
}
return 0;
}
大数除以小数:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
void StringToNum(string str,int num[])
{
//字符串转int数组
int len = str.length();
for(int i = 0;i < len;i++)
{
num[i+1] = str[i] - '0';
}
num[0] = len;
}
int Divide(int div[],int ans[],int divisor)//得到div除于divisor的结果
{
//int carry = 0;
int temp = 0;//保存上一步的余数
int i;
for(i = 1;i <= div[0];i++)
{
temp = temp*10 + div[i];
if(temp < divisor) ans[i] = 0;//不够除,ans[i]为0
else
{
//够除
ans[i] = temp/divisor;//ans[i]为商
temp = temp % divisor;//更新余数
}
}
ans[0] = i;//保存长度标记
return temp;//返回余数
}
int main()
{
string str;
int dividend[1001];//被除数
int ans[1001];//结果
while(cin >> str)
{
StringToNum(str,dividend);
DecToBinary(dividend,ans);
for(int i = ans[0];i > 0;i--)
{
cout << ans[i];
}
cout << endl;
/*
int carry = DivideTwo(dividend,ans);
for(int i=1;i < ans[0];i++)
{
cout << ans[i];
}
ans[1001] = {0};
cout << ".............."<<carry<< endl;
*/
}
return 0;
}
大数阶乘:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
void factorial(int n,int *);
int b[10000];
int n;
while(cin>>n)
{
factorial(n,b);
}
return 0;
}
void factorial(int n,int b[10000])
{
long a[10000];
int i,j,l,c,m=0,w;
a[0] = 1;
for(i=1;i<=n;i++)
{
c=0;
for(j=0;j<=m;j++)
{
a[j] = a[j] * i + c;
c = a[j]/10000;
a[j] = a[j]%10000;
}
if(c>0)
{
m++;
a[m] = c;
}
}
w = m*4 + log10(a[m])+1;
cout << w << endl;
cout << a[m];
for(i=m-1;i>=0;i--)
{
cout << a[i];
}
cout << endl;
}
大整数乘以小数:
#include<stdio.h>
#include<string.h>
void mult(char c[],int m,char t[])
{
char s[100];
int len=strlen(c);
for(int i=0;i<len;i++)
{
s[len-i-1] = c[i] - '0';
}
int flag,add=0;
for(int i=0;i<len;i++)
{
int k = s[i] * m + add;
if(k>=10)
{
s[i] = k % 10;
add = k / 10;
flag = 1;
}
else
{
s[i] = k;
add = 0;
flag = 0;
}
}
while(add)
{
s[len++] = add % 10;
add /= 10;
}
for(int i=0;i<len;i++)
t[len-i-1] = s[i] + '0';
t[len] = '#include<stdio.h>
#include<string.h>
void mult(char c[],int m,char t[])
{
char s[100];
int len=strlen(c);
for(int i=0;i<len;i++)
{
s[len-i-1] = c[i] - '0';
}
int flag,add=0;
for(int i=0;i<len;i++)
{
int k = s[i] * m + add;
if(k>=10)
{
s[i] = k % 10;
add = k / 10;
flag = 1;
}
else
{
s[i] = k;
add = 0;
flag = 0;
}
}
while(add)
{
s[len++] = add % 10;
add /= 10;
}
for(int i=0;i<len;i++)
t[len-i-1] = s[i] + '0';
t[len] = '\0';
}
//大数加小数
void addt(char a[],int b,char c[])
{
int len = strlen(a);
char s[100];
for(int i=0;i<len;i++)
s[len-i-1] = a[i] - '0';
int add = 0;
for(int i=0;;i++)
{
if(i>=len)
{
s[i] = 0;
len++;
}
int k = s[i] + b % 10 + add;
b /= 10;
if(k>=10)
{
s[i] = k%10;
add = k/10;
}
else
{
s[i] = k;
add = 0;
}
if(b==0 && add==0) break;
}
for(int i=0;i<len;i++)
{
c[len-i-1] = s[i] + '0';
}
c[len] = '\0';
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%s",&m))
{
mult(c,m,t);
printf("%s\n",t);
addt(c,m,sum);
printf("%s\n",sum);
}
return 0;
}
';
}
//大数加小数
void addt(char a[],int b,char c[])
{
int len = strlen(a);
char s[100];
for(int i=0;i<len;i++)
s[len-i-1] = a[i] - '0';
int add = 0;
for(int i=0;;i++)
{
if(i>=len)
{
s[i] = 0;
len++;
}
int k = s[i] + b % 10 + add;
b /= 10;
if(k>=10)
{
s[i] = k%10;
add = k/10;
}
else
{
s[i] = k;
add = 0;
}
if(b==0 && add==0) break;
}
for(int i=0;i<len;i++)
{
c[len-i-1] = s[i] + '0';
}
c[len] = '#include<stdio.h>
#include<string.h>
void mult(char c[],int m,char t[])
{
char s[100];
int len=strlen(c);
for(int i=0;i<len;i++)
{
s[len-i-1] = c[i] - '0';
}
int flag,add=0;
for(int i=0;i<len;i++)
{
int k = s[i] * m + add;
if(k>=10)
{
s[i] = k % 10;
add = k / 10;
flag = 1;
}
else
{
s[i] = k;
add = 0;
flag = 0;
}
}
while(add)
{
s[len++] = add % 10;
add /= 10;
}
for(int i=0;i<len;i++)
t[len-i-1] = s[i] + '0';
t[len] = '\0';
}
//大数加小数
void addt(char a[],int b,char c[])
{
int len = strlen(a);
char s[100];
for(int i=0;i<len;i++)
s[len-i-1] = a[i] - '0';
int add = 0;
for(int i=0;;i++)
{
if(i>=len)
{
s[i] = 0;
len++;
}
int k = s[i] + b % 10 + add;
b /= 10;
if(k>=10)
{
s[i] = k%10;
add = k/10;
}
else
{
s[i] = k;
add = 0;
}
if(b==0 && add==0) break;
}
for(int i=0;i<len;i++)
{
c[len-i-1] = s[i] + '0';
}
c[len] = '\0';
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%s",&m))
{
mult(c,m,t);
printf("%s\n",t);
addt(c,m,sum);
printf("%s\n",sum);
}
return 0;
}
';
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%s",&m))
{
mult(c,m,t);
printf("%s\n",t);
addt(c,m,sum);
printf("%s\n",sum);
}
return 0;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/184999.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...