2.1.17
PlusOne 实现加几操作
#include<stdio.h> int* plusOne(int A[],int length,int number) { int* index = &A[length-1]; int i; int forward; for(i=length;i>0;i--) { if(i==length) { *index=*index+number; }else { *index=*index+forward; } forward=*index/10; if(forward==0) { break; } *index=*index%10; index--; } return A; } void main() { int A[5]={4,2,7,1,9}; int* b = plusOne(A,5,111); printf("%d,%d,%d,%d,%d",b[0],b[1],b[2],b[3],b[4]); }
2.1.18
Climbing Stairs
CS和CS2是不同的前者是递归,后者是基于循环
#include<stdio.h> int CS(int sumStairs) { if(sumStairs==1) { return 1; } if(sumStairs==2) { return 2; } return CS(sumStairs-1)+CS(sumStairs-2); } int CS2(int sumStairs) { int pre=0; int cur=1; int tmp; int i=1; for(;i<=sumStairs;i++) { tmp=cur; cur=cur+pre; pre=tmp; } return cur; } void main() { int sum=CS2(10); printf("%d",sum); }
时间: 2024-11-07 10:34:11