这道题一开始只用到两个循环,但其实没搞清楚题目的意思,我只是求出来了怎么搭配1和2去完成题目给的条件,但并没有考虑1和2是在哪一步走的。调试发现这个问题后,我便用排列组合去测试用例2中的10级台阶的89,因此修改了我的代码,传进了两个参数,进行排列组合,最后将他们相加,就是答案,但是过程中也遇到一些问题,比如求C73时,一开始先得到7/3这样子的除法会丢失精度,所以只能先求出7*6*5再去除以3*2*1,所以可以看到我的排列组合函数写了好几个循环,写完看了下大家答案,都用递归,发现确实思路上来讲更胜一筹。
考虑状态值,我还是得加强练习!
再补充一下,虽然我用例全过了,但我用了long int才过的,不知道为什么用递归大家用int也能过;
#include <stdio.h> long int sortgroup(int a,int b){ long int sum = 1; int c = a+b; if(a == 0 || b == 0){ sum = 1; } else if(a<b){ int temp = a; while(temp != 0){ sum *= c; c--; temp--; } temp = a; while(temp != 0){ sum /= temp; temp--; } } else{ int temp = b; while(temp != 0){ sum *= c; c--; temp--; } temp = b; while(temp != 0){ sum /= temp; temp--; } } return sum; } int main(){ int n; scanf("%d",&n); long int count = 0; for(int i = 0;i<=n;i++){ for(int j = 0;j<= n/2;j++){ if(i + j*2 == n){ count += sortgroup(i,j); } } } printf("%ld",count); return 0; }