⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rd_list.c

📁 Sherwood算法消除最坏实例
💻 C
字号:
#include<stdio.h>
#include<stdlib.h>

/****** create a random sequence of n integers not equal to each other******/
main(int argc, char *argv[])
{
   int n, i, j, flag, rnd, *rd;
   FILE *outfp;

   if(argc == 3)
   {
      if( (outfp = fopen(argv[1], "w")) == NULL)
      {
         printf("Can't open the file!\n");
         exit(2);
      }
   }
   else
   {
      printf("Usage: %s <outfilename> <n>        <outfilename> is to store the <n> integer created randomly\n", argv[0]);
      exit(1);
   }

   n = atoi(argv[2]);
   rd = (int *)malloc(n * sizeof(int));

   srand( (unsigned)time( NULL ) );
   i = 0; flag = 0;
   while(i < n){
      rnd = rand() % (10*n) + 1;
      printf("%d ", rnd);
      for(j = 0; j < i; j++)
         if(rnd == rd[j]){ 
            i--; flag = 1;
            break;
         }
      if(flag != 1) rd[i] = rnd;
      flag = 0;
      i++;
   }
   for(i = 0; i < n; i++)
      fprintf(outfp, "%d ", rd[i]);
   fclose(outfp);
   free(rd);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -