北大POJ关于高精度幂的问题,程序用sample input测试没问题,但是提交上去后显示wrong answer,问题在哪? 20

题目:http://poj.org/problem?id=1001我的程序:#include<iostream>usingnamespacestd;classhugein... 题目:http://poj.org/problem?id=1001

我的程序:

#include <iostream>
using namespace std;

class hugeint{

public:
hugeint();
hugeint operator+(const hugeint &);
hugeint operator*(const hugeint &);
void set();
int get()const;
int a[150];
private:
int b;
int c;
double d;

};

int main()
{
hugeint op;
hugeint op1;
op1.set();
op = op1;
int j = 0;

for(j = 0;j < op1.get() - 1;j++)
{
op = op* op1;
}

int i = 0;
int k = 0;

while(i<150)
{
if(op.a[i]==0)
i++;
else
break;
}

if(i==150)
cout<< 0;

else
{
j = 149;

while(j>=(150-4*(op1.get())))
{
if(op.a[j]==0)
j--;
else
break;
}

if(j == 149 - 4*(op1.get()))
{
for(k = i;k<=j;k++)
cout<<op.a[k];
}

else
{
for(k = i;k<=(149-4*(op1.get()));k++)
cout<<op.a[k];

cout<<".";

for(k = (150-4*(op1.get()));k<=j;k++)
cout<<op.a[k];
}

}

return 0;

}

hugeint::hugeint()
{
b = 0;
c = 0;
d = 0.0;

int i = 0;

for(i = 0;i<150;i++)
{
a[i] = 0;
}
}

void hugeint::set()
{
cin>>d>>c;
d = d + 0.0000001;
b = static_cast<int>(d*10000);

int i = 0;

for(i = 149;i>=0 && b!=0;i--)
{
a[i] = b%10;
b = b/10;
}

}

hugeint hugeint:: operator+(const hugeint& op1)
{
hugeint op2;
int carry = 0;
int i = 0;

for(i = 149;i>=0;i--)
{
int temp = 0;

temp = a[i] + op1.a[i] + carry;

if(temp<=9)
{
op2.a[i] = temp;
carry = 0;
}
else
{
op2.a[i] = temp%10;
carry = 1;
}

}
return op2;

}

hugeint hugeint::operator*(const hugeint &op1)
{
hugeint ob[10];
hugeint op;
int carry = 0;
int i = 0;

int k = 0;

for(i = 0;i < 10;i++)
{
for(k = 149;k>=0;k--)
{
int temp = 0;

temp = (op1.a[149-i]) * a[k] + carry;

if(temp<=9)
{
ob[i].a[k-i] = temp;
carry = 0;
}
else
{
ob[i].a[k-i] = temp % 10;
carry = temp / 10;
}
}
}

op = ob[1] + ob[2] + ob[3] + ob[4] + ob [0]
+ ob[5] + ob [6] +ob[7] +ob[8] +ob[9] ;

return op;

}

int hugeint::get()const
{
return c;
}
展开
 我来答
porker2008
2011-05-26 · TA获得超过1.4万个赞
知道大有可为答主
回答量:7066
采纳率:62%
帮助的人:1.1亿
展开全部
改成多组输入即可

#include <iostream>
using namespace std;

class hugeint{

public:
hugeint();
hugeint operator+(const hugeint &);
hugeint operator*(const hugeint &);
bool set();
int get()const;
int a[150];
private:
int b;
int c;
double d;

};

int main()
{
while(1)
{
hugeint op;
hugeint op1;
if(!op1.set()) break;
op = op1;
int j = 0;

for(j = 0;j < op1.get() - 1;j++)
{
op = op* op1;
}

int i = 0;
int k = 0;

while(i<150)
{
if(op.a[i]==0)
i++;
else
break;
}

if(i==150)
cout<< 0;

else
{
j = 149;

while(j>=(150-4*(op1.get())))
{
if(op.a[j]==0)
j--;
else
break;
}

if(j == 149 - 4*(op1.get()))
{
for(k = i;k<=j;k++)
cout<<op.a[k];
}

else
{
for(k = i;k<=(149-4*(op1.get()));k++)
cout<<op.a[k];

cout<<".";

for(k = (150-4*(op1.get()));k<=j;k++)
cout<<op.a[k];
}

}

cout << endl;
}
//system("pause");
return 0;

}

hugeint::hugeint()
{
b = 0;
c = 0;
d = 0.0;

int i = 0;

for(i = 0;i<150;i++)
{
a[i] = 0;
}
}

bool hugeint::set()
{
if(!(cin>>d>>c)) return false;
d = d + 0.0000001;
b = static_cast<int>(d*10000);

int i = 0;

for(i = 149;i>=0 && b!=0;i--)
{
a[i] = b%10;
b = b/10;
}
return true;
}

hugeint hugeint:: operator+(const hugeint& op1)
{
hugeint op2;
int carry = 0;
int i = 0;

for(i = 149;i>=0;i--)
{
int temp = 0;

temp = a[i] + op1.a[i] + carry;

if(temp<=9)
{
op2.a[i] = temp;
carry = 0;
}
else
{
op2.a[i] = temp%10;
carry = 1;
}

}
return op2;

}

hugeint hugeint::operator*(const hugeint &op1)
{
hugeint ob[10];
hugeint op;
int carry = 0;
int i = 0;
int k = 0;
for(i = 0;i < 10;i++)
{
for(k = 149;k>=0;k--)
{
int temp = 0;

temp = (op1.a[149-i]) * a[k] + carry;

if(temp<=9)
{
ob[i].a[k-i] = temp;
carry = 0;
}
else
{
ob[i].a[k-i] = temp % 10;
carry = temp / 10;
}
}
}
op = ob[1] + ob[2] + ob[3] + ob[4] + ob [0]
+ ob[5] + ob [6] +ob[7] +ob[8] +ob[9] ;

return op;
}

int hugeint::get()const
{
return c;
}
富港检测技术(东莞)有限公司_
2024-05-27 广告
ISTA3E程序是对相同产品的集合包装的综合模拟性能测试,集合包装件被定义为将一个产品、多个产品或包装件放置在滑板或托盘上,固定在一起或是作为一个单元运输。例如:一台机器由带瓦楞底托的托盘上、瓦楞侧围、顶盖包装,用缠绕膜缠绕在托盘上。用于评... 点击进入详情页
本回答由富港检测技术(东莞)有限公司_提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式