poj-1007

原本使用二維數組實現,後來看到別人ac的代碼中有使用結構體的實現,甚爲合適

順便複習了快排...

#include <stdio.h>
#include <string.h>
// char dna[50][100];
// int value[50];
struct Dna
{
  char str[100];
  int value;
};

void qsort(int left, int right, struct Dna *dna)
{
  struct Dna tmp;
  int l, h, x;
  l = left;
  h = right;
  x = dna[(left + right) >> 1].value;
  while(l <= h)
  {
    while(dna[l].value < x)
      l++;
    while(dna[h].value > x)
      h--;
    if (l <= h)
    {
      tmp = dna[l];
      dna[l] = dna[h];
      dna[h] = tmp;
      l++;
      h--;
    }
  }
  if (left < h)
    qsort(left, h, dna);
  if (l < right)
    qsort(l, right, dna);
}

int main(int argc, char const *argv[])
{
  int n, m;
  int i, j, k;
  scanf("%d %d", &n, &m);
  // memset(value, 0 ,sizeof(value));
  struct Dna dna[m];
  for (i = 0; i < m; i++)
  {
    j = 0;
    scanf("%s", dna[i].str);
    dna[i].value = 0;
    for (j = 0; j < n; j++)
    {
      for (k = j + 1; k < n; k++)
      {
        if(dna[i].str[j] > dna[i].str[k])
          dna[i].value += 1;
      }
    }
  }

  qsort(0, m-1, dna);

  for (i = 0; i < m; i++)
  {
    printf("%s\n", dna[i].str);
  }

  return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章