大数加法
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void add(char a[],char b[],char back[])
{
int i,j,k,up,x,y,z,l;
char *c;
if(strlen(a) > strlen(b))
l = strlen(a)+2;
else
l = strlen(b)+2;
c = (char*)malloc(l*sizeof(char));
i = strlen(a)-1;
j = strlen(b)-1;
k = 0;
up = 0;
while(j>=0 || i>=0)
{
if(i<0) x = '0';
else
x = a[i];
if(j<0) y = '0';
else
y = b[j];
z = x-'0'+y-'0';
if(up)
z++;
if(z>9)
{
up = 1;
z%=10;
}
else
up = 0;
c[k++] = z+'0';
i--;
j--;
}
if(up)
c[k++] = '1';
i = 0;
c[k] = ' ';
for(k-=1; k>=0; k--)
back[i++] = c[k];
back[i] = ' ';
}
int main()
{
char c[10000],t[10000],sum[1000];
int m;
scanf("%d%*c",&m);
while(m--)
{
scanf("%s%s",c,t);
add(c,t,sum);
printf("%s
",sum);
if(m)
printf("
");
}
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
",res[i+blen-j-1][j]);
}
result[k] = sum%10;
k++;
sum = sum/10;
}
for(i = blen-2;i>=0;i--)
{
for(j = 0;j<=i;j++)
{
sum = sum+res[i-j][j];
}
result[k] = sum%10;
k++;
sum = 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] = ' ';
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
",sum);
}
return 0;
}
大数乘以小数
#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-1-i]=s[i]+'0';
t[len]=' ';
}
//大数加小数
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-1-i]=s[i]+'0';
c[len]=' ';
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%d",c,&m))
{
mult(c,m,t);
printf("%s
",t);
addt(c,m,sum);
printf("%s
",sum);
}
return 0;
}
大数减法
#include <stdio.h>
#include <string.h>
void sub(char s1[],char s2[],char t[])
{
int i,l1,l2,k;
l2 = strlen(s2);
l1 = strlen(s1);
t[l1] = ' ';
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] = ' ';
goto loop;
}
if(strlen(t) == 0)
{
t[0] = '0';
t[1] = ' ';
}
}
int main()
{
char c[1000],t[1000],sum[1000];
int m;
while(~scanf("%s%s",c,t))
{
sub(c,t,sum);
printf("%s
",sum);
}
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 consversion(char s[],char s2[],long d1,long d2)
{
long i,j,t,num;
char c;
num = 0;
for(i = 0;s[i]!=' ';i++)
{
if(s[i]<='9' && s[i]>='0')
t = s[i] - '0';
else
t = s[i] - 'A' +10;
num = num*d1+t;
}
i = 0;
while(1)
{
t = num%d2;
if(t<=9)
s2[i] = t+'0';
else
s2[i] = t+'A'-10;
num/=d2;
if(num == 0)
break;
i++;
}
for(j = 0;j<i/2;j++)
{
c = s2[j];
s2[j] = s[i-j];
s2[i-j] = c;
}
s2[i+1]=' ';
}
int main()
{
char s1[1000],s2[1000];
int d1,d2;
while(~scanf("%s%d%d",s1,&d1,&d2))
{
consversion(s1,s2,d1,d2);
printf("%s
",s2);
}
return 0;
}