NYOJ 8-一种排序
2013-07-18 19:31
399 查看
点击打开链接
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);
1.按照编号从小到大排序
2.对于编号相等的长方形,按照长方形的长排序;
3.如果编号和长都相同,按照长方形的宽排序;
4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;
输入第一行有一个整数 0<n<10000,表示接下来有n组测试数据;
每一组第一行有一个整数 0<m<1000,表示有m个长方形;
接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,
第二个和第三个数值大的表示长,数值小的表示宽,相等
说明这是一个正方形(数据约定长宽与编号都小于10000);
输出顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
样例输入
样例输出
没什么技术含量,多级排序问题,用二叉树进行插入排序然后输出结果就行,今天整理的时候感觉用set和pair也能做,当时不会,现在写更简单了100行变40行
刚写的代码,还wa了一次,忘了清空set了,以后要注意,太粗心:
一种排序
时间限制:3000 ms | 内存限制:65535 KB难度:3
描述现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);
1.按照编号从小到大排序
2.对于编号相等的长方形,按照长方形的长排序;
3.如果编号和长都相同,按照长方形的宽排序;
4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;
输入第一行有一个整数 0<n<10000,表示接下来有n组测试数据;
每一组第一行有一个整数 0<m<1000,表示有m个长方形;
接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,
第二个和第三个数值大的表示长,数值小的表示宽,相等
说明这是一个正方形(数据约定长宽与编号都小于10000);
输出顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
样例输入
1 8 1 1 1 1 1 1 1 1 2 1 2 1 1 2 2 2 1 1 2 1 2 2 2 1
样例输出
1 1 1 1 2 1 1 2 2 2 1 1 2 2 1
没什么技术含量,多级排序问题,用二叉树进行插入排序然后输出结果就行,今天整理的时候感觉用set和pair也能做,当时不会,现在写更简单了100行变40行
#include<stdio.h> #include<stdlib.h> typedef struct NODE { int num , length , wide; struct NODE * left , *right; }Node; Node * insert(Node * root , Node * node) { int flag; if(root == NULL) return node; else { for(Node * curr = root ; curr != NULL ;) { if(curr->num > node->num ) flag = 1; else if(curr->num < node->num ) flag = -1; else { if(curr->length > node->length ) flag = 1; else if(curr->length < node->length ) flag = -1; else { if(curr->wide > node->wide ) flag = 1; else if(curr->wide < node->wide ) flag = -1; else flag = 0; } } if( flag > 0) { if(curr->left == NULL) { curr->left = node; return root; } else curr = curr->left ; } else if(flag == 0) { free(node); return root; } else { if(curr->right == NULL ) { curr->right = node; return root; } else curr = curr->right ; } } return root; } } void print_node(Node * node) { if(node == NULL) return ; print_node(node->left ); printf("%d %d %d\n" , node->num , node->length , node->wide ); print_node(node->right ); free(node); } int main() { int i , j; int num , length , wide; Node * node , * root; scanf("%d" , & i); while(i--) { scanf("%d" , & j); root = NULL; while(j--) { scanf("%d %d %d" , &num , &length , &wide); node = (Node *)calloc(1 , sizeof(Node)); node->num = num; if(length > wide) { node->length = length; node->wide = wide; } else { node->length = wide; node->wide = length; } root = insert(root , node); } print_node(root); } return 0; }
刚写的代码,还wa了一次,忘了清空set了,以后要注意,太粗心:
#include<stdio.h> #include<utility> #include<set> using namespace std; int main() { int n; scanf("%d", &n); set<pair<int, pair<int, int> > > s; pair<int, pair<int, int> > p; int m; while(n--) { scanf("%d", &m); while(m--) { int a, b, c; scanf("%d%d%d", &a, &b, &c); if(c > b) { int temp = b; b = c; c = temp; } p.first = a; p.second.first = b; p.second.second = c; s.insert(p); } set<pair<int, pair<int, int> > > ::iterator i; for(i = s.begin(); i != s.end(); i++) { printf("%d %d %d\n", (*i).first, (*i).second.first, (*i).second.second); } s.clear(); } return 0; }