蓝桥杯每日一题2023.10.3

杨辉三角形 - 蓝桥云课 (lanqiao.cn)

题目描述

题目分析

40分写法:

可以自己手动构造一个杨辉三角,然后进行循环,用cnt记录下循环数的个数,看哪个数与要找的数一样,输出cnt

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10;
int a[N][N], x, cnt; 
int main()
{
	a[1][1] = 1;
	for(int i = 2; i <= 1000; i ++)
	{
		for(int j = 1; j <= i; j ++)
		{
			a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
		}
	}
	cin >> x;
	for(int i = 1; i <= 1000; i ++)
	{
		for(int j = 1; j <= i; j ++)
		{
			cnt ++;
			if(a[i][j] == x)
			{
				cout << cnt << '\n';
				return 0;
			}
		}
	}
	return 0;
}

50分写法:找规律(假定在第二列出现)

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
int main()
{
	cin >> n;
	cout << n * (n + 1) / 2 + 2;
	return 0;
}
/*
1	3	1 + 2
2	5	3 + 2
3	8	6 + 2
4	12 	10 + 2
5	17	15 + 2
...
n	n * (n + 1) + 2
*/

80分写法:上面两个结合

满分写法:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
ll c(int a, int b)//组合数
{
	ll res = 1;
	for(int i = b, j = 1; j <= a; j ++, i --)
	{
		res = res * i / j;
	}
	return res;

}
bool check(ll k)//找在k行的哪个数
{
	ll l = 2 * k, r = max(2 * k, n);
	while(l < r)
	{
		ll mid = l + r >> 1;
		if(c(k, mid) >= n)r = mid;
		else l = mid  + 1;
	}
	if(c(k, r) != n)return false;
	cout << r * (r + 1) / 2 + k + 1;
}
int main()
{
	cin >> n;
	int k = 16;
	while(true)
	{
		if(check(k))break;
		k --;
	}
	return 0;
}
相关推荐
源代码•宸22 分钟前
Leetcode—620. 有趣的电影&&Q3. 有趣的电影【简单】
数据库·后端·mysql·算法·leetcode·职场和发展
阿亮爱学代码3 小时前
Java 面试 (三)
面试·职场和发展
yaoh.wang7 小时前
力扣(LeetCode) 111: 二叉树的最小深度 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·深度优先
沐雪架构师7 小时前
大模型Agent面试精选题(第六辑)-Agent工程实践
面试·职场和发展
(●—●)橘子……9 小时前
记力扣42.接雨水 练习理解
笔记·学习·算法·leetcode·职场和发展
沐雪架构师10 小时前
大模型Agent面试精选题(第五辑)-Agent提示词工程
java·面试·职场和发展
Swift社区13 小时前
LeetCode 455 - 分发饼干
算法·leetcode·职场和发展
杜子不疼.15 小时前
【LeetCode 153 & 173_二分查找】寻找旋转排序数组中的最小值 & 缺失的数字
算法·leetcode·职场和发展
CSDN_RTKLIB15 小时前
【LeetCode 热题 HOT 100】两数之和
算法·leetcode·职场和发展
LYFlied1 天前
【每日算法】LeetCode 153. 寻找旋转排序数组中的最小值
数据结构·算法·leetcode·面试·职场和发展