4
//#include<iostream> //#include<cstdio> #include<stdio.h> //蓝色部分为调试时所写 int ans, num; int cnt = 0, count = 0; void dg(int x, int n1, short flag) { //问题终于找出来了,对于n1的每次增加必须在确定本次所铺的瓷砖为1时, 然后在下一次进行增加,否则会导致本次可能多次自增 if(x == 0 && !flag) {ans++; num += n1; /*cout << "第" << ++count << "次成功------"<< "n1: " << n1 << "出栈" << endl; */return ;} else if(x == 0 && flag) {n1++; ans++; num += n1; return ;} //cout <<"第" << ++cnt << "次 n1: " << n1 << " flag: " << flag << " ans: " << ans << endl; if(flag) n1++; for(int i = 1; i <= 3; i++) if(x >= i) { // cout << "i -- " << i << endl; if( i == 1 && !flag) {flag = 1;/*cout << "入栈 " ;*/dg(x-i, n1, flag);} else if(i == 1 && flag) ; else {flag = 0; /*cout << "入栈 " ;*/dg(x-i, n1, flag);} } // cout << " 出栈 " << endl; 一次函数的结束会减少一个栈帧,勉强写为"出栈" } int main() { int N; while(scanf("%d", &N) != EOF) { ans = num = 0; dg(N, 0, 0); printf("%d\n%d\n", ans, num); } return 0; }
原文地址:http://blog.csdn.net/czkct/article/details/45715489