编写一个c++程序求3的n次方,可输出高精度的结果,例如输入48,输出79766443076872509863361。

(本人是初学者,希望程序尽量简约)...
2026年09月20日 12:33
有3个网友回答
网友(1):

double x=1.0;
for(i=0;idouble 的精度只有15为,离开你的 3 的48 次方 要求的 20多位 很远。
不能满足你的要求。
============
为达到精度要求,需要用大数整数乘法方法:用字符串形式存放数据,分段计算,处理进位。
大数乘法 程序,网上很多,下载一个使用便是。至于原理,研究起来费劲,除了烦人,没太多学问。

好,完整程序来了:
#include
using namespace std;

#define MAX_DIGITS 100
void mulBigInteger(char*mulor,char*mulant,char*result);
int POS=0;

main(){
double x=1;
int i,n;
char v1[100]="1",v2[100]="3",r[100];
cout << "please input n" << endl;
cin >> n;
for (i=0;imulBigInteger(v1,v2,r);
strcpy(v1,r);
}
r[strlen(r)-POS]='\0';
cout << fixed << "x= "<< r << endl;
return 0;
}

void mulBigInteger(char*mulor,char*mulant,char*result){
char one[MAX_DIGITS];
char two[MAX_DIGITS];
char rel[2*MAX_DIGITS];
int pro[2*MAX_DIGITS];
int row = strlen(mulant);
int col = strlen(mulor);
int pos =0;
int i,j;
strcpy(one,mulor);
strcpy(two,mulant);
for (i=0;i<2*MAX_DIGITS;i++)
pro[i]=0;
for(i = row-1; i >=0; i--)
for(j = col-1 ; j >= 0; j--)
{
int product = (one[j]-'0')*(two[i]-'0');
pro[i+j+0] += product/10 ;
pro[i+j+1] += product%10 ;
}
for(i = row+col-1; i>0;i--)
{
if(pro[i]>=10)
{
pro[i-1] +=pro[i]/10;
pro[i] =pro[i]%10;
}
}

for(i = 0;i if (pro[i]!= 0)
{
pos = i;
break;
}
POS=POS+pos;
for(i = 0,j = pos;i rel[i] = pro[j]+'0';
rel[row+col]='\0';
strcpy(result,rel);
}

运行例子:
please input n
48
x= 79766443076872509863361

网友(2):

#include
void main
{
  double x=3;
  int n,i;
  cout<<"\nInput n: ";
  cin>>n;
   for(i=0;i      x*=x;
  cout<<"\nThis is "<}

网友(3):

#include
#include
#include
#include
using namespace std;

int main(){
int n;
cin>>n;
int a[100];
a[0]=1;
int k=1;
int temp;
int carry=0;
int i;
int j;
for(i=0;i carry=0;
for(j=0;j temp=a[j]*3+carry;
a[j]=temp%10;
carry=temp/10;
}
if(carry!=0)
a[k++]=carry;
}
for(i=k-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
这个简单的,dev通过