#include "stdio.h"
創(chuàng)新互聯(lián)長期為上千多家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為洋縣企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、網(wǎng)站制作,洋縣網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
#include "math.h"
main()
{
int a, b;
long power(), t;
printf("輸入兩個整數(shù):");
scanf("%d, %d", a, b);
t = power(a, b);
printf("%d ^ %d = %1d", a, b, t);
}
long power(x ,n)
int x, n;
{
long y;
if(n0)
y = x * power(x, n-1);
else y = 1;
return y;
}
#includestdio.h
int?power(int?x,int?n)
{
if(n==0)
return?1;
elseif(n%2==1)
return?x*power(x,n-1);
else{
int?y=power(x,n/2);
return?y*y;
}
}
int?main()
{
int?a,b,c;
printf("enter x and n:");
setvbuf(stdout,NULL,_IONBF,0);
scanf("%d%d",a,b);
c=power(a,b);
printf("結(jié)果為%d",c);
return?0;
}
擴展資料
#includestdio.h
double power(double x,int n);
main()
{
double x;
int n;
printf("Input x,n:");
scanf("%lf,%d",x,n);
printf("%.2lf",power(x,n));
}
double power(double x,int n)
{
double a=1.0;
int i;
for(i=1;i=n;i++)
a*=x;
return a;
}
參考資料:百度百科 - 遞歸調(diào)用
首先糾正一下,C語言不能重載同名函數(shù),C++也不能僅以返回值的區(qū)別來重載。所以可以統(tǒng)一使用double recursive_pow_pos(int n, int m)。
我?guī)湍阕隽藗€示范,應(yīng)該很容易看懂。
(注意,0^0=1,這是規(guī)定,也是合理的)
#includestdio.h
double recursive_pow_pos(int n, int m)
{
register double t;
if (m == 0)
return 1.0;
if (m 0)
return 1.0 / recursive_pow_pos(n, -m);
t = recursive_pow_pos(n, m / 2);
if (m % 2)
return t * t * (double)n;
else
return t * t;
}
int main(void)
{
int n, m;
double result;
double recursive_pow_pos(int, int);
printf("Input n-");
scanf("%d", n);
printf("Input m-");
scanf("%d", m);
result = recursive_pow_pos(n, m);
printf("%d ^ %d = %f\n", n, m, result);
return 0;
}
#include
stdio.h
//
遞歸函數(shù)計算x的n次冪,這里都是用的int,因此計算范圍只能限制在int類型的取值范圍下
//
如果需要計算更大的值,需要將類型修改為float
int
mypow(int
x,
int
n)
{
if
(n
=
0)
return
1;
else
return
x
*
mypow(x,
n
-
1);
}
int
main()
{
printf("2^32
=
%d\n",
mypow(2,
10));
//
計算2的10次方
return
0;
}
#includestdio.h
int Xn(int x,int n)//x的n次冪(n=0)
{
if(n=0)return 1;
else if(n=1) return x;
else return(Xn(x,n-1));
}
void main()
{
int a,b,xn;
scanf("%d %d",a,b);
xn=Xn(a,b);//a的b次冪
printf("The result is:%d",xn);
}
/*用c語言中的函數(shù)遞歸調(diào)用算法實現(xiàn)n階矩陣的n次冪*/
#include?stdio.h
#include?stdlib.h
#include?time.h
#include?string.h
//創(chuàng)建矩陣,矩陣用一維數(shù)組存儲
double?*matCreate(unsigned?int?m,?unsigned?int?n)
{
double?*p?=?(double?*)malloc(sizeof(double)?*?m?*?n);
if?(p?==?NULL)?printf("創(chuàng)建矩陣失?。n");
return?p;
}
//輸入矩陣元素
void?matInput(double?*a,?unsigned?int?m,?unsigned?int?n)
{
for?(int?i?=?0;?i??m;?++i)
{
for?(int?j?=?0;?j??n;?++j)
{
scanf("%f?",?a[i?*?n?+?j]);
}
}
return;
}
//隨機產(chǎn)生矩陣元素,均勻分布于[from?to]
void?matInitRand(double?*a,?unsigned?int?m,?unsigned?int?n,?double?from,?double?to)
{
if?(a?==?NULL?||?m?=?0?||?n?=?0)?return;
double?x;
srand(time(NULL));
for?(int?i?=?0;?i??m;?++i)
{
for?(int?j?=?0;?j??n;?++j)
{
x?=?(1.0?*?rand()?/?RAND_MAX)?*?(to?-?from)?+?from;
a[i?*?n?+?j]?=?x;
}
}
return;
}
//轉(zhuǎn)置
void?matTranspose(double?*a,?double?*b,?unsigned?int?m,?unsigned?int?n)
{
for?(int?i?=?0;?i??m;?++i)
{
for?(int?j?=?0;?j??n;?++j)
{
b[j*n?+i]=a[i?*?n?+?j]?;
}
}
}
//輸出矩陣
void?matPrint(double?*a,?unsigned?int?m,?unsigned?int?n)
{
for?(int?i?=?0;?i??m;?++i)
{
for?(int?j?=?0;?j??n;?++j)
{
printf("%8.4f?",?a[i?*?n?+?j]);
}
putchar('\n');
}
return;
}
//矩陣乘法c=a*b
void?matMul(double?*a,??double?*b,?double?*c,?unsigned?int?m,?unsigned??int?n,?unsigned?int?k)
{
if?(a?==?NULL?||?b?==?NULL?||?c?==?NULL?||?m?=?0?||?n?=?0?||?k?=?0)????return;
double?x?=?0.0f;
for?(int?i?=?0;?i??m;?++i)
{
for?(int?u?=?0;?u??k;?++u)
{
x?=?0.0f;
for?(int?j?=?0;?j??n;?++j)
{
x?+=?a[i?*?n?+?j]?*?b[j?*?k?+?u];
}
c[i?*?k?+?u]?=?x;
}
}
return;
}
//b=a^n,?a:m*m階矩陣
void?matFac(double?*a,?double?*b,?unsigned?int?n,?unsigned?int?m)
{
double?*c?=?(double?*)malloc(sizeof(double)?*?m?*?m);?//保存臨時結(jié)果
if?(n??1)
{
matFac(a,?c,?n?-?1,?m);
matMul(a,?c,?b,?m,?m,?m);
}
else
memcpy(b,?a,?sizeof(double)*m?*?m);
//?printf("%d:\n",n);
//?matPrint(b,?m,m);
free(c);????????????????????????????????????//回收內(nèi)存
return?;
}
#define?M?3
#define?N?4
#define?K?N
int?main(int?argc,?char?const?*argv[])
{
double?*A,?*B,?*B1,*BT,?*C;
A?=?matCreate(M,?N);
B?=?matCreate(N,?K);
B1?=?matCreate(N,?K);
BT?=?matCreate(K,N);
C?=?matCreate(M,?K);
if?(!A?||?!B?||?!B1?||?!BT?||?!C)?return?-1;
matInitRand(A,?M,?N,?0.0f,?1.0f);
printf("A=\n");
matPrint(A,?M,?N);
matInitRand(B,?N,?K,?0.0f,?1.0f);
printf("B=\n");
matPrint(B,?N,?K);
matTranspose(B,BT,N,K);
printf("B'=\n");
matPrint(BT,?K,N);
matMul(A,?B,?C,?M,?N,?K);
printf("C=A*B\n");
matPrint(C,?M,?N);
matFac(B,?B1,?4,?N);
printf("B^4\n");
matPrint(B1,?N,?K);
return?0;
}