用迴圈方法設置一funtion anna(), 讓使用者輸入一個大於零的整數,
若為奇數 則 anna(n) = (3*n + 1)/2 + anna(n-1)
若為偶數 則 anna(n) = n/2 + anna(n-1)
若為零 則 anna(0) = 0
結果如下所示:
Enter a positive integer (q to quit).
Input :7
Output : 0 + 2 + 1 + 5 + 2 + 8 + 3 + 11 = 32
Input : -6
You should enter a positive integer!
Input :3
Output : 0 + 1 + 5 = 8
Input :q
Bye!
歡迎光臨 亞寶論壇 (http://bbs.ahpal.com/) | Powered by Discuz! 7.0.0 |