UVa1587

1587 Box
Ivan works at a factory that produces heavy machinery. He has a simple job | he knocks up wooden
boxes of different sizes to pack machinery for delivery to the customers. Each box is a rectangular
parallelepiped. Ivan uses six rectangular wooden pallets to make a box. Each pallet is used for one side
of the box.
Joe delivers pallets for Ivan. Joe is not very smart and often makes mistakes | he brings Ivan
pallets that do not t together to make a box. But Joe does not trust Ivan. It always takes a lot of
time to explain Joe that he has made a mistake.
Fortunately, Joe adores everything related to computers and sincerely believes that computers never
make mistakes. Ivan has decided to use this for his own advantage. Ivan asks you to write a program
that given sizes of six rectangular pallets tells whether it is possible to make a box out of them.
Input
Input le contains several test cases. Each of them consists of six lines. Each line describes one pallet
and contains two integer numbers w and h (1 w; h 10 000) | width and height of the pallet in
millimeters respectively.
Output
For each test case, print one output line. Write a single word `POSSIBLE‘ to the output le if it is
possible to make a box using six given pallets for its sides. Write a single word `IMPOSSIBLE‘ if it is not
possible to do so.
Sample Input
1345 2584
2584 683
2584 1345
683 1345
683 1345
2584 683
1234 4567
1234 4567
4567 4321
4322 4567
4321 1234
4321 1234
Universidad de Valladolid OJ: 1587 { Box 2/2
Sample Output
POSSIBLE
IMPOSSIBLE

题意:

给出六个长方形的长和宽,判断它们是否能够构成一个长方体。

输入:

多组数据,每组6行,每行两个整数表示长和宽。

输出:

每组一行,输出判断结果。

分析:

使用pair来表示长方形,first量表示长,second量表示宽。输入数据后,对6个长方形进行排序first量小的排在前面的情况下second量小的排在前面。如果能够组成长方体,那么这6个长方形能够分成三组,每组两个相同的长方形。此外,三组长方形的边长应该分别满足:a,b;b,c;c,a。为了方便,我们在输入的时候就让每一个矩形pair的first量总小于second量。不妨设a<b<c,如果输入的6个矩形可以组成一个长方体,那么排序后位于前面的两个矩形的first和second量分别为a和b,中间的两个矩形为a和c,最末的两个矩形为b和c,反之也成立。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef pair<int,int> P;
 8 bool cmp(const P& p1,const P& p2){
 9     if(p1.first < p2.first) return true;
10     if(p1.first == p2.first) return p1.second < p2.second;
11     return false;
12 }
13 P p[10];
14 int main(){
15     int a,b;
16     while(scanf("%d%d",&a,&b) != EOF){
17         if(a > b){
18             int tmp = a;
19             a = b; b = tmp;
20         }
21         p[0].first = a; p[0].second = b;
22         for(int i = 1; i < 6 ; i++){
23             scanf("%d%d",&p[i].first,&p[i].second);
24             if(p[i].first > p[i].second){
25                 int tmp = p[i].first;
26                 p[i].first = p[i].second;
27                 p[i].second = tmp;
28             }
29         }
30         sort(p,p + 6,cmp);
31         //for(int i = 0 ;i < 6 ; i++)
32         //    cout << p[i].first <<  " " << p[i].second << endl;
33         if(p[0].first == p[1].first && p[0].second == p[1].second
34            && p[2].first == p[3].first && p[2].second == p[3].second
35            && p[4].first == p[5].first && p[4].second == p[5].second){
36             int x = p[0].first,y = p[2].second,z = p[0].second;
37             int x1 = p[2].first,y1 = p[4].second,z1 = p[4].first;
38             //int y2 = p[4].second,z2 = p[4].first;
39             //printf("%d %d %d\n",x,y,z);
40             //printf("%d %d %d\n",x1,y1,z1);
41             if(x == x1 && y == y1 && z == z1) {
42                 printf("POSSIBLE\n");
43                 continue;
44             }
45             else{
46                 printf("IMPOSSIBLE\n");
47                 continue;
48             }
49         }
50         else printf("IMPOSSIBLE\n");
51     }
52     return 0;
53 }

时间: 2024-11-05 16:08:21

UVa1587的相关文章

UVa1587.Digit Counting

题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=247&page=show_problem&problem=3666 13764622 1225 Digit Counting Accepted C++11 0.035 2014-06-18 07:44:02 1225 - Digit Counting Time limit: 3.000 seconds Tru

UVA1587 UVALive3214 POJ2160 Box

Regionals 2004 >> Europe - Northeastern 问题链接:UVA1587 UVALive3214 POJ2160 Box. 问题简述:给出六组整数,问能否构成六面体. 这个题用C语言做的毫无技术含量,也许用C++来写会好一些. AC的C语言程序如下: /* UVA1587 UVALive3214 POJ2160 Box */ #include <stdio.h> #define MAXN 6 struct { int w, h; int count;

UVA1587 BOX盒子

给定矩形的长和宽,判断它们能否构成长方体的六个面 1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int t; 5 void change(int a[7][3]) //输入两个数字,按顺序:小,大:排列. 6 { 7 for (int k = 1; k <= 6; k++) 8 if (a[k][1] > a[k][2]) 9 { 10 t = a[k][1]; 11 a[k][1] =

BOX (UVA-1587)

对比一下代码的书写差距: 我的代码: 1 #include<iostream> 2 3 using namespace std; 4 5 int a[6]; 6 int b[6]; 7 int v[3]; //访问标记 8 9 bool judge(int i, int j) 10 { 11 if(a[i]==a[j]&&b[i]==b[j]) 12 return true; 13 return false; 14 } 15 16 bool judge1() 17 { 18 i

UVa1587 Box

#include <iostream>#include <algorithm>#include <set>using namespace std; int main(){    set<int> s; // 12个数字中不能出现3个以上不同的值,最多只有三种值:长.宽.高    int face[6];    int i = 0, w, h;    while (cin >> w >> h)    {        if (s.siz

[算法竞赛入门经典]Kickdown ACM/ICPC NEERC 2004,UVa1587

Description A research laboratory of a world-leading automobile company has received an order to create a special transmission mechanism, which allows for incredibly efficient kickdown - an operation of switching to lower gear. After several months o

算法竞赛入门经典第二版第三章习题

写这个的原因是看到一位大神的习题答案总结,于是自己心血来潮也想写一个这个,目的主要是督促自己刷题吧,毕竟自己太弱了. 习题3-1 得分 UVa 1585 大致就是设置一个变量记录到当前为止的连续的O的数量,碰到X就变0,水题. #include<stdio.h> #include<ctype.h> #include<string.h> char s[90]; int main(void) { int length,n,sum,num; scanf("%d&qu

2019年2月做题记录

UVA10082 (字符串常量水题) UVA272 (字符串替换水题) UVA401 (回文串镜像串水题) UVA340 (模拟题) UVA1583 (打表水题) UVA1584 (暴力) UVA1585 (模拟) UVA1586 (数学) UVA1225 (打表水题) UVA455 (KMP算法) UVA232 (模拟+思维) UVA202 (除法高精度水题) UVA1587 (思维) UVA10340 (模拟,定序求交集) 原文地址:https://www.cnblogs.com/Aya-U