-1

我尝试将随机函数用于不同的事情。现在它可以工作,但我遇到一个问题是我想在我的代码中随机生成 F 但它们之后需要变成数组。我也需要使用它,如果我需要一个一个地做,我认为我的代码会太长而且太乱。你知道我该怎么做吗?

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
    srand(time(0));
    //random numbers 1 to 10 for Time:
    int t = rand() % 10 + 1 ;
    cout << "There is "<< t << " Time;
    int* F1 = new int [t];
    int* F2 = new int [t];
    int* F3 = new int [t];
    int* F4 = new int [t];
    int* F5 = new int [t];
    cout << "Time per F: 0 Not available, 1 available;
    //For F1
    for(int i = 0; i < t; i++){
        //random numbers 0 or 1:
        F1[i] = rand() % 2 ;
    }
    cout << "The Time for F1 is ";
    for(int a = 0; a < t; a++){
        cout << " "<< F1[a] <<" ";
    } 
    //For F2
    for(int j = 0; j < t; j++){
        //random numbers 0 or 1:
        F2[j] = rand() % 2 ;
    }
    cout << "The Time slot for F2 is ";
    for(int b = 0; b < t; b++){
        cout << " "<< F2[b] << " ";
    }
    return 0;
}

谢谢

编辑:你给我的解决方案有助于找到我做的解决方案 int F[em][t];

4

3 回答 3

0

我尝试将随机函数用于不同的事情。

随机的东西,大概。

现在它可以工作,但我遇到一个问题是我想在我的代码中随机生成 F 但它们之后需要变成数组。

您可以像生成值一样生成它t

const int number_of_fs = rand() % 10 + 1;
std::cout << "Working with " << number_of_fs << " Fs" << std::endl;

我也需要使用它,如果我需要一个一个地做,我认为我的代码会太长而且太乱。你知道我该怎么做吗?

你的代码已经太长太乱并且有内存泄漏。您应该使用标准容器库中的容器,而不是指向永远不会得到deleted 的指针的指针。

例如,如果您使用 a std::mapof std::vectors,您的代码可能如下所示:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <vector>

int main(){
    srand(time(0));
    //random numbers 1 to 10 for Time:
    const int t = rand() % 10 + 1;
    std::cout << "There is "<< t << " Time" << std::endl;
    const int number_of_fs = rand() % 10 + 1;
    std::cout << "Working with " << number_of_fs << " Fs" << std::endl;

    std::map<int, std::vector<int> > f;

    std::cout << "Time per F: 0 Not available, 1 available" << std::endl;

    for (int f_slot = 1; f_slot <= number_of_fs; ++f_slot) {
        for(int i = 0; i < t; i++){
            //random numbers 0 or 1:
            f[f_slot].push_back(rand() % 2);
        }
        std::cout << "The Time for F" << f_slot << " is " << std::endl;
        std::copy(f[f_slot].begin(), f[f_slot].end(), 
            std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
    }
}

看它跑

当然,这些容器内容的填充和打印可能应该被提取到单独的函数中,但我不想完全重写你的代码。

于 2013-03-12T04:49:57.407 回答
0

由于您使用的是 C++,并且可以访问标准库,因此可以像std::vector一样写得更干净:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;
int main()
{
    srand(time(0));

    //Random number from 3 to 7
    int numberOfVectors = (rand() % 7) + 3;

    //Random number from 1 to 10.
    int size = (rand() % 10) + 1;

    std::cout << "There are " << numberOfVectors << " vectors." << std::endl;
    std::cout << "Each vector has " << size  << " elements." << std::endl;

    std::vector< std::vector<int> > vectorOfVectorOfInt;

    std::cout << "Value per element: 0 = Not available, 1 = available" << std::endl;

    for(int vec = 0; vec < numberOfVectors; vec++)
    {
        //Create a new vector with 'size' elements.
        std::vector<int> newVector(size);

        for(int i = 0; i < size; i++)
        {
            //Generate a random value between 0 and 50
            newVector[i] = (rand() % 50);
        }

        //Add the vector to our vector-of-vectors.
        vectorOfVectorOfInt.push_back(newVector);

        std::cout << "The values for Vector #" << (vec+1) << " is:";
        for(int b = 0; b < size; b++)
        {
            int value = vectorOfVectorOfInt[vec][b];
            std::cout << "\t" << value;
        }
        std::cout << std::endl;
    }

    return 0;
}

您可以运行它并在此处查看结果:http: //ideone.com/RWQhjO

于 2013-03-12T04:41:56.377 回答
0

使用数组数组:

#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
   srand(time(0));
    //random numbers 1 to 10 for Time:
    int t = rand() % 10 + 1 ;
    cout << "Time per F: 0 Not available, 1 available";
    const int f_num = 5;
    cout << "There is "<< t << "Time";
    int* F = new int*[f_num];
    for(int i = 0; i < f_num; i++){
       F[i] = new int[t];
       cout << "The Time for F"<<i<<" is :";
       for(int j = 0; j < t; j++){
          //random numbers 0 or 1:
          F[i][j] = rand() % 2 ;
          cout << " "<< F1[i][j] <<" ";
       }
    }
}

顺便说一句,您的代码类似于 C。在 C++ 中,我们通常使用std::vector代替普通数组,并使用http://en.cppreference.com/w/cpp/numeric/random代替 rand() 。另外,不要使用using namespace std. 这可能会导致名称冲突。

于 2013-03-12T04:19:03.600 回答