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

hello!!

我是YR,一个闲的蛋疼的C++程序员

今天,我又开始闲的没事,

便打开洛谷,发现

84行C++代码教你实现洛谷占卜功能_第1张图片

我就想复刻这个简单的占卜系统 

话不多说,上效果!

效果

84行C++代码教你实现洛谷占卜功能_第2张图片

怎么样?还不错吧,只要84行代码实现呦!

生成带权重的随机数

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

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

百度到了这个代码

#include 
#include 
#include 
#include 
#include 

using std::vector;
using std::rand;
using std::srand;
using std::cout;
using std::endl;

class MyMath{
public:
    vector GetRandomNumWithWeight(vector weight,int number){
        int size = weight.size();
        vector 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 weight = {1000, 2000, 3000, 1000, 1000, 500, 500, 500, 500 };//数字1-9的权重(这里的数字范围与权重都可以自定义)
    MyMath myMath;
    vector 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,结果就是每一份权重。

练一下英语:

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()%100, and the result is each weight.

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

就搞定啦!

这是这一小部分的代码:

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

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

srand((unsigned)time(NULL));
    int w_list[10] = { 		2, 			4, 		 15, 	 15, 	 16 ,  	 16 ,  25 ,    7 , 	5 };
    string names[10] = { "宇宙超级凶", "大凶", "中平", "小平", "小凶" ,"中吉","小吉","超级吉","中凶" };
    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];
    }

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

哈哈!

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

84行C++代码教你实现洛谷占卜功能_第3张图片

代码

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

#include 
#include 
#include 
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<<"                你的运势是:"<

拜拜!

再见!

这次的博客就写到这里,因为作者关注量已经到了2000,所以关注我的可能有一些不能会关了,请谅解。

拜拜

84行C++代码教你实现洛谷占卜功能_第4张图片

 

你可能感兴趣的:(c++)