📄 l37.1a
字号:
#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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -