XM10. 爬楼梯
描述
在你面前有一个n阶的楼梯,你一步只能上1阶或2阶。输入描述
一个正整数n(n<=100),表示这个楼梯一共有多少阶输出描述
一个正整数,表示有多少种不同的方式爬完这个楼梯示例1
输入:
5
输出:
8
C 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2021-10-14
#include <limits.h> int main(){ int n; scanf("%d",&n); char**d=(char**)malloc(n*sizeof(char*)); d[0]=(char*)malloc(50*sizeof(char)); d[0]="1"; d[1]=(char*)malloc(50*sizeof(char)); d[1]="1"; for(int i=2;i<=n;i++){ int c=0; d[i]=(char*)malloc(50*sizeof(char)); int l1=strlen(d[i-1]); int l2=strlen(d[i-2]); // printf("%d %d\n",l1,l2); int l=0; while(l<l2){ int x=d[i-1][l]-'0'+d[i-2][l]-'0'+c; if(x>=10){ c=1; x=x%10; } else{ c=0; } d[i][l++]=x+'0'; } while(l<l1){ int x=d[i-1][l]-'0'+c; if(x>=10){ c=1; x=x%10; } else{ c=0; } d[i][l++]=x+'0'; } if(c==1){ d[i][l++]='1'; } d[i][l]='\0'; // printf("%s\n",d[i]); } int l=strlen(d[n]); for(int i=l-1;i>=0;i--){ printf("%c",d[n][i]); } return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-08-29
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int a[500][100] = { 0 }; int main() { int n, i, j, r, k = 0, t, p = 0; scanf("%d", &n); a[0][99] = 1, a[1][99] = 1, a[2][99] = 2;//, a[3][99] = 2; for (j = 3; j <= n; ++j) { for (r = 99; r >= 0; --r) { t = a[j - 1][r] + a[j - 2][r] + p; if (t >= 10) { p = t / 10; a[j][r] = t % 10; } else { a[j][r] = t; p = 0; } } } for (j = 0; j <= 99; ++j) { if (k || a[n][j]) { k = 1; printf("%d", a[n][j]); } } }
C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-08-22
#include <stdio.h> int a[500][100]; int main() { int n, i, j, r, k = 0, t, p = 0; scanf("%d", &n); a[0][99] = 1, a[1][99] = 1, a[2][99] = 2;//, a[3][99] = 2; for(j = 3; j <= n; ++j) { for(r = 99; r >= 0; --r) { t = a[j-1][r] + a[j-2][r] + p; if(t >= 10) { p = t/10; a[j][r] = t%10; } else { a[j][r] = t; p = 0; } } } for(j = 0; j <= 99; ++j) { if(k || a[n][j]) { k = 1; printf("%d", a[n][j]); } } }
C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-08-21
#include<stdio.h> #define MAX 101 typedef unsigned long long uint64; #define maxdll (uint64)1e19 int ansh[MAX] = {0}; uint64 ansl[MAX] = {1,1}; void ullAdd (uint64* ansl, int* ansh, int i) { ansl[i] = ansl[i-2]+ansl[i-1]; ansh[i] = ansh[i-2]+ansh[i-1]; if (ansl[i]>=maxdll) { ansh[i] ++; ansl[i] = ansl[i]-maxdll; } } int main() { for(int i=1;i<MAX;i++) { ansh[i] = 0; } int n = 0; scanf("%d",&n); for (int i=2;i<=n;i++) ullAdd(ansl,ansh,i); if (ansh[n]>0) printf("%llu%llu",ansh[n],ansl[n]); else printf("%llu",ansl[n]); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-08-19
#include <stdio.h> int a[500][100]; int main() { int n, i, j, r, k = 0, t, p = 0; scanf("%d", &n); a[0][99] = 1, a[1][99] = 1, a[2][99] = 2;//, a[3][99] = 2; for(j = 3; j <= n; ++j) { for(r = 99; r >= 0; --r) { t = a[j-1][r] + a[j-2][r] + p; if(t >= 10) { p = t/10; a[j][r] = t%10; } else { a[j][r] = t; p = 0; } } } for(j = 0; j <= 99; ++j) { if(k || a[n][j]) { k = 1; printf("%d", a[n][j]); } } }