1. 程式人生 > >SDUT-3324 順序表應用1:多餘元素刪除之移位演算法

SDUT-3324 順序表應用1:多餘元素刪除之移位演算法

Problem Description 一個長度不超過10000資料的順序表,可能存在著一些值相同的“多餘”資料元素(型別為整型),編寫一個程式將“多餘”的資料元素從順序表中刪除,使該表由一個“非純表”(值相同的元素在表中可能有多個)變成一個“純表”(值相同的元素在表中只保留第一個)。 要求: 1、必須先定義線性表的結構與操作函式,在主函式中藉助該定義與操作函式呼叫實現問題功能; 2、本題的目標是熟悉順序表的移位演算法,因此題目必須要用元素的移位實現刪除;                 Input 第一行輸入整數n,代表下面有n行輸入; 之後輸入n行,每行先輸入整數m,之後輸入m個數據,代表對應順序表的每個元素。                 Output 輸出有n行,為每個順序表刪除多餘元素後的結果 Sample Input 4 5 6 9 6 8 9 3 5 5 5 5 9 8 7 6 5 10 1 2 3 4 5 5 4 2 1 3 Sample Output 6 9 8 5 9 8 7 6 5 1 2 3 4 5  

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int * h;
    int len;
    int size;
}list;
int main()
{
   int t, n, i, j, k;
   scanf("%d", &t);
   while(t--)
   {
       scanf("%d", &n);
       list.h = (int *)malloc((n + 5) * sizeof(int));
       list.len = n + 5;
       list.size = (n + 5) * 4;
       for(i = 1; i <= n; i++)
       {
           scanf("%d", &list.h[i]);
       }
       for(i = 1; i <= n; i++)
       {
           for(j = i + 1; j <= n; j++)
           {
               if(list.h[i] == list.h[j])
               {
                   for(k = j; k < n; k++)
                   {
                       list.h[k] = list.h[k + 1];
                   }
                   n--;
                   j--;
               }
           }
       }
       for(i = 1; i <= n; i++)
       {
           if(i == n)
           {
               printf("%d\n", list.h[i]);
           }
           else
           {
               printf("%d ", list.h[i]);
           }
       }
   }
    return 0;
}