蓝桥杯每日一题2023.10.20

题目描述

等差数列 - 蓝桥云课 (lanqiao.cn)

知识点:

排序找出最大公约数则为公差

项数: n = (第n项值-首项) / 公差 +1。 n = (an-a1) / d+1
cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int a[N], n;
int gcd(int a, int b)
{
	return a ? gcd(b % a, a) : b;
}
int main()
{
	cin >> n;
	for(int i = 1; i <= n; i ++)
	{
		cin >> a[i];
	}
	sort(a + 1, a + 1 + n);
	int d = 0;
	for(int i = 2; i <= n; i ++)
	{
		d = gcd(d, a[i] - a[i - 1]);	
	}	
	if(d == 0)cout << n;
	else cout << (a[n] - a[1]) / d + 1;
	return 0;
}
相关推荐
Dream it possible!12 分钟前
LeetCode_热题100_知识点总结
算法·leetcode·职场和发展
杨逢昌工厂6S管理2 小时前
107-杨逢昌钣金车间换型管控:从物料切换杂乱到有序换型的四步技术方案
经验分享·笔记·职场和发展·学习方法
ychqsq10 小时前
203.诊断
经验分享·职场和发展
ychqsq1 天前
201.春风
经验分享·职场和发展
泡沫_花火1 天前
大模型开发相关岗位面试资料(含算法)
语言模型·自然语言处理·面试·职场和发展
我命由我123452 天前
Photoshop - Photoshop 快速共享作品
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop
我命由我123452 天前
自动化 / 智能装配的对象、内容、层级
运维·学习·职场和发展·自动化·求职招聘·职场发展·学习方法
AI职业加油站2 天前
大模型开发工程师证书怎么考?零基础学习路径与价值拆解
大数据·人工智能·学习·职场和发展·数据分析
blxr_2 天前
力扣hot100爬楼梯https://leetcode.cn/problems/climbing-stairs/
算法·leetcode·职场和发展
我命由我123452 天前
Photoshop - 将Photoshop图像置入其他应用程序
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop