table of Contents
To prove safety Offer- face questions 9 --- Fibonacci number
1, Question 1: Output Fibonacci number
Input n, output Fibonacci number is located in bit n value.
Recursive manner efficiency will be low, because there are a large number of repeat calculations. So at this time in order to push is a good idea.
My solution: front to back calculation, the calculated values are stored in the array.
#include <iostream>
#include <vector>
using namespace std;
long long CalculateNums(vector<long long> nums,unsigned int n)
{
nums[0] = 0;
nums[1] = 1;
//每次计算时先检测之前是否已经计算过
if(n<nums.size()){
return nums[n];
}
//接着从未计算过的位置开始计算
for(int i=nums.size();i<=n;i++){
nums.push_back(nums[i-1] + nums[i-2]);
}
return nums[n];
}
int main()
{
int n;
vector<long long> nums;
//给初始的两个数预留空间
nums.resize(2);
while(cin>>n){
if(n<0){
cout<<"输入范围错误"<<endl;
}
else{
cout<<CalculateNums(nums,n)<<endl;
}
}
return 0;
}
2, Topic 2: frog jump step 1
Frog jump step 1: one can choose a frog or a two-step. N-layer jump request jump method has several steps. (N <= 30)
In fact, on stairs and jump Fibonacci number is basically the same. Not repeat wrote.
3, Question 3: frog jump step 2
Frog jump step 2: one can choose frog 1,2,, n-1, n steps. N-layer jump request jump method has several steps. (N <= 30)
This question is considered a good rule: f (n) = 2 ^ (n-1)
My solution: I use the power of fast count
long long CalculateWays(unsigned int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
n--;
long long result = 1;
long long base = 2;
while(n != 0){
if(n%2 !=0){
result *= base;
}
base *= base;
n /= 2;
}
return result;
}
Gangster Solution: bit operation, a line of code to get!
long long CalculateWays(int n)
{
if(n==0)
return 0;
return 1<<(n-1);
}
4. Question 4: 3 steps frog jump
Frog jump step 3: 1 frogs can pick one or two-step. N-layer jump request jump method has several steps. (500> = n> = 100)
This question is clearly defined scope beyond the scope of long long, so use a large integer add operation.
My solution: long-winded large integer +
#include <iostream>
#include <string>
using namespace std;
string CalculateWays(unsigned int n);
string BigNumAdd(string s1,string s2);
int main()
{
int n;
cin>>n;
if(n<=0){
cout<<"输入范围错误";
}
else{
cout<<CalculateWays(n);
}
return 0;
}
string CalculateWays(unsigned int n)
{
if(n==1)
return "1";
if(n==2)
return "2";
string lastlast = "1";
string last = "2";
string result = "0";
for(int i=3;i<=n;i++){
result = BigNumAdd(lastlast,last);
lastlast = last;
last = result;
}
return result;
}
string BigNumAdd(string s1,string s2)
{
int len1 = s1.size()-1;
int len2 = s2.size()-1;
string result = "";
int carry = 0;
//先算两个数都有的位
while(len1>=0 && len2>=0){
int add = s1[len1] - 48 + s2[len2] - 48 + carry;
carry = add/10;
add%=10;
char addChar = add+48;
result = addChar + result;
len1--;
len2--;
}
//再算位数多的
while(len1>=0){
int add = s1[len1] - 48 + carry;
carry = add/10;
add%=10;
char addChar = add+48;
result = addChar + result;
len1--;
}
while(len2>=0){
int add = s2[len2] - 48 + carry;
carry = add/10;
add%=10;
char addChar = add+48;
result = addChar + result;
len2--;
}
//最后是否进位
if(carry > 0){
char addChar = carry+48;
result = addChar + result;
}
return result;
}
Gangster's solution: high-quality large integer +
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string BigNumAddString(string s1,string s2)
{
string& longStr = s1.size()>=s2.size()?s1:s2;
string& shortStr = s1.size()<s2.size()?s1:s2;
//给短的字符串前面填充0
shortStr.insert(0,longStr.size()-shortStr.size(),'0');
string result;
//给result预留足够的空间
result.resize(s1.size()+s2.size()+1);
//取各自最后一个字符的下标(要从后向前加)
int longIndex = longStr.size()-1;
int shortIndex = shortStr.size()-1;
int resultIndex = result.size()-1;
//进位标志
int carry = 0;
//加运算
while(shortIndex>=0){
int add = longStr[longIndex--] - '0' + shortStr[shortIndex--] - '0' + carry;
carry = add/10;
result[resultIndex--] = add%10 + '0';
}
//最后是否进位
if(carry > 0){
result[resultIndex--] = carry + '0';
}
//截取从这个参数下标开始一直到结尾(把前面多余的0截掉)
result = result.substr(resultIndex+1);
return result;
}
string CalculateWays(unsigned int n)
{
//也可以使用一个数组,把每次计算结果存进去
if(n==1)
return "1";
if(n==2)
return "2";
string lastlast = "1";
string last = "2";
string result = "0";
for(int i=3;i<=n;i++){
result = BigNumAddString(lastlast,last);
lastlast = last;
last = result;
}
return result;
}
int main()
{
int n;
cin>>n;
if(n<=0){
cout<<"输入范围错误"<<endl;
}
else{
cout<<CalculateWays(n);
}
return 0;
}
5 new knowledge
Some containers may be used string and resize pre-allocated space, so that the object can be used directly + [] in the form of assignment.