大家好,关于射手座intp很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于处女座的int的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!
本文目录
c+n=意思算命C语言,星座匹配问题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~ 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;
}
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
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()%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];
}
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
这是随机权重完整一点的代码,加上了随机的名字
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];
}
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
最后输出结果的时候,就直接输出names[rward].c_str()就可以啦!
哈哈!
我凭借我的智慧写出了如此简单的代码!

代码
好了,最核心的东西都讲完了,上完整代码!!(Dev-c++编译通过)
#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§
", 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;
1)建立两个数组 a[2];b[2]分别存储第一人的月,日,第二人的月,日,从合理性角度分析
月a[0]∈[1,12],a[1]也就是日期根据a[0]决定,a[0]是1,3,5,7,8,12情况下,a[1]不能是31.
a[0]是2的情况下,a[1]不能超过29.如果输入29要减去1(为了后面的日期差计算星座做基础)
2)第一个函数int sum_day(int* a),把刚才输入作为形参传入,按照一定公式计算N月M日是这年的第K天,然后返回这个日期。
3)因为每个星座之隔都是30天(2月按照28来看)。把这个天数,假设是返回的k,那么分类讨论,
第一种,k<19,那么直接知道是摩羯
第二种 switch((k-19)/30){
case 0:水瓶
case 1:双鱼
........
4)匹配的规则,我们已经知道双方的星座了,至于是否合得来用一个二维数组来存放情况
int c[12][12];横坐标的0~11分别表示水瓶~摩羯,纵坐标也是一个道理。比如
a[0][0]就是水瓶配水瓶,c[11][11]就是摩羯配摩羯。如果c[0][0]是存放1,则表示水瓶配水瓶是合适的,如果是0则表示不合适,1还是0由编程人员设置。
3)输出,(k-19)/30就是下标,例如c [(k1-19)/30][,(k2-19)/30],那么这将表示这个坐标的数据,1
表示合适,0表示不合适,k是表示这一年的第k天。
好了,文章到这里就结束啦,如果本次分享的射手座intp和处女座的int问题对您有所帮助,还望关注下本站哦!