POJ 1001
从上午11点到晚上8点,本姑娘终于把它给AC了!!说实话,这是我第一个解题写了这么长的程序,哈哈,其实并没有解过几道题。。。太激动,几次想放弃,但都忍住了,写完开始调试的时候竟然有些小激动,难道我爱上了编程?!
高精度数计算问题,调试过程非常感谢
http://blog.sina.com.cn/s/blog_7b7c7c5f0100rkjg.html
这篇文章提供的测试样例
奉上我的代码咩~~
/*poj 1001 大数乘法*/
#include<iostream>
#include<cstring>
using namespace std;
int res[10000];
int a[10000],b[10000],c[10000];
int lenres = 0;//记录结果数组的指针
int lenb;
void mul(int *a,int lencin)
{
int c = 0;//进位
int posres;
for(int i = 0; i < lenb; i++)
{
lenres = i;
for(int j = 0; j < lencin; j++)
{
int temp = b[i] * a[j];
if(c)
{
temp += c;
}
c = (temp + res[lenres]) / 10;
res[lenres] = (temp + res[lenres]) % 10;
lenres++;
posres = lenres;
}
if(c)
{
res[lenres] = c;
lenres++;
posres = lenres;
}
c=0;
}
for(int i = 0;i < posres;i++)
{
b[i] = res[i];
res[i] = 0;
}
lenb = posres;
lenres = 0;
}
int main()
{
char s[100],tp[100];//={'0','0','0','0','.','1'},tp[100];
char fres[10000],ffres[10000];
int n;
while(cin >> s >> n)
{
int lencin = strlen(s);
int docpos = -1;
//去掉输入中多余的0
int st = 0;
int ed = lencin - 1;
for(int i = 0; i < lencin; i++)
{
if(s[i]!='0')
{
st = i;
break;
}
}
for(int i = lencin - 1;i >= 0; i--)
{
if(s[i]!='0')
{
ed = i;
break;
}
}
lencin = ed - st + 1;
for(int i = st,j=0;i <= ed,j<lencin;i++,j++)
{
tp[j] = s[i];
}
for(int i = 0;i<lencin;i++)
{
s[i] = tp[i];
}
for(int i = 0; i < lencin; i++)
{
if(s[i]=='.')
{
//docpos = lencin - i - 1;
docpos = i;
}else{
a[i] = s[i] - '0';
}
}
if(docpos != -1)
{
for(int i = docpos; i < lencin - 1; i++)
{
a[i] = a[i+1];
}
lencin--;
docpos = (lencin - docpos) * n;
}
b[0] = 1;
lenb = 1;
for(int i = lencin - 1,j = 0; i >= 0, j < lencin; i--,j++)
{
c[j] = a[i];
}
for(int i = 0; i < n;i++)
{
mul(c,lencin);
}
if(docpos != -1){
if(lenb < docpos){
for(int i = lenb;i<lenb+docpos-1;i++)
{
b[i] = 0;
}
lenb += docpos;
}
for(int i = 0; i < docpos;i++)
{
fres[i] = b[i] + '0';
}
fres[docpos] = '.';
for(int i = docpos; i < lenb; i++)
{
fres[i+1] = b[i] + '0';
}
}else{
for(int i = 0; i < lenb; i++)
{
fres[i] = b[i] + '0';
}
}
int start,end;
int templen;
if(docpos != -1){
templen = lenb + 1;
}else{
templen = lenb;
}
for(int i = templen - 1; i >= 0; i--)
{
if(fres[i]!='0')
{
start = i;
break;
}
}
for(int i = 0; i < templen; i++)
{
if(fres[i]!='0')
{
end = i;
break;
}
}
if(start!=end)
{
if(fres[end]=='.')
{
end++;
}
for(int i = start; i >= end; i--)
{
cout << fres[i];
}
}else{
cout << 0;
}
cout << endl;
}
}