s队算命 历届s赛冠军队标

2023-10-11 10:20:43
版权说明:以下内容来自网友投稿,若有侵权请联系: 处理。

c n=意思是算命。

84行C 代码教你实现洛谷占卜功能

因为我们要随机用户的运势,但是不可能每种运势的几率都相等,所以需要生成带权重的随机数

看到这个需求,先百度一下

百度到了这个代码

#include<iostream>

#include<vector>

#include<numeric>

#include<ctime>

#include<cstdlib>

using std::vector;

using std::rand;

using std::srand;

using std::cout;

using std::endl;

class MyMath{

public:

vector<int> GetRandomNumWithWeight(vector<int> weight,int number){

int size= weight.size();

vector<int> res;

int accumulateValue= accumulate(weight.begin(),weight.end(),0);

srand(time(0));// srand()一定要放在循环外面或者是循环调用的外面,否则的话得到的是相同的随机数

for(int i= 0;i< number; i )

{

int tempSum= 0;

int randomNnm= 0;

randomNnm= rand()% accumulateValue;

//0~ weight0为1,weight0 1~ weight1为2,依次类推

for(int j= 0;j< size;j )

{

tempSum = weightj;

cout<< randomNnm<< endl;

if(randomNnm<= tempSum)

{

res.push_back(j 1);

break;

}

}

}

return res;

}

};

int main()

{

vector<int> weight={1000, 2000, 3000, 1000, 1000, 500, 500, 500, 500};//数字1-9的权重(这里的数字范围与权重都可以自定义)

MyMath myMath;

vector<int> result= myMath.GetRandomNumWithWeight(weight,5);

for(auto const&num:result)

{

cout<< num<<'';

}

cout<< endl;

return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

snh48s队成员名单

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

这个代码可以实现我们想要的随机数效果,

原理很简单,随机数ranIndex生成的区间为权重的总和,根据权重分割子区间。

但代码有点复杂,其实没必要辣么麻烦

所以,还是自己动手,丰衣足食!!!

大概的原理如下:

我们先定义一个整数数组 w_list,用来存储我们随机的权重。

再定义一个w_sum,用来存权重总和。

再定义一个 lenth里面存数组的长度int length= sizeof(w_list)/ sizeof(int);

然后,一个for循环,用w_sum把w_list的每一项累加起来。

再int一个randVal,把每一份权重存到里面。int randVal= rand()% w_sum;

这一步可能有点难懂,举个例子,一共有100份权重(权重总和是100),我们用rand()0,结果就是每一份权重。

练一下英语:

Let’s start by defining an integer array w_list to store our random weights.

Define w_sum to store the sum of weights.

Int length= sizeof(w_list)/sizeof(int);

Then, a for loop adds up each item of the w_list with w_sum.

Int randVal and store each weight in it. int randVal= rand()% w_sum;

This step can be a little confusing, for example, if there are 100 weights(the total weight is 100), we use rand()0, and the result is each weight.

再int一个rward,接下来一个for循环,

就搞定啦!

这是这一小部分的代码:

for(int i= 0; i< length; i )

{

if(randVal<= w_listi)

{

rward= i;

break;

}

randVal-= w_listi;

}

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

这是随机权重完整一点的代码,加上了随机的名字

srand((unsigned)time(NULL));

int w_list10={ 2, 4, 15, 15, 16, 16, 25, 7, 5};

string names10={"宇宙超级凶","大凶","中平","小平","小凶","中吉","小吉","超级吉","中凶"};

int w_sum= 0;

int length= sizeof(w_list)/ sizeof(int);

for(int i= 0; i< length; i )

{

w_sum = w_listi;

}

int randVal= rand()% w_sum;

int rward= 0;

for(int i= 0; i< length; i )

{

if(randVal<= w_listi)

{

rward= i;

break;

}

randVal-= w_listi;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

最后输出结果的时候,就直接输出namesrward.c_str()就可以啦!

哈哈!

我凭借我的智慧写出了如此简单的代码!

代码

好了,最核心的东西都讲完了,上完整代码!!(Dev-c 编译通过)

#include<iostream>

#include<time.h>

#include<windows.h>