当前位置:网站首页>2704:寻找平面上的极大点
2704:寻找平面上的极大点
2022-07-31 06:25:00 【mooczhimahu】
题目
总时间限制: 1000ms内存限制: 65536kB
描述
在一个平面上,如果有两个点(x,y),(a,b),如果说(x,y)支配了(a,b),这是指x>=a,y>=b;
用图形来看就是(a,b)坐落在以(x,y)为右上角的一个无限的区域内。
给定n个点的集合,一定存在若干个点,它们不会被集合中的任何一点所支配,这些点叫做极大值点。
编程找出所有的极大点,按照x坐标由小到大,输出极大点的坐标。
本题规定:n不超过100,并且不考虑点的坐标为负数的情况。
输入
输入包括两行,第一行是正整数n,表示是点数,第二行包含n个点的坐标,坐标值都是整数,坐标范围从0到100,输入数据中不存在坐标相同的点。
输出
按x轴坐标最小到大的顺序输出所有极大点。
输出格式为:(x1,y1),(x2,y2),...(xk,yk)
注意:输出的每个点之间有","分隔,最后一个点之后没有",",少输出和多输出都会被判错
样例输入
5 1 2 2 2 3 1 2 3 1 4
样例输出
(1,4),(2,3),(3,1)
首先打好程序框架,定义变量。
其中a用来储存输入,b储存结果,n为点数,i和j用来循环,t用来判断"极大点",m用来累加储存b
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
}a[105],b[105];
int main()
{
int m=0,t,i,j,n;
cin>>n;
for(i=1;i<=n;i++)
cin>>a[i].x>>a[i].y;
}接下来双for判断,判断某个点是否被支配,如果循环结束,t为1,说明"只有自己才能支配自己"。
这时,把"极大点"储存到b中。
for(i=1;i<=n;i++)
{
t=0;
for(j=1;j<=n;j++)
if(a[j].x>=a[i].x&&a[j].y>=a[i].y)
t++;
if(t==1)
{
b[++m].x=a[i].x;
b[m].y=a[i].y;
}
}最后给b排序,可参见:
sort(快速排列)的使用方法_mooczhimahu的博客-CSDN博客
排列后输出就可以了。
sort(b+1,b+1+m,cmp);
printf("(%d,%d)",b[1].x,b[1].y);
for(i=2;i<=m;i++)
printf(",(%d,%d)",b[i].x,b[i].y);易错点
1.输出时数据之间有逗号,需要格外注意。
2.快排(快速排列)时,排列长度是m,而不是n。
3.因为有多组数据,t要初始化。
完整程序
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
}a[105],b[105];
bool cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int m=0,t,i,j,n;
cin>>n;
for(i=1;i<=n;i++)
cin>>a[i].x>>a[i].y;
for(i=1;i<=n;i++)
{
t=0;
for(j=1;j<=n;j++)
if(a[j].x>=a[i].x&&a[j].y>=a[i].y)
t++;
if(t==1)
{
b[++m].x=a[i].x;
b[m].y=a[i].y;
}
}
sort(b+1,b+1+m,cmp);
printf("(%d,%d)",b[1].x,b[1].y);
for(i=2;i<=m;i++)
printf(",(%d,%d)",b[i].x,b[i].y);
}边栏推荐
- The Perfect Guide|How to use ODBC for Agentless Oracle Database Monitoring?
- nohup principle
- 第十六章:构建n(5,7)阶素数幻方
- leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
- 2022.07.14_Daily Question
- van-uploader上传图片,使用base64回显无法预览的问题
- 深度学习通信领域相关经典论文、数据集整理分享
- Run the NPM will pop up to ask "how are you going to open this file?"
- interrupt and pendSV
- 360 push-360 push tool-360 batch push tool
猜你喜欢

HighTec 的安装与配置

金融租赁业务

One of the small practical projects - food alliance ordering system

Analysis of the principle and implementation of waterfall flow layout

2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)

从入门到一位合格的爬虫师,这几点很重要

小实战项目之——吃货联盟订餐系统

2022.07.13_Daily Question

Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software

链表实现及任务调度
随机推荐
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
2022.07.15_Daily Question
把 VS Code 当游戏机
‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
Jobject 使用
Introduction and self-order of bcos
2022.07.15_Daily Question
Chapter 17: go back to find the entrance to the specified traverse, "ma bu" or horse stance just look greedy, no back to search traversal, "ma bu" or horse stance just look recursive search NXM board
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
tidyverse笔记——管道函数
2022.07.14_每日一题
Difficulty comparison between high concurrency and multithreading (easy to confuse)
shell之条件语句(test、if、case)
文件 - 05 下载文件:根据文件Id下载文件
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
【微服务】 微服务学习笔记二:Eureka注册中心的介绍及搭建
PCB抄板
Bulk free text translation
第十六章:构建n(5,7)阶素数幻方
iOS大厂面试查漏补缺
