Chinaunix首页 | 论坛 | 博客
  • 博客访问: 579701
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-03-03 10:27:24

题目链接: 

答案错误,存一下,留作比较

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct node
  5. {
  6.     char Address[6] ;
  7.     int Data ;
  8.     char Next[6] ;
  9. }Node ;

  10. void reverse ( int from , int to , int *arr )
  11. {
  12.     int temp ;
  13.      for ( int i = 0 ; i <= (to - from )/2 ; i++ )
  14.      {
  15.          temp = arr[i+from] ;
  16.          arr[i+from] = arr [to-i] ;
  17.         arr[to-i] = temp ;
  18.      }
  19. }

  20. void Reverse ( int K , int N , int *arr )
  21. {
  22.     
  23.     for ( int i = 0 ; i < (int)(N/K) ;i++ )
  24.     {
  25.         reverse( i*K , (i+1)*K - 1, arr ) ;
  26.     }
  27. }
  28.  

  29. int main ( void )
  30. {
  31.     Node *nodeList = NULL ;
  32.         
  33.     int pList [100001] ,N , K ; // is the length of list , K is the reverse unit
  34.     char first[6] , *next;
  35.     
  36.     scanf ("%s%d%d" , first , &N , &K ) ;
  37.  
  38.     memset (pList , 0 , sizeof(pList)) ;

  39.     nodeList = (Node*)malloc (N*sizeof(Node) ) ;
  40.  
  41.     for ( int i = 0 ; i < N ; i++ )
  42.     {
  43.         scanf ("%s%d%s" , nodeList[i].Address , &nodeList[i].Data ,nodeList[i].Next ) ;

  44.         if ( strcmp(first , nodeList[i].Address) == 0 ) // find first
  45.             pList[0] = i ;
  46.     }

  47.     next = nodeList[ pList[0] ].Next ;
  48.     int counter = 1 ;
  49.     while ( counter != N )
  50.     {
  51.      for ( int j = 0 ; j < N ; j++ )
  52.         {
  53.             if ( strcmp( next , nodeList[j].Address )== 0 )
  54.             {
  55.                 pList[counter++] = j ;
  56.                 next = nodeList[j].Next ;                
  57.             }
  58.         }
  59.     }

  60.     
  61.     Reverse ( K, N, pList ) ;

  62.     for ( int i = 0 ; i < N ; i++ )
  63.         printf ("%d " , nodeList [pList[i] ].Data ) ;

  64.      for ( int i = 0 ; i < N-1 ; i++ )
  65.      {
  66.         strcpy(    nodeList[pList[i]].Next ,nodeList[pList[i+1]].Address) ;
  67.      }

  68.      for ( int i = 0 ; i < N ; i++ )
  69.     {
  70.      printf ("%s %d %s" , nodeList[pList[i]].Address , nodeList[pList[i]].Data , nodeList[pList[i] ].Next) ;
  71.     
  72.          if ( i != N -1 )
  73.            printf ("\n") ;
  74.         }
  75.     
  76.     return 0 ;
  77. }


第二次提交代码: 一个测试点没有过, 运行超时,我觉得应该是翻转数组的方法不妥当。


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct node
  5. {
  6.     char Address[6] ;
  7.     int Data ;
  8.     char Next[6] ;
  9. }Node ;

  10. void reverse ( int from , int to , int *arr )
  11. {
  12.     int temp ;
  13.      for ( int i = 0 ; i <= (to - from )/2 ; i++ )
  14.      {
  15.          temp = arr[i+from] ;
  16.          arr[i+from] = arr [to-i] ;
  17.         arr[to-i] = temp ;
  18.      }
  19. }

  20. void Reverse ( int K , int N , int *arr )
  21. {
  22.        if ( K == 0 || K == 1 )
  23.      return ;
  24.  
  25.     for ( int i = 0 ; i < (int)(N/K) ;i++ )
  26.     {
  27.         reverse( i*K , (i+1)*K - 1, arr ) ;
  28.     }
  29. }
  30.  

  31. int main ( void )
  32. {
  33.     Node *nodeList = NULL ;
  34.         
  35.     int pList [100001] ,N , K ; // is the length of list , K is the reverse unit
  36.     char first[6] , *next;
  37.     
  38.     scanf ("%s%d%d" , first , &N , &K ) ;
  39.  
  40.     memset (pList , 0 , sizeof(pList)) ;

  41.     nodeList = (Node*)malloc (N*sizeof(Node) ) ;
  42.  
  43.     for ( int i = 0 ; i < N ; i++ )
  44.     {
  45.         scanf ("%s%d%s" , nodeList[i].Address , &nodeList[i].Data ,nodeList[i].Next ) ;

  46.         if ( strcmp(first , nodeList[i].Address) == 0 ) // find first
  47.             pList[0] = i ;
  48.     }

  49.     next = nodeList[ pList[0] ].Next ;
  50.     int counter = 1 ;
  51.     while ( counter != N )
  52.     {
  53.      for ( int j = 0 ; j < N ; j++ )
  54.         {
  55.             if ( strcmp( next , nodeList[j].Address )== 0 )
  56.             {
  57.                 pList[counter++] = j ;
  58.                 next = nodeList[j].Next ;                
  59.             }
  60.         }
  61.     }

  62.     
  63.     Reverse ( K, N, pList ) ;

  64.     

  65.      for ( int i = 0 ; i < N-1 ; i++ )
  66.     {
  67.      printf ("%s %d %s\n" , nodeList[pList[i]].Address , nodeList[pList[i]].Data , nodeList[pList[i+1] ].Address) ;
  68.         }
  69.       printf ("%s %d -1" , nodeList[pList[N-1]].Address , nodeList[pList[N-1]].Data ) ;
  70.     
  71.     return 0 ;
  72. }

阅读(903) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~