C + +; some questions per week (2)

Source: Internet
Author: User


650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/7F/DF/wKiom1cwQm6hRaQ2AAKd4HogHfc459.jpg "style=" float: none; "title=" QQ picture 20160507133124.jpg "alt=" Wkiom1cwqm6hraq2aakd4hoghfc459.jpg "/>

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;    }};

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/DC/wKioL1cwQ03h6KxvAAO9FAwQAF0448.jpg "style=" Float:none, "title=" QQ picture 20160507133128.jpg "alt=" wkiol1cwq03h6kxvaao9fawqaf0448.jpg "/>

Class solution1 {public:     int  numberof1 (int n)  {                    int i=0;         int tag=1;          int count=0;         while (i++ &LT;32) {             if ((n& (tag))!=0)                  count++;              tag=tag<<1;                        }         return count;      }};//Optimal Solution Class soluTion {public:     int  numberof1 (int n)  {          int i=0;         while (n ) {             n= (n-1) &n;              i++;          }         return i;      }};

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/7F/DF/wKiom1cwQnOgUj87AAKoaKxZrbM902.jpg "style=" float: none; "title=" QQ picture 20160507133136.jpg "alt=" Wkiom1cwqnoguj87aakoakxzrbm902.jpg "/>

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); }};

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7F/DC/wKioL1cwQ1GgGnjmAAK5bqBsklQ112.jpg "style=" float: none; "title=" QQ picture 20160507133140.jpg "alt=" Wkiol1cwq1gggnjmaak5bqbsklq112.jpg "/>


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;}


650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/7F/DC/wKioL1cwQ1bgi6hYAAMdhCblyWU547.jpg "style=" float: none; "title=" QQ picture 20160507133148.jpg "alt=" Wkiol1cwq1bgi6hyaamdhcblywu547.jpg "/>


C + +; some questions per week (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.