April 21, 2016 Baidu iOS intern online pen Questions & amp; programming questions, 2016 ios

Source: Internet
Author: User

April 21, 2016 Baidu iOS intern online pen Questions & programming questions, 2016 ios

1. How many steps can a person go up one, two, or three steps at a time?

Idea: first establish a mathematical model, set up three steps to go I, two steps to go j times, one step to go k times, 3 * I + 2 * j + 1 * k = n steps. taking I + j + k times in total, equals to dividing the length of n steps into I + j + k paragraphs, and then entering I 3, j 2, k 1. in this way, when divided into I + j + k paragraphs, according to the knowledge of arrangement and combination, all filling methods are (I + j + k )! /(I! * J! * K !) In the program, use the GetComb (I, j, k) function to calculate this value.
For the determination of I, j, k, we can use the large to small division, divide the number of times by 3 first, then divide the number by 2, and the rest are counted as the number of times by 1, the specific program contains the I, j, and two repeating loops.

1 # include <iostream> 2 # include <cstdio> 3 int Factorial (int n) 4 {5 int ret = n; 6 if (n <= 1) 7 {8 return 1; 9} 10 while (n --> 1) 11 {12 ret * = n; 13} 14 return ret; 15} 16 17 // calculate (I + j + k )! /(I! * J! * K !) 18 19 int GetComb (int I, int j, int k) 20 {21 int m = Factorial (I + j + k); 22 int l = Factorial (I) * Factorial (j) * Factorial (k); 23 return m/l; 24} 25 26 27 int NStepFor123 (int n) 28 {29 int I = 0; 30 int j = 0; 31 int p; 32 int k; 33 int result = 0; 34 for (I = 0; I <= n/3; I ++) 35 {36 p = n-I * 3; 37 for (j = 0; j <= p/2; j ++) 38 {39 k = p-j * 2; 40 // evaluate (I + j + k )! /(I! * J! * K !) 41 result + = GetComb (I, j, k); 42} 43} 44 return result; 45} 46 int main (int argc, const char * argv []) {47 // insert code here... 48 printf ("% d", NStepFor123 (32); 49 return 0; 50}

There can be another method for this question.

F (n) = f (n-1) + f (n-2) + f (n-3), especially f (0) = 1; f (1) = 1; f (2) = 2;
The formula is as follows: when we add a common step of f (n + 1), there are three situations after we calculate this new step, 1 is that this step is only taken as f (n) Times, 2 is that this step matches the original last step as the two steps are taken as f (n-1) times, 3 is this step with the previous two steps for three steps for f (n-2); so the formula f (n + 1) = f (n) + f (n-1) + f (n-2), summed up evidence.

int f (int k){   int v[3]={1,1,2};    long index = -1;    if (k<0)    {        return 0;    }        if (k<3)    {        return v[k];    }        while(k-->2)    {        index++;        index %= 3;        v[index] = v[0]+v[1]+v[2];    }    return v[index];}int main(int argc, const char * argv[]) {    printf("%d",f(32));    return 0;}

The strange thing is that the two programs run different results.

First 1 ~ found during debugging ~ 13. The results are consistent. Because of the second type of O (n), it should be the first type of problem.

Int Factorial (int n) int cannot be mounted with ret, so an error occurs. If you change it to the long type, it will be OK.
The Code is as follows:
Long Factorial (int n) {long ret = n; if (n <= 1) {return 1 ;}while (n --> 1) {ret * = n ;} return ret;} // calculate (I + j + k )! /(I! * J! * K !) Double GetComb (int I, int j, int k) {double m = Factorial (I + j + k); double l = Factorial (I) * Factorial (j) * Factorial (k); return m/l;} long NStepFor123 (int n) {int I = 0; int j = 0; int p; int k; long result = 0; for (I = 0; I <= n/3; I ++) {p = n-I * 3; for (j = 0; j <= p/2; j ++) {k = p-j * 2; // evaluate (I + j + k )! /(I! * J! * K !) Result + = GetComb (I, j, k) ;}} return result ;}int main () {printf ("% ld", NStepFor123 (32); return 0 ;}
However, we found that the values are still not equal at 32, which means that the ret obtained by the factorial operation is greater than the value of long, And int64_t is not enough, so we need to simulate large numbers with strings (it will be very troublesome)
We recommend that you use the second recursive method to solve the problem.

Related Article

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.