【问题标题】:Inputing string and printing them in random order输入字符串并以随机顺序打印
【发布时间】:2017-04-02 13:40:00
【问题描述】:

让我的话以随机顺序打印出来有点麻烦。我插入的 8 个单词需要以随机顺序打印出来。现在我只能以随机顺序生成其中的一些,因为如果两次生成相同的数字,它会覆盖以前的空间。我怎样才能消除这个问题?

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #include <ctype.h>
    #define MAX 50

    int readLine(char string[]);

    int main(void)
    {
        int i;
        char str[8][MAX], temp[8][MAX];
        srand((unsigned)time(NULL));
        int r_num;

        printf("Enter 8 words:\n");

        for(i=0; i<8; ++i)
        {
            readLine(str[i]);
        }


        for (int i = 0; i < 8; i++)
        {
            r_num = rand()%8+1;
            strcpy(temp[r_num], str[i]);
        }

    printf("\nRandom order of words: \n");
    for(i=0; i<8; ++i)
    {
        printf("%s\n", temp[i]);
    }
    printf("\n");

    return 0;
}

int readLine(char string[])
{
    int ch;
    int i=0;
    while (isspace(ch = getchar()))
        ;
    while (ch != '\n' && ch != EOF)
    {
        if (i < MAX)
        {
            string[i++] = ch;
            ch = getchar();
        }
    }
    string[i] = '\0';
    return i;
}

【问题讨论】:

    标签: c arrays string random char


    【解决方案1】:

    洗牌数据的一种方法是从尚未选择的数据中挑选。

    int main(void)
    {
        int i;
        char str[8][MAX];
        int order[8]; /* order list */
        srand((unsigned)time(NULL));
        int r_num;
    
        printf("Enter 8 words:\n");
    
        for(i=0; i<8; ++i)
        {
            readLine(str[i]);
        }
    
        /* initialize order list */
        for (i = 0; i < 8; i++)
        {
            order[i] = i;
        }
        for (i = 7; i > 0; i--) /* select from back to front */
        {
            int pos = rand() % (i + 1); /* pick one position randomly */
            /* pick selected element by swapping */
            int temp = order[pos];
            order[pos] = order[i];
            order[i] = temp;
        }
    
        printf("\nRandom order of words: \n");
        for(i=0; i<8; ++i)
        {
            printf("%s\n", str[order[i]]); /* print strings in the shuffled order */
        }
        printf("\n");
    
        return 0;
    }
    

    【讨论】:

    • 谢谢!这对我来说看起来很简单的解决方案。我没有得到的是,当你生成一个随机数时,它会选择一个从 i+1 的范围,即第一次从 0 到 8。第二次从 0 到 7 .. 至少这是我假设它在做什么。那么为什么它不会两次选择相同的数字呢?即第一次是 5,然后第二次是 5?
    • @Alex 第一次选择 5 时,通过交换将 5 移到数组的后面。所以候选人是第二次 [0, 1, 2, 3, 4, 7, 6],而 index 5 可以再次选择,data 5 获胜不会再次被选中。 i 以 7 开头(元素数 - 1),所以第一次迭代的范围是 0 到 7(非负数模 (7+1)),而不是 0 到 8。第二次是 0 到 6。从一个候选中选择是显而易见的,因此可以使用条件i &gt; 0 代替i &gt;= 0
    【解决方案2】:

    在将输入行 string[i] 存储到随机输出行 temp[r_num] 之前,您只需验证所选输出是否空闲。

    第一步,从第一步初始化输出数组temp[8][MAX] 循环:

    for(i=0; i<8; ++i)
    {
        strcpy(temp[i],""); // set each output line to free (empty).
        readLine(str[i]);
    }
    

    第二步,在选择字符串的同时在随机选择器上循环 不输出: 随机索引应为 0 到 7。

    for (i = 0; i < 8; i++)
    {
        do {
            r_num = rand()%8; // index is between 0 and 7
        } while (strlen(temp[r_num])!=0); // output shall be free/empty
        strcpy(temp[r_num], str[i]);
    }
    

    代替:

        for (int i = 0; i < 8; i++)
        {
            r_num = rand()%8+1; // error index start from 0
            strcpy(temp[r_num], str[i]);
        }
    

    【讨论】:

      【解决方案3】:

      您需要注意不要将相同的数据复制到临时数组中。

      你可以检查以下简单的逻辑,

      1) 找到 randno r_num

      2) 交换。 str[r_num] 和 str[maxlen]

      3) 将 max len 减 1

      4) 重复

      for ( i = 0; i < 8; i++)
      {
          // Commenting your code 
          //r_num = rand()%8+1;
          //strcpy(temp[r_num], str[i]);
      
          memset(temp1,0,sizeof(temp1));
          r_num = (rand()%(8-i))+1;
          if(r_num != i) // Do not swap if r_num is max
          {
              //swapping
              strcpy(temp1,str[r_num]);
              strcpy(str[r_num],str[7-i]);
              strcpy(str[7-i],temp1);
          }
      }
      

      希望对你有帮助:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多