每日一题

59 篇文章 0 订阅
49 篇文章 1 订阅

一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

f(1) = 1

f(2) = f(2-1) + f(2-2) //f(2-2) 表示2阶一次跳2阶的次数。

f(3) = f(3-1) + f(3-2) + f(3-3)

f(n) = f(n-1) + f(n-2) + f(n-3) + … + f(n-(n-1)) + f(n-n)

说明:

1)这里的f(n) 代表的是n个台阶有一次1,2,…n阶的 跳法数。

2)n = 1时,只有1种跳法,f(1) = 1

  1. n = 2时,会有两个跳得方式,一次1阶或者2阶,这回归到了问题(1) ,f(2) = f(2-1) + f(2-2)

  2. n = 3时,会有三种跳得方式,1阶、2阶、3阶,

    那么就是第一次跳出1阶后面剩下:f(3-1);第一次跳出2阶,剩下f(3-2);第一次3阶,那么剩下f(3-3)

    因此结论是f(3) = f(3-1)+f(3-2)+f(3-3)

  3. n = n时,会有n中跳的方式,1阶、2阶…n阶,得出结论:

    f(n) = f(n-1)+f(n-2)+…+f(n-(n-1)) + f(n-n) => f(0) + f(1) + f(2) + f(3) + … + f(n-1)

  4. 由以上已经是一种结论,但是为了简单,我们可以继续简化:

    f(n-1) = f(0) + f(1)+f(2)+f(3) + … + f((n-1)-1) = f(0) + f(1) + f(2) + f(3) + … + f(n-2)

    f(n) = f(0) + f(1) + f(2) + f(3) + … + f(n-2) + f(n-1) = f(n-1) + f(n-1)

    可以得出:

    f(n) = 2*f(n-1)

class Solution {
public:
int jumpFloorII(int number) {
	if(number <= 0)
		return 0;
		int res = 1;
	for(int i = 1; i < number; ++i)
	{
		res *= 2;
	}
	return res;
	//return 1<<(number - 1);
	}
};

小喵们很喜欢把自己装进容器里的(例如碗),但是要是碗的周长比喵的身长还短,它们就进不去了。

现在告诉你它们的身长,和碗的半径,请判断一下能否到碗里去。

输入描述:
输入有多组数据。

每组数据包含两个整数n (1≤n≤2^128) 和r (1≤r≤2^128),分别代表喵的身长和碗的半径。

圆周率使用3.14。

输出描述:
对应每一组数据,如果喵能装进碗里就输出“Yes”;否则输出“No”。
示例1
输入
6 1
7 1
9876543210 1234567890
输出
Yes
No
No
方法一:

#include <iostream>
using namespace std;
int main(){
	double n,r;
	while(cin >> n >> r){
	//周长:2*r*3.1415
	//身长:n
	if(n > (2*r*3.1415))
		cout<<"No"<<endl;
	else
		cout<<"Yes"<<endl;
	}
	return 0;
}

题目本质考察大数计算
方法二:

#include <iostream>
#include <string>

using namespace std;
string Area(string &s)
{
	int slen = s.size();
	string str1(slen + 3, 0);
	string str2 = "628";
	int a = 0;
	for(int m = 2; m>=0 ;--m)
		for (int i = slen - 1,j = str1.size() + m -3; i >= 0; --i,--j)
		{
			str1[j] += (s[i] - '0')*(str2[m] - '0');
		}
	for (int i = str1.size(); i >= 0; --i)
	{
		str1[i] += a;
		a = str1[i] / 10;
		str1[i] = str1[i] % 10 + '0';
	}
 
	int index = 0;
	while (str1[index] == '0')
		++index;
	return string(str1.begin() + index, str1.end() - 2);
}

int compare(string& s1, string &s2)
{
	if (s1.size() != s2.size())
	{
		return (s1.size() > s2.size() ? 1 : -1);
	}
	else
	{
		for (int i = 0; i < s1.size(); ++i)
		{
			if (s1[i] == s2[i])
				continue;
			else
				return (s1[i] > s2[i] ? 1 : -1);
		}
		return 0;
	}
}

int main()
{
	string s1, s2;
	while (cin >> s1 >> s2)
	{
		s2 = Area(s2);
		if (compare(s1, s2) == 1)
			cout << "No" << endl;
		else
			cout << "Yes" << endl;

	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值