教你用 84 行 C++ 代码实现洛谷测运势功能

今天一打开洛谷就看见

image.png

然后我就开始想怎么复刻这个效果,

因为不可能每种运势的概率相同,比如凶和中平不可能相同,所以我们要生成带权重的随机数,就是概率的大小。

我先百度搜了一下,搜到了这个代码

#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 ~ weight[0]为1,weight[0]+1 ~ weight[1]为2,依次类推
            for(int j = 0;j < size;j++)
            {
                tempSum += weight[j];
                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;
}
 

原理很简单,随机数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()%100,结果就是每一份权重。
再来一个for循环

for (int i = 0; i < length; i++)
{
    if (randVal <= w_list[i])
    {
        rward = i;
        break;
    }
    randVal -= w_list[i];
}

就可以了

完整代码:


#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std;
int rd(int a,int b){
	srand((unsigned)time(NULL));
	return (rand()%(b-a+1)+a);
}
int main(){
	system("color F0");
    srand((unsigned)time(NULL));
    int w_list[10] = { 		2, 			4, 		 15, 	 15, 	 16 ,  	 16 ,  25 ,    7 , 	5 };
    string names[10] = { "宇宙超级凶", "大凶", "中平", "小平", "小凶" ,"中吉","小吉","超级吉","中凶" };
    string yi_list[100][100]={
    						{"宜:诸事不宜","宜:诸事不宜","宜:诸事不宜","宜:诸事不宜"},
    						{"宜:装弱","宜:窝在家里","宜:刷题","宜:吃饭"},
    						{"宜:刷题","宜:开电脑","宜:写作业","宜:睡觉"},
							{"宜:发朋友圈","宜:出去玩","宜:打游戏","宜:吃饭"},
							{"宜:学习","宜:研究Ruby","宜:研究c#","宜:玩游戏"},
							{"宜:膜拜大神","宜:扶老奶奶过马路","宜:玩网游","宜:喝可乐"},
							{"宜:吃东西","宜:打sdvx","宜:打开洛谷","宜:出行"},
							{"宜:写程序","宜:刷题","宜:偷塔","宜:上CSDN"},
							{"宜:扶老奶奶过马路","宜:上课","宜:写作业","宜:写程序"},
						  	}; 
	string yi_shi_list[100][100]={
    						{"","","",""},
    						{"谦虚最好了","不出门没有危险","直接AC","吃的饱饱的再学习"},
    						{"一次AC","发现电脑死机了","全对","睡足了再学习"},
							{"点赞量破百","真开心","十连胜","吃饱了"},
							{"都会","有了新发现","发现新大陆","直接胜利"},
							{"接受神之沐浴","增加RP","犹如神助","真好喝"},
							{"吃饱了","今天状态好","发现AC的题变多了","路途顺畅"},
							{"不会报错","直接TLE","胜利","发现粉丝涨了200个"},
							{"增加RP","听懂了","都会","没有Bug"},
						  	}; 
	string ji_list[100][100]={
    						{"忌:诸事不宜","忌:诸事不宜","忌:诸事不宜","忌:诸事不宜"},
    						{"忌:打sdvx","忌:出行","忌:玩手机","忌:吃方便面"},
    						{"忌:关电脑","忌:开挂","忌:纳财","忌:考试"},
							{"忌:膜拜大神","忌:评论","忌:研究Java","忌:吃方便面"},
							{"忌:发朋友圈","忌:打开洛谷","忌:研究C++","忌:出行"},
							{"忌:探险","忌:发视频","忌:发博客","忌:给别人点赞"},
							{"忌:写程序","忌:使用Unity打包exe","忌:装弱","忌:打开CSDN"},
							{"忌:点开wx","忌:刷题","忌:打吃鸡","忌:和别人分享你的程序"},
							{"忌:纳财","忌:写程序超过500行","忌:断网","忌:检测Bug"},
						  	}; 
	string ji_shi_list[100][100]={
    						{"","","",""},
    						{"今天状态不好","路途也许坎坷","好家伙,直接死机","没有调味料"},
    						{"死机了","被制裁","你没有财运","没及格"},
							{"被人嘲笑","被喷","心态崩溃","只有一包调味料"},
							{"被人当成买面膜的","大凶","五行代码198个报错","路途坎坷"},
							{"你失踪了","被人喷","阅读量1","被人嘲笑"},
							{"报错19999+","电脑卡死,发现刚才做的demo全没了","被人看穿","被人陷害"},
							{"被人陷害","WA","被队友炸死","别人发现了Bug"},
							{"没有财运","99+报错","连不上了","503个Bug"},
						  	};
    int w_sum = 0;
    int length = sizeof(w_list) / sizeof(int);
    for (int i = 0; i < length; i++){
        w_sum += w_list[i];
    }
    int randVal = rand() % w_sum;
    int rward = 0;
    for (int i = 0; i < length; i++){
        if (randVal <= w_list[i]){
            rward = i;
            break;
        }
        randVal -= w_list[i];
    }
    cout<<"                你的运势是:"<<endl;
    printf("                 §%s§\n", names[rward].c_str());
    for (int ii=0;ii<9;ii++){
		if (names[ii]==names[rward].c_str()){
			cout<<"         "<<yi_list[ii][rd(0,3)];
			cout<<"    "<<ji_list[ii][rd(0,3)]<<endl;
			cout<<"         "<<yi_shi_list[ii][rd(0,3)];
			cout<<"    "<<ji_shi_list[ii][rd(0,3)];
			break;
		}
	}
    return 0;
}

我还没有用出压码神技,如果用了,最多20行代码。。。