输入只有1行,即整数N。
可能有多组测试数据,对于每组数据, 输出只有1行,即名名吃巧克力的方案数。
4
5
直接暴力dfs穷举
using namespace std;
int N, solution;
void dfs(int rem, int eat){
if(rem < eat){
return;
}
if(rem == eat){
solution++;
return;
}
dfs(rem - eat, 1);
dfs(rem - eat, 2);
}
int main(){
while(cin >> N){
solution = 0;
dfs(N, 0);
cout << solution << endl;
}
return 0;
}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] record =new int[21]; record[1]=1; record[2]=2; for (int i = 3; i <= n; i++) { record[i]=record[i-1]+record[i-2]; } System.out.println(record[n]); } }
#include <cstdio> #include <iostream> #include <cmath> #include <vector> using namespace std; int manage(int k){ if(k == 1) return 1; else if(k == 2) return 2; return manage(k-1)+manage(k-2); } int main(){ int k; cin>>k; int sum; sum = manage(k); cout<<sum; return 0; }
#include<iostream> using namespace std; int main(){ int n; cin>>n; int* candy=new int[n+1]; candy[1]=1; candy[2]=2; for(int i=3;i<=n;i++) candy[i]=candy[i-1]+candy[i-2]; cout<<candy[n]<<endl; }
#include<iostream> using namespace std; int function(int n) { if(n == 0)//终止条件一 return 1; else if(n < 0)//终止条件二 return 0; else return function(n - 2) + function(n - 1);//每天只有吃一块或者两块这两种方法 } int main() { int n; int cnt; cin >> n; cnt = function(n); cout << cnt << endl; return 0; }
#include<iostream> #include<cstdio> using namespace std; const int maxn = 21; int num[maxn]; int main() { num[1] = 1; num[2] = 2; for(int i = 3; i < maxn; ++i) { num[i] = num[i-1]+num[i-2]; } int n; while(cin >> n) { cout << num[n] << endl; } return 0; }
#include<stdio.h> int select(int a,int b){ if(b==0){ return 1; } int result,temp1=1,temp2=1; int i; for(i = a; i > a-b; i--){ temp1 *= i; } for(i = b; i>0; i--){ temp2 *= i; } result = temp1/temp2; return result; } int main(){ int n; while(scanf("%d",&n)!=EOF){ int b[20] = {0}; if(n%2==0){ for(int i = n/2; i <= n; i++){ b[i] = select(i, n-i); } }else{ for(int i = n/2+1; i<=n; i++){ b[i] = select(i, n-i); } } int sum = 0; for(int i = 0; i <= n; i++){ sum += b[i]; } printf("%d\n", sum); } return 0; }
#include <iostream> using namespace std; // 动态规划吃糖果 int dp(int n) { // 返回吃巧克力的方案数目 if (n == 1) // 只有一块巧克力的情况 return 1; if (n == 2) // 只有两块巧克力的情况 return 2; /* 吃n块巧克力的方案数=最后一次是吃一块巧克力的方案数+最后一次是吃两块巧克力的方案数 =吃n-1块巧克力的方案数+吃n-2块巧克力的方案数 */ return dp(n - 1) + dp(n - 2); } int main() { int n; while (cin >> n) { cout << dp(n); } }