#include<stdio.h> #include<string.h> //排列组合,阶梯总数为1和2的组合int zuhe(int n,int m){ int t,i,r=1; if(n==0||m==0) return 1; if(n>m){ t=n; n=m; m=t; } for(i=n;i>0;i--){ r*=(m+i); } for(i=1;i<=n;i++){ r/=i; } return r; }int main(){ int m,n,N,d; long long c; while(scanf("%d",&N)!=EOF){ m=N;//1的个数 n=0;//2的个数 c=0;//总方案数 d=0;//每种情况的方案数 while(m>1){ d=zuhe(n,m);//组合数 c+=d; m-=2; n++; } d=zuhe(n,m); c+=d; printf("%lld\n",c);}}
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int n = in.nextInt(); long[] fab = new long[n+1]; fab[0] = 0; if(n > 0){ fab[1] = 1; } if(n > 1){ fab[2] = 2; } for(int i = 3; i <= n; i++){ fab[i] = fab[i-1] + fab[i-2]; } System.out.println(fab[n]); } } }
import java.util.Scanner; public class Main { // 使用动态规划解法 // 状态转移方程 dp[i]=dp[i-1]+dp[i-2] public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); // n+2 是为了防止数组下标越界,如输入1时 int[] dp = new int[n+2]; dp[1]= 1; dp[2] = 2; for (int i = 3; i <= n; i++) dp[i]=dp[i-1]+dp[i-2]; System.out.println(dp[n]); } }
#include<stdio.h> int main(){ int n; int dp[90]; dp[1] = 1; dp[2] = 2; for(int i=3; i<90; i++) dp[i] = dp[i-1] + dp[i-2]; while(scanf("%d",&n)!=EOF) printf("%d\n",dp[n]); return 0; }
/* 更快更强?RH @L W Q time@2019/9/817:09 为什么return bool? */ #include <stdio.h> #include <string.h> using namespace std; int lwq[91]; bool get(){ memset(lwq,0,sizeof(lwq)); lwq[1] = 1; lwq[2] = 2; for(int i = 3;i<91;i++) lwq[i] = lwq[i-1]+lwq[i-2]; return true; } int main(){ int n; get(); while(scanf("%d",&n)!=EOF){ printf("%d\n",lwq[n]); } return 0; }
#include <iostream> using namespace std; int main() { int N; long long total = 1; long long f = 0; while(cin >> N) { for(int i = 0; i < N; i++) { total = total + f; f = total - f; } cout << total << endl; total = 1; f = 0; } return 0; }
import java.math.BigInteger;
1.valueOf(parament); 将参数转换为制定的类型 2.add(); 大整数相加 a.add(b); 3.subtract(); 相减 4.multiply(); 相乘 5.divide(); 相除取整 6.remainder(); 取余 7.pow(); a.pow(b)=a^b 8.gcd(); 最大公约数 9.abs(); 绝对值 10.negate(); 取反数 11.mod(); a.mod(b)=a%b=a.remainder(b); 12.max(); min(); 13.punlic int comareTo(); 14.boolean equals(); 是否相等
代码如下:
BigInteger t1 = new BigInteger("1"); BigInteger t2 = new BigInteger("2"); BigInteger result = new BigInteger("0"); for(int i = 3;i<=N;i++){ result = t1.add(t2); t1 = t2; t2 = result; } System.out.println(result);
//注意int型的数据会溢出,定义成long long类型; #include<iostream> using namespace std; long long upper(int N) { if(N<=0) return 0; if(N==1) return 1; if(N==2) return 2; long long a=1; long long b=2; long long temp; for(int i=3;i<=N;i++) { temp=a+b; a=b; b=temp; } return temp; } int main() { int N; while(cin>>N) { cout<<upper(N)<<endl; } return 0; }