HDU1014 Uniform Generator

简介:
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1014

这道题就是题目有点难懂,其实难度很小,就是产生伪随机数时给出的步长step和上限mod,判断下这两个值是否可以让产生的随机数均匀分布,所谓的均匀分布就是在mod步里能产生出0到mod-1这mod个数。

#include <iostream>
using namespace std;

const long int MAX_MOD = 100000;
bool gened[MAX_MOD] = {false};//数是否产生

void doRandomTest(int step,int mod)
{
    int seed = 0,tmp,i;//初始种子值为零
    for(i=0;i<mod;++i)
    {//刚开始数都未产生
        gened[i] = false;
    }
    gened[0] = true;//第一个产生的是零
    do
    {
        tmp = (seed+step)%mod;
        gened[tmp] = true;//第tmp个数已经产生了
        seed = tmp;//更新种子数
    }while(seed!=0);
    for(i=0;i<mod;++i)
    {
        if(gened[i]==false)//有数没有产生出来
            break;
    }
    if(i==mod)
    {
        printf("%10d%10d    Good Choice\n",step,mod);
    }
    else
        printf("%10d%10d    Bad Choice\n",step,mod);
    cout<<endl;
}

int main(int argc,char* argv[])
{
    int step,mod,i;
    while(cin>>step>>mod)
    {
        doRandomTest(step,mod);
    }
    return 0;
}


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/12/29/1019397.html,如需转载请自行联系原作者
目录
相关文章
|
9月前
UVa1583 - Digit Generator
UVa1583 - Digit Generator
34 0
|
9月前
|
Python
Python random 随机函数(random、uniform、randint、choice、choices、randrange、shuffle、sample)
Python random 随机函数(random、uniform、randint、choice、choices、randrange、shuffle、sample)
77 0
Leetcode-Easy 867.Transpose Matrix
Leetcode-Easy 867.Transpose Matrix
68 0
Leetcode-Easy 461.Hamming Distance
Leetcode-Easy 461.Hamming Distance
88 0
Leetcode-Easy 461.Hamming Distance
HDOJ 1014 Uniform Generator(公约数问题)
HDOJ 1014 Uniform Generator(公约数问题)
74 0
HDU 1014 Uniform Generator【GCD,水】
Uniform Generator Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29336    Accepted Submissio...
1150 0