Problem: Given a n-step ladder, we can climb 1 or 2 steps each time. Please print out all possible climbing ways to reach step-n. For example n = 3; the output should be 1 2 1 1 1 2 1 My solution is attached in the end. Could you please help me improving the code. Thank you a lot. ======================================= void printFioStep(int n) { assert(n > 0);
去一些大公司会好点吗?象 TY LIN. 美国做到SENIOR好象到头了,10万封顶,这辈子就这样了。
p*r
4 楼
呵呵,这题应该用DP,从后往前推 慢慢想想吧。。。
out the
【在 e******d 的大作中提到】 : Problem: : Given a n-step ladder, we can climb 1 or 2 steps each time. Please print out : all possible climbing ways to reach step-n. : For example n = 3; the output should be : 1 2 : 1 1 1 : 2 1 : My solution is attached in the end. Could you please help me improving the : code. : Thank you a lot.
Z*g
5 楼
炒股?
【在 B***i 的大作中提到】 : 去一些大公司会好点吗?象 TY LIN. : 美国做到SENIOR好象到头了,10万封顶,这辈子就这样了。
X*r
6 楼
I didn't look closely at your code, but it seems way too complicated at first glance. Why don't you just use recursion? The code should be very simple, like: void printStepsImpl(int x[], int k, int n) { if (n--) { x[k] = 1; printStepsImpl(x, k+1, n); if (n--) { x[k] = 2; printStepsImpl(x, k+1, n); } } else { int i; for (i = 0; i < k; i++) { printf("%d ", x[i]); } printf("\n"); } } void printSteps(int n) { int x[n]; // gcc extension, or t
【在 e******d 的大作中提到】 : Problem: : Given a n-step ladder, we can climb 1 or 2 steps each time. Please print out : all possible climbing ways to reach step-n. : For example n = 3; the output should be : 1 2 : 1 1 1 : 2 1 : My solution is attached in the end. Could you please help me improving the : code. : Thank you a lot.
B*i
7 楼
你在炒吗?战果如何?
【在 Z****g 的大作中提到】 : 炒股?
z*e
8 楼
红猪侠是不是搞竞赛出身的? 家里是不是教育工作者知识分子家庭啊?
【在 X****r 的大作中提到】 : I didn't look closely at your code, but it seems way too complicated : at first glance. Why don't you just use recursion? The code should be : very simple, like: : void printStepsImpl(int x[], int k, int n) { : if (n--) { : x[k] = 1; : printStepsImpl(x, k+1, n); : if (n--) { : x[k] = 2; : printStepsImpl(x, k+1, n);
【在 X****r 的大作中提到】 : I didn't look closely at your code, but it seems way too complicated : at first glance. Why don't you just use recursion? The code should be : very simple, like: : void printStepsImpl(int x[], int k, int n) { : if (n--) { : x[k] = 1; : printStepsImpl(x, k+1, n); : if (n--) { : x[k] = 2; : printStepsImpl(x, k+1, n);