就当是练习了(各种高精度)!综合很强!
#include " iostream "
#define M 1010
using namespace std;
char ch1[M],ch2[M];
int a[M],b[M];
int c[M],d[M];
int num[M];
int sum[M];
int W[M];
int H[M];
int t,k,g,v;
int i,j;
int La,Lb;
int sign = 0 ;
int s;
int L;
void _ADD()
{
La = 0 ;
W[La ++ ] = (sum[L - 1 ] + 1 ) % 10 ;
int flag = (sum[L - 1 ] + 1 ) / 10 ;
int i;
for (i = L - 2 ;i >= 0 ;i -- )
{
W[La ++ ] = (sum[i] + flag) % 10 ;
flag = (sum[i] + flag) / 10 ;
}
while (flag)
{
W[La ++ ] = flag % 10 ;
flag /= 10 ;
}
}
void Div( int * xx)
{
Lb = 0 ;
int sign = 0 ;
for ( int i = 0 ;i < L;i ++ )
{
if ((xx[i] + sign) >= 2 )
{
b[Lb ++ ] = (xx[i] + sign) / 2 ;
sign = (xx[i] - 2 * (xx[i] / 2 ));
}
else
{
sign = xx[i] * 10 ;
if (i) b[Lb ++ ] = 0 ;
}
}
}
void Add( int x , int y)
{
sign = 0 ; t = 0 ;
int p,q,flag = 0 ;
if (x > y)
{
for (p = 0 ,q = 0 ;p < y,q < y; p ++ ,q ++ )
{
num[t ++ ] = (d[p] + H[q] + flag) % 10 ;
flag = (d[p] + H[q] + flag) % 10 ;
}
for (p = y; p < x ; p ++ )
{
num[t ++ ] = (d[p] + flag) % 10 ;
flag = (d[p] + flag) / 10 ;
}
}
else if (x == y)
{
for (p = 0 ,q = 0 ;p < x,q < y; p ++ ,q ++ )
{
num[t ++ ] = (d[p] + H[q] + flag) % 10 ;
flag = (d[p] + H[q] + flag) / 10 ;
}
}
else
{
flag = 0 ;
for (p = 0 ,q = 0 ;p < x,q < x; p ++ ,q ++ )
{
num[t ++ ] = (H[p] + d[q] + flag) % 10 ;
flag = (H[p] + d[q] + flag) / 10 ;
}
for (q = x;q < y;q ++ )
{
num[t ++ ] = (H[q] + flag) % 10 ;
flag = (H[q] + flag) / 10 ;
}
}
while (flag)
{
num[t ++ ] = flag % 10 ;
flag /= 10 ;
}
for ( int yy = 0 ; yy < t;yy ++ )
{
d[yy] = num[yy];
if (d[yy] == 0 ) sign ++ ; // sign用于标记结果为0的情况
}
g = t;
memset(H , 0 , sizeof (H));
memset(num, 0 , sizeof (num));
}
void BigN()
{
int flag = 0 ;
int mark = 0 ;
if (La > Lb)
{
for (i = Lb - 1 ;i >= 0 ;i -- )
{
k = 0 ;
for (j = La - 1 ;j >= 0 ;j -- )
{
c[k ++ ] = (b[i] * a[j] + flag) % 10 ;
flag = (b[i] * a[j] + flag) / 10 ;
}
while (flag)
{
c[k ++ ] = flag % 10 ;
flag /= 10 ;
}
// 末尾赋值0
v = 0 ;
for ( int xx = 0 ; xx < mark; xx ++ )
H[v ++ ] = 0 ;
for ( int m = 0 ;m < k;m ++ )
H[v ++ ] = c[m];
memset(c, 0 , sizeof (c));
Add(g , v);
mark ++ ;
}
}
else
{
for (i = La - 1 ;i >= 0 ;i -- )
{
k = 0 ;
for (j = Lb - 1 ;j >= 0 ;j -- )
{
c[k ++ ] = (a[i] * b[j] + flag) % 10 ;
flag = (a[i] * b[j] + flag) / 10 ;
}
while (flag)
{
c[k ++ ] = flag % 10 ;
flag /= 10 ;
}
// 末尾赋值0
v = 0 ;
for ( int xx = 0 ; xx < mark; xx ++ )
H[v ++ ] = 0 ;
for ( int m = 0 ;m < k;m ++ )
H[v ++ ] = c[m];
Add(g , v);
mark ++ ;
}
}
}
int main()
{
while (cin >> ch1)
{
t = 0 ;k = 0 ;g = 0 ;
L = strlen(ch1);
for (i = 0 ;i < L;i ++ ) { sum[i] = ch1[i] - ' 0 ' ;}
_ADD();
if (W[La - 1 ] % 2 == 0 )
{
for (i = 0 ;i < L;i ++ ) a[i] = ch1[i] - ' 0 ' ;
L = La; Div(W);
}
else
{
for (i = 0 ;i < La;i ++ ) a[i] = W[i];
Div(sum);
}
BigN();
if (sign != t)
{
for (i = t - 1 ;i >= 0 ;i -- )
cout << d[i];
cout << endl;
}
else cout << " 0 " << endl;
}
return 0 ;
}