本文目錄一覽:
- 1、c語言乘方函數
- 2、用c語言實現大數乘方
- 3、C語言乘方運算
c語言乘方函數
在C語言的頭文件 math.h中定義了pow(x,y),返回結果是x的y次方。其中,x、y及函數值都是double型;具體使用時要先添加#includemath.h。
在C++以及其他高級編程語言中都定義了此操作函數。C++中,乘方函數被定義在了頭文cmath頭文件下。具體使用時,需先引用頭文件#include cmath。
對於64位長整型數據進行乘方計算,pow函數已無法滿足其精度需要,這裡需要通過長整型數的四則運算來實現。
乘方函數名稱:pow(double,double), 具體參數中至少一方為float、double、long double類型。如計算5³;時, 直接使用 pow(5,3);返回結果即記為125。
用c語言實現大數乘方
#includestdio.h
#includestdlib.h
void main()
{
int *a,n,b;
a=(int *)malloc(sizeof(int)*200000);
for(int i=0;i200000;i++)
a[i]=0;
a[199999]=1;
printf(“請輸入要被乘方的數\n”);
scanf(“%d”,b);
printf(“請輸入乘方數\n”);
scanf(“%d”,n);
for(i=1;in+1;i++)
{
for(int j=0;j200000;j++)
a[j]*=b;
for(j=199999;j=0;j–)
if(a[j]=10)
{
a[j-1]+=a[j]/10;
a[j]%=10;
}
}
for(i=0;a[i]==0;i++);
for(;i200000;i++)
printf(“%d”,a[i]);
printf(“\n”);
free(a);
}
可以處理2的200000次內的乘方
乘方數大了會比較慢
輸入1024的10000次,能輸出,不過你看看像什麼樣子
和輸入32的20000次一樣的,應該沒有什麼問題
C語言乘方運算
C語言的乘方運算可以利用庫函數pow。
pow函數原型:double pow( double x, double y );
頭文件:math.h/cmath(C++中)
功能:計算x的y次冪。
參考代碼:
#include stdio.h
#include math.h
int main()
{
int a=3,b=2;
double t = pow(a,b);//計算3的平方並輸出
printf(“%.0lf\n”,t);
return 0;
}
/*
輸出:
9
*/
原創文章,作者:LYIS,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/139036.html