再帰処理 例2
コード #include <iostream> using namespace std; /* 再起処理 1からnまでの和 */ int mysum(int n){ int buff; printf("mysum n = %d\n", n); if(n == 1){ return 1; }else{ buff = mysum(n - 1) + n; printf("mysum buff = %d\n", buff); return buff; } } void main() { int n = 10; printf("mysum = %d\n",mysum(n)); return; }
結果 mysum n = 10 mysum n = 9 mysum n = 8 mysum n = 7 mysum n = 6 mysum n = 5 mysum n = 4 mysum n = 3 mysum n = 2 mysum n = 1 mysum buff = 3 mysum buff = 6 mysum buff = 10 mysum buff = 15 mysum buff = 21 mysum buff = 28 mysum buff = 36 mysum buff = 45 mysum buff = 55 mysum = 55