pat1052
来源:互联网 发布:java字节转为字符串 编辑:程序博客网 时间:2024/06/07 17:14
1052. Linked List Sorting (25)
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.
Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Sample Input:5 0000111111 100 -100001 0 2222233333 100000 1111112345 -1 3333322222 1000 12345Sample Output:
5 1234512345 -1 0000100001 0 1111111111 100 2222222222 1000 3333333333 100000 -1
/**first read in all the nums into the list then find the head elements , and select it from the list and insert it into the first place of another list2 , and select from the list1 the element which head = x .end*/#include <cstdio>#include <cstdlib>#include <vector>#include <algorithm>using namespace std ;#define Max 1001struct node{int head, end ,key ;bool operator < ( const node &b ) const{return key < b.key ;}} ;vector<node>list1 ;int Num , head ;vector<node> list2 ; int len = 0 ;void getInput(){node temp ;scanf("%d%d", &Num , &head ) ;for ( int i = 0 ; i < Num ; i++ ){scanf("%d%d%d", &temp.head, &temp.key , &temp.end ) ;list1.push_back( temp ) ;}}int findLoc( int head ){int loc ;for ( int i = 0 ; i < Num ; i++ ){if ( list1[i].head == head ){loc = i ;return loc ;}}}void deal_ (){int loc = findLoc( head ) ;list2.push_back( list1[loc]) ; while ( list1[loc].end != -1 ){loc = findLoc ( list1[loc].end ) ;list2.push_back( list1[loc] );}sort ( list2.begin() , list2.end() ) ;for ( int i = 0 ; i < list2.size() -1 ; i++ ){list2[i].end = list2[i+1].head ;}list2[list2.size() -1].end = -1 ; }void showList(){if ( list2.size() == 0){ printf("0") ; return ;}printf("%d %05d\n", list2.size() , list2[0].head) ;for ( vector<node>::iterator it = list2.begin() ; it != list2.end() ; it++ ){if ( it != list2.end() -1){printf( "%05d %d %05d\n", it->head , it->key , it->end ) ; }else{printf("%05d %d %d", it->head, it->key , it->end ) ;}}}int main ( void ){getInput() ;deal_() ;showList() ;return 0 ;}
在这个地方需要注意的是,如果数组需要的空间很大的话,就应该将vector 等容器使用数组进行替换,不然会出错的。
if ( list1.size() == 0 ){printf("%d %05d", Num , head ) ;return ;}
不对,这里我忘了将 list1从 vector 修改成为了 数组,所以这里的判断语句为
if ( Num == 0 ){....}
/**first read in all the nums into the list then find the head elements , and select it from the list and insert it into the first place of another list2 , and select from the list1 the element which head = x .end*/#include <cstdio>#include <cstdlib>#include <vector>#include <algorithm>using namespace std ;#define Max 1001struct node{int head, end ,key ;bool operator < ( const node &b ) const{return key < b.key ;}} ;struct node list1[100000] ;int Num , head ;struct node list2[100000] ; int len = 0 ;void getInput2(){node temp ;int nextLoc ;scanf("%d%d", &Num , &head ) ; for ( int i = 0; i < Num ; i++ ){ scanf("%d%d%d", &temp.head , &temp.key , &temp.end) ;list1[temp.head] = temp ;}nextLoc = head ;while ( nextLoc != -1 ){list2[len++] = list1[nextLoc] ;nextLoc = list1[nextLoc].end ;}}void deal2_(){sort( list2 , list2+len ) ; for ( int i = 0 ; i < len -1 ; i++ ){list2[i].end = list2[i+1].head ;}list2[len-1].end = -1 ;}void showList2( ){printf("%d %05d\n", len ,list2[0].head ) ;for ( int i = 0 ; i < len-1 ; i++ ){printf("%05d %d %05d\n", list2[i].head , list2[i].key , list2[i].end ) ;}printf("%05d %d -1", list2[len-1].head , list2[len-1].key ) ;}int main ( void ){getInput2() ;deal2_() ;showList2() ; return 0 ;}
/**first read in all the nums into the list then find the head elements , and select it from the list and insert it into the first place of another list2 , and select from the list1 the element which head = x .end*/#include <cstdio>#include <cstdlib>#include <vector>#include <algorithm>using namespace std ;#define Max 1001struct node{int head, end ,key ;bool operator < ( const node &b ) const{return key < b.key ;}} ;struct node list1[100000] ;int Num , head ;struct node list2[100000] ;int len = 0 ;void getInput2(){node temp ;int nextLoc ;scanf("%d%d", &Num , &head ) ; if ( Num == 0 ) return ; for ( int i = 0; i < Num ; i++ ){ scanf("%d%d%d", &temp.head , &temp.key , &temp.end) ;list1[temp.head] = temp ;}nextLoc = head ;while ( nextLoc != -1 ){list2[len++] = list1[nextLoc] ;nextLoc = list1[nextLoc].end ;}}void deal2_(){sort( list2 , list2+len ) ; for ( int i = 0 ; i < len -1 ; i++ ){list2[i].end = list2[i+1].head ;}list2[len-1].end = -1 ;}void showList2( ){ if ( len == 0 ) { printf("%d -1", len ) ; return ; } printf("%d %05d\n", len ,list2[0].head ) ;for ( int i = 0 ; i < len-1 ; i++ ){printf("%05d %d %05d\n", list2[i].head , list2[i].key , list2[i].end ) ;}printf("%05d %d -1", list2[len-1].head , list2[len-1].key ) ;}int main ( void ){ getInput2() ;deal2_() ; showList2() ; return 0 ;}
- pat1052
- pat1052 Linked List Sorting
- PAT1052 卖个萌(20)
- PAT1052 Linked List Sorting (25)
- 我的第一个博客
- boj 452 解码锦标赛【动态规划】(排位赛08_D)
- hdu 2032 杨辉三角(最后一组后不加空行的处理) 20140731.c
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- 阿里云服务器安装配置mysql数据库
- pat1052
- POJ2441 Arrange the Bulls 状态压缩DP
- 再说Java EE
- 扩展欧几里德算法
- C++多态性分析之虚函数表
- HDU1799 循环多少次?【找规律】
- RadASM如何编辑资源并使用资源
- 高仿京东商品放大镜js
- Android下载进度条,一个通俗易懂案例