big_hit

别人问个双色球的事,本人装逼意味随便写写就有了。记过估计弄了1个多点。NM自信心完全受打击。最后还是个丑陋至极的东西。

如果不用stl,搜索,随机产生并检测都要自己写。贪图了个安逸结果也没减少时间,CTM啊。开始用set,当然是为了贪图find(),结果不知道出的啥问题,反正肯定就是iterator的问题,烦人的.end()。后来没办法还是用vector,遍历搜索啥的还是没有避免,就为了用erase()来完成避免随机生成的数重复。可选择的多了反而2B了,要是只能用c还不让用STL估计时间也差不多。心情很差。

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <vector>
#include <math.h>
using namespace std;

#define RED_NUM 33
#define BLUE_NUM 16
#define IN_RED_NUM 6
#define IN_BLUE_NUM 1

vector<int> redball;
vector<int> blueball;

void init()
{
for (int i = 0; i< RED_NUM; i++)
{
redball.push_back(i + 1);
}
for (int i = 0; i< BLUE_NUM; i++)
{
blueball.push_back(i + 1);
}
}

int generateRed()
{
int val;
vector<int>::iterator it;
bool flag = false;
do
{
val = rand() % RED_NUM + 1;
for (it = redball.begin(); it != redball.end(); it++)
{
int temp = *it;
if (temp == val)
{
flag = true;
break;
}
}
} while (!flag);
redball.erase(it);
return val;
}

int generateBlue()
{
int val;
vector<int>::iterator it;
bool flag = false;
do
{
val = rand() % BLUE_NUM + 1;
for (it = blueball.begin(); it != blueball.end(); it++)
{
if ((*it) == val)
{
flag = true;
break;
}
}
} while (!flag);
blueball.erase(it);
return val;
}

bool checkIn(int val, int* arr, int num)
{
for (int i = 0; i < num; i++)
{
if (val == arr[i])
{
return true;
}
}
return false;
}

void main()
{
srand(time(NULL));
int red[IN_RED_NUM];
int blue;

int inRed[IN_RED_NUM];
int inBlue;

int hitRed = 0;
int hitBlue = 0;

init();

for (int i = 0; i < IN_RED_NUM; i++)
{
red[i] = generateRed();
}

blue = generateBlue();

cout<<"input red 6:"<<endl;
for (int i = 0; i < IN_RED_NUM; i++)
{
cin>>inRed[i];
}
cout<<"input blue 1:"<<endl;
cin>>inBlue;

for (int i = 0; i < IN_RED_NUM; i++)
{
if (checkIn(inRed[i], red, IN_RED_NUM))
{
hitRed++;
}
}
if (inBlue == blue)
{
hitBlue = 1;
}

cout<<"right red:";
for (int i = 0; i < IN_RED_NUM; i++)
{
cout<<red[i]<<" ";
}
cout<<endl;
cout<<"your red:";
for (int i = 0; i < IN_RED_NUM; i++)
{
cout<<inRed[i]<<" ";
}
cout<<endl;
cout<<"right blue:"<<blue<<endl;
cout<<"your blue:"<<inBlue<<endl;
cout<<"hits: red-"<<hitRed<<";blue-"<<hitBlue<<endl;

system("pause");
}



posted on 2012-02-17 18:29  shizuka  阅读(196)  评论(0编辑  收藏  举报

导航