class Solution { public: int Fibonacci(int n) { int i=2; int prev=0,cur=1; if(n==0) return 0; if(n==1) return 1; while(i++<=n) { int tmp=cur; cur=cur+prev; prev=tmp; } return cur; } };
class Solution1 { public: int NumberOf1(int n) { int i=0; int tag=1; int count=0; while(i++<32){ if((n&(tag))!=0) count++; tag=tag<<1; } return count; } }; //最优解 class Solution { public: int NumberOf1(int n) { int i=0; while(n){ n=(n-1)&n; i++; } return i; } };
class Solution { public: double Power(double base, int exponent) { if(exponent==0) return 1; if(exponent<0&&base!=0) return 1/base*Power(base,exponent+1); return base*Power(base,exponent-1); } };
int Numadd(char* num, int bit) { int i = bit,needcarry=1; char carry = 1; while (i >0) { num[i-1]+=carry ; num[i] =‘0‘; if (num[i - 1] <= ‘9‘) { needcarry = 0; break; } --i; } if (needcarry == 1) { num[0] = ‘1‘; num[bit + 1] = ‘0‘; } return needcarry; } void Print1toN(int n) { if (n <= 0) return; char num[MAX_SIZE] = {0}; int i = 0; while (1) { if (num[i]<‘0‘) num[i] = ‘0‘; if (++num[i]>‘9‘) { if (Numadd(num, i) == 1) ++i; } if (i == n) break; cout << num << ‘ ‘; //_sleep(100); } cout << endl; }
时间: 2024-10-13 19:07:34