蓝桥杯每日一题2023.11.21

题目描述

"蓝桥杯"练习系统 (lanqiao.cn)

题目分析

思路:

1.去重排序将其进行预处理

2.用gcd得到最简比值

3.用gcd_sub分别计算分子、分母的指数最大公约数

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
typedef long long ll;
ll n, cnt, a[N], x[N], y[N];
ll gcd(ll a, ll b)
{
	return b ? gcd(b , a % b) : a;
}
ll gcd_sub(ll a, ll b)
{
	if(a < b)swap(a, b);
	if(b == 1)return a;
	return gcd_sub(b, a / b);
}
int main()
{
	cin >> n;
	for(int i = 1; i <= n; i ++)
	{
		cin >> a[i];
	}
	sort(a + 1, a + 1 + n);
	for(int i = 2; i <= n; i ++)
	{
		if(a[i] != a[i - 1])
		{
			ll d = gcd(a[i], a[1]);
			cnt ++; 
			x[cnt] = a[i] / d;//分子 
			y[cnt] = a[1] / d;//分母 
		}
	}
	ll u = x[1], d = y[1];
	for(int i = 2; i <= cnt; i ++)
	{
		u = gcd_sub(u, x[i]);
		d = gcd_sub(d, y[i]);
	}
	cout << u << '/' << d;
	return 0;
}
相关推荐
爱吃生蚝的于勒9 小时前
【Linux】深入理解进程(一)
java·linux·运维·服务器·数据结构·c++·蓝桥杯
爱coding的橙子9 小时前
每日算法刷题Day76:10.19:leetcode 二叉树12道题,用时3h
算法·leetcode·职场和发展
夏鹏今天学习了吗11 小时前
【LeetCode热题100(47/100)】路径总和 III
算法·leetcode·职场和发展
007php00715 小时前
猿辅导Java面试真实经历与深度总结(二)
java·开发语言·python·计算机网络·面试·职场和发展·golang
缓风浪起17 小时前
【力扣】2011. 执行操作后的变量值
算法·leetcode·职场和发展
电子_咸鱼1 天前
LeetCode——Hot 100【电话号码的字母组合】
数据结构·算法·leetcode·链表·职场和发展·贪心算法·深度优先
微笑尅乐1 天前
中点为根——力扣108.讲有序数组转换为二叉搜索树
算法·leetcode·职场和发展
夏鹏今天学习了吗1 天前
【LeetCode热题100(46/100)】从前序与中序遍历序列构造二叉树
算法·leetcode·职场和发展
吃着火锅x唱着歌1 天前
LeetCode 2389.和有限的最长子序列
算法·leetcode·职场和发展