C ++;循环与随机阵列按键不灵 [英] C++; loop with randomizing array keys not working

查看:123
本文介绍了C ++;循环与随机阵列按键不灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持与我原本以为会这么容易解决的问题!

我有4个值(空,男,女,童)枚举。我有一个对应50个座位的数组。所以,我想放的男人,女人和孩子的基础上随机生成的座位号的空座位。如果座椅不为空,那么程序应该继续寻找其他的空座位。

这是我有:

 的#include< cstdlib>
 #包括LT&;&的ctime GT;
 #包括LT&;&iostream的GT;
 #包括LT&;串GT; 使用命名空间std;    枚举PAX {E,M,F,C};
    PAX席[50] = {E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E ,E,E,E,
                    E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E,E} ;    INT paxNumMaleTotal; //男乘客加载总数
    INT paxNumFemaleTotal;
    INT paxNumChildTotal;    随机无效(INT j)条;
    诠释主(){
    //随机排列座位男    INT N = 1;
    INT I = 0;
    paxNumMaleTotal = 35;
    paxNumFemaleTotal = 8;
    paxNumChildTotal = 5;        而(N< = paxNumMaleTotal){//循环,直到所有的人入席            而(席[I]!= M ||席[I]!= F ||席[I]!= C){
                   //坐一坐,除非座椅未占用
                 随机的(一); //生成随机座位采取
                 座椅由[i] = M; //座位采取的是一个男人
            }
            Ñ​​++; //座位取,采取下一个         }
     对于(INT K = 0; K< 50; k ++){//打印座位表
        COUT<<座椅[K]&下;&下; ENDL;
        }
 } 无效随机(诠释J)//这是给一个随机的座位号功能
 {
    函数srand(时间(NULL));
    J =兰特()%50;
 }

所以,我尝试了所有可能的选项。它要么把男人中的所有50个座位,或者,象在这种情况下,把一个人在没有循环的第一座。非常感谢您的时间!

感谢您的帮助。使用参考,我解决了随机选择一个座位的问题。使用循环的一个问题是仍然在这里。它选择只有一个座位(随机),而不是35个座位。 : - (


编辑:谢谢!该random_shuffle功能的伎俩!

 的for(int i = 0; I< paxNumMaleTotal;我++){
        座椅由[i] = M;
}的for(int i = paxNumMaleTotal; I< paxNumFemaleTotal + paxNumMaleTotal;我++){
        座椅由[i] = F;
}的for(int i = paxNumFemaleTotal + paxNumMaleTotal; I< paxNumFemaleTotal + paxNumMaleTotal +
                            paxNumChildTotal;我++){
        座椅由[i] = C;
}//随机化乘客座位
的std :: random_shuffle(安培;座椅[0],&安培;席位[49]);


解决方案

所以,其他人指出小错误在你的随机函数签名。

有可能是这样做虽然更简单的方法。首先,填写乘客的正确比例的数组。只是for循环一对夫妇,或者说如果你喜欢票友这样做是为了。

然后使用std :: random_shuffle。它给你一个非常可靠的洗牌。有很多方法来<一href=\"http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Comparison_with_other_shuffling_algorithms\"相对=nofollow>弄错了,所以这是很好的使用经过严格测试的算法..:)

I am stuck with a problem that I originally thought would be so easy to solve!

I have an enum with 4 values (empty, male, female, child). I have an array which corresponds to 50 seats. So, I would like to put men, women, and children in the empty seats based on a randomly generated seat number. If the seat is not empty, then the program should continue to search for other empty seats.

This is what I have:

 #include <cstdlib>
 #include <ctime>
 #include <iostream>
 #include <string>

 using namespace std;

    enum pax {E, M, F, C};
    pax seats[50] = {E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
                    E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E};

    int paxNumMaleTotal;    // Total number of Male passengers Loaded
    int paxNumFemaleTotal;
    int paxNumChildTotal;

    void random(int j);


    int main()  {


    // Randomize seating for Male

    int n=1;
    int i=0;
    paxNumMaleTotal=35;
    paxNumFemaleTotal=8;
    paxNumChildTotal=5;

        while (n<= paxNumMaleTotal)  {  // loop until all men take their seats

            while (seats[i] != M || seats[i] != F || seats[i] != C)   {  
                   // take a seat unless the seat is not occupied
                 random(i);                 // generate a random seat to be taken
                 seats[i] = M;              // Seat is taken by a man
            }
            n++;  // seat is taken, take the next one

         }


     for (int k=0; k<50; k++)  {     // printing the seating chart
        cout << seats[k] << endl;
        }
 }

 void random(int j)     // this is the function that gives a random seat number
 {
    srand( time( NULL ) );
    j = rand() % 50;
 }

So, I tried all possible options. It either puts men in all 50 seats or, like in this case, put a man in the first seat with no loop. Thanks a lot for your time!

Thanks for your help. Using the reference, I solved the issue of randomly selecting a seat. A problem with the loop is still here. It selects only one seat (randomly) and not 35 seats. :-(


EDIT: Thank you! The random_shuffle function did the trick!

for (int i=0; i < paxNumMaleTotal; i++)    {
        seats[i] = M;
}

for (int i = paxNumMaleTotal; i < paxNumFemaleTotal + paxNumMaleTotal; i++)    {
        seats[i] = F;
}

for (int i=paxNumFemaleTotal + paxNumMaleTotal; i < paxNumFemaleTotal + paxNumMaleTotal +
                            paxNumChildTotal; i++)  {
        seats[i] = C;
}

// Randomize seating for passengers
std::random_shuffle (&seats[0], &seats[49]);

解决方案

So, others have pointed out the small error in your random function signature.

There's probably an easier way to do this though. First, fill your array with the correct proportion of passengers. Just do this in order with a couple of for loops, or something fancier if you like.

Then use std::random_shuffle. It gives you a very reliable shuffle. There are lots of ways to get it wrong, so it's good to use the well-tested algorithm.. :)

这篇关于C ++;循环与随机阵列按键不灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆