蓝桥杯每日一题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;
}
相关推荐
qq_459234423 天前
【题库】| 商用密码应用安全性评估从业人员考核题库(四十)
职场和发展·密码学·学习方法·考核·商用密码·商用密码应用安全性评估·密评
敲敲了个代码3 天前
[特殊字符] 空数组的迷惑行为:为什么 every 为真,some 为假?
前端·javascript·react.js·面试·职场和发展
诚思报告YH3 天前
视频面试软件市场洞察:2026 - 2032年复合年均增长率(CAGR)为10.3%
面试·职场和发展
重生之后端学习3 天前
74. 搜索二维矩阵
开发语言·数据结构·算法·职场和发展·深度优先
tyb3333333 天前
leetcode:吃苹果和队列
算法·leetcode·职场和发展
Pitiless-invader3 天前
MySQL 相关知识及面试问题汇总
面试·职场和发展
重生之后端学习3 天前
35. 搜索插入位置
java·数据结构·算法·leetcode·职场和发展·深度优先
逆境不可逃3 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
筱昕~呀3 天前
冲刺蓝桥杯-DFS板块(第二天)
算法·蓝桥杯·深度优先
zheshiyangyang3 天前
前端面试基础知识整理【Day-10】
前端·面试·职场和发展