l37.1a

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· 1A 代码 · 共 58 行

1A
58
字号
#printLet's try a recursive function.  Write a subroutine    power(x,n)which computes x to the power n by the followingalgorithm: 1. if n is zero return 1. 2. if n is odd return x*power(x,n-1). 3. if n is even return the square of    power(x,n/2).You may assume than x and n are integers, n>=0.If n is negative return 0 for an answer.Put your routine on a file "power.c".  Compileit and test it; then type "ready".#once #create tzaqc.cmain(){if (power(-1,-1) != 0) 		return(1);	if (power(-3,2) != 9) 		return(1);	if (power(2,12) != 4096) 		return(1);	if (power(3,5) !=  243) 		return(1);	if (power(-5, 5) != -3125) 		return(1);	if (power(7,3) != 343) 		return(1);	if (power(7,4) != 2401) 		return(1);	if (power(3,7) != 2187) 		return(1);	if (power(2,10) != 1024) 		return(1);	return(0);}#usercc tzaqc.c power.oa.out#succeed/*  a possible solution */power(x, n){	int k;	if (n < 0)		return(0);	if (n == 0)		return(1);	if (n%2 == 1)		return(x * power(x, n-1));	k = power(x, n/2);	return(k*k);}#log#next40.1a 10

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?