您的位置:首页 > 其它

[2010山东省第一届ACM大学生程序设计竞赛]——Hello World!

2014-04-13 15:20 393 查看
Hello World!

题目描述

We know that Ivan gives Saya three problems to solve (Problem F), and this is the first problem.

“We need a programmer to help us for some projects. If you show us that you or one of your friends is able to program, you can pass the first hurdle.

I will give you a problem to solve. Since this is the first hurdle, it is very simple.”

We all know that the simplest program is the “Hello World!” program. This is a problem just as simple as the “Hello World!”

In a large matrix, there are some elements has been marked. For every marked element, return a marked element whose row and column are larger than the showed element’s row and column respectively. If there are multiple solutions, return the element whose row
is the smallest; and if there are still multiple solutions, return the element whose column is the smallest. If there is no solution, return -1 -1.

Saya is not a programmer, so she comes to you for help
Can you solve this problem for her?

输入

The input consists of several test cases.

The first line of input in each test case contains one integer N (0<N≤1000), which represents the number of marked element.

Each of the next N lines containing two integers r and c, represent the element’s row and column. You can assume that 0<r, c≤300. A marked element can be repeatedly showed.
The last case is followed by a line containing one zero.

输出
For each case, print the case number (1, 2 …), and for each element’s row and column, output the result. Your output format should imitate the sample output. Print a blank line after each test case.

示例输入

3

1 2

2 3

2 3

0

示例输出

Case 1:

2 3

-1 -1
-1 -1

题意:有一个矩阵,输入坐标代表将矩阵某些位置标记,标记结束后,再重新查找一遍,输出比已经标记的比当前输入的横、纵坐标都大的点。

注意:

先标记,所有标记完后再查找。

找横、纵坐标都大的点。(任何一个相等都不行)

当然,排序不可少,但是如果用一个数组存,排序后,原先访问顺序就没有了,所以最好两个数组存状态。

#include <iostream>
#include<string.h>
#include<algorithm>
using namespace std;

struct point
{
int x;
int y;
};
point p[1005];
point p2[1005];
int N;
bool cmp(point a,point b)
{
if(a.x==b.x)    return a.y<b.y;
else    return a.x<b.x;
}

void find(point p)
{
int i;
if(p.x>p2[N-1].x && p.y>p2[N-1].y)
{
cout<<"-1 -1"<<endl;
return;
}

for(i=0;i<N;++i)
{
if(p2[i].x>p.x && p2[i].y>p.y)
{
cout<<p2[i].x<<" "<<p2[i].y<<endl;
return;
}
}
cout<<"-1 -1"<<endl;
}

int main()
{
int count_case;
count_case = 1;
int i,j;
while(cin>>N&&N)
{
for(int i = 0;i<N;i++)
cin>>p[i].x>>p[i].y;
memcpy(p2,p,sizeof(p));

sort(p2,p2+N,cmp);

cout<<"Case "<<count_case++<<":"<<endl;
for(i = 0;i<N;i++)
find(p[i]);
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐