欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > Day30 洛谷真题讲解(数学)(模拟)(质数)

Day30 洛谷真题讲解(数学)(模拟)(质数)

2025/5/15 21:43:28 来源:https://blog.csdn.net/lxy2966201752/article/details/146403613  浏览:    关键词:Day30 洛谷真题讲解(数学)(模拟)(质数)

#include <iostream>#include <algorithm>
using namespace std;// 定义结构体类型
struct Participant {int num;int score;
};// 使用结构体类型定义数组
Participant u[5050];// 比较函数
bool compare(const Participant& a, const Participant& b) {if (a.score == b.score) {return a.num < b.num; // 如果 score 相同,按 num 升序排序}return a.score > b.score; // 按 score 降序排序
}int main() {int n, k;cin >> n >> k;int kk = k * 1.5;if (kk > n) {kk = n; // 如果 kk 超过 n,则取 n}// 输入数据for (int i = 0; i < n; i++) {cin >> u[i].num >> u[i].score;}// 使用自定义比较函数对数组排序int sum = 0;sort(u, u + n, compare);for (int i = 0; i < n; i++) {if (u[i].score >= u[kk - 1].score){sum++;}}// 输出前 kk 个元素cout << u[kk - 1].score << sum << endl;for (int i = 0; i < sum; i++) {cout << u[i].num << " " << u[i].score << endl;}return 0;
}

//#include<iostream>
//#include<cmath>
//using namespace std;
//bool pan(int x1, int x2)
//{
//	if (x1 != 1&&x1!=2)
//	{
//		for (int i = 2; i < x1; i++)
//		{
//			if (x1 % i == 0)
//			{
//				return false;
//			}
//
//		}
//	}
//	if (x2 != 1&&x2!=2)
//	{
//		for (int i = 2; i < x2; i++)
//		{
//			if (x2 % i == 0)
//			{
//				return false;
//			}
//		}
//	}
//	return true;
//}
//
//int main()
//{
//	int k = 1;
//	int n;
//	cin >> n;
//	for (int i = 1; i <= n; i++)
//	{
//		for (int j = i;j<=n; j++)
//		{
//			if (i == j)
//			{
//				continue;
//			}
//			if (i * j == n)
//			{
//				if (pan(i, j))
//				{
//					int p = max(i, j);
//					if (p > k)
//					{
//						k = p;
//					}
//				}
//			}
//		
//		}
//	}
//	cout << k;
//}
#include <iostream>
#include <cmath>
using namespace std;// 判断一个数是否为质数
bool isPrime(int x) {if (x <= 1) return false; // 1不是质数if (x == 2) return true;  // 2是质数if (x % 2 == 0) return false; // 排除偶数for (int i = 3; i <= sqrt(x); i += 2) { // 只遍历奇数if (x % i == 0) return false;}return true;
}int main() {int n;cin >> n;int maxPrime = -1;// 遍历到 sqrt(n)for (int i = 2; i <= sqrt(n); i++) {if (n % i == 0) { // 如果 i 是 n 的因子int j = n / i; // 计算对应的 jif (isPrime(i) && isPrime(j)) { // 检查 i 和 j 是否都是质数maxPrime = max(maxPrime, max(i, j)); // 更新最大质数}}}cout << maxPrime << endl;return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词