题目描述
Every positive number can be presented by the exponential form.For example, 137 = 2^7 + 2^3 + 2^0。 Let's present a^b by the form a(b).Then 137 is presented by 2(7)+2(3)+2(0). Since 7 = 2^2 + 2 + 2^0 and 3 = 2 + 2^0 , 137 is finally presented by 2(2(2)+2 +2(0))+2(2+2(0))+2(0). Given a positive number n,your task is to present n with the exponential form which only contains the digits 0 and 2.
输入描述:
For each case, the input file contains a positive integer n (n<=20000).
输出描述:
For each case, you should output the exponential form of n an a single line.Note that,there should not be any additional white spaces in the line.
示例1
输入
复制
1315
输出
复制
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
int Simple(int n,int i)
{
return (n>>i)&1;
}
void print(int n)
{
bool first=true;
for(int i=15;i>=0;i--)
{
if(Simple(n, i))
{
if(!first)
{
printf("+");
}
else
first=false;
if(!i)
{
printf("2(0)");
}
else if(i==1)
{
printf("2");
}
else
{
printf("2(");
print(i);
printf(")");
}
}
}
}
int main(){
int n;
scanf("%d",&n);
print(n);
cout<<endl;
}
题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
//根据上一个题目:青蛙只跳1或2可以得出是一个斐波那契问题,
//即a[n]=a[n-1]+a[n-2],那么能跳1,2,3个台阶时a[n]=a[n-1]+a[n-2]+a[n-3],......
//依次类推,能推出本题的a[n]=a[n-1]+a[n-2]+......+a[1];由此得出代码:
int jumpFloor(int number) {
int a[10001];
a[0]=1;
a[1]=1;
a[2]=2;
for(int i=3;i<10001;i++)
a[i]=2*a[i-1];
return a[number];
}
int main()
{
printf("%d",jumpFloor(4));
}
题目描述
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?(最笨的方法: 穷举)
class Solution {
public:
int rectCover(int number) {
int a[10001];
a[1]=1;
a[2]=2;
for(int i=3;i<10001;i++)
{
a[i]=a[i-1]+a[i-2];
}
return a[number];
}
};