本帖最后由 jhq999 于 2021-10-10 17:22 编辑 #include "stdio.h"
#include "stdlib.h"
#include <time.h>
struct SHOOT
{
int name;
bool isAlive;
double prob;
double shootnum;
double sezong;
double live;
};
void Shoot(bool & targetAlive,double accuracy);
int StartDuel(SHOOT *inshts,int shtnum);
void Probsort(SHOOT *inshts,int shtnum);
int main(int argv,char argc[])
{
SHOOT sht[3]={0};
sht[0].name=0,sht[0].isAlive=1,sht[0].prob=1.0/3.0;
sht[1].name=1,sht[1].isAlive=1,sht[1].prob=1.0/2.0;
sht[2].name=2,sht[2].isAlive=1,sht[2].prob=1.0;
srand(unsigned int(time(NULL)));
for (int i = 0; i < 1000; i++)
{
sht[0].isAlive=1;
sht[1].isAlive=1;
sht[2].isAlive=1;
int l=StartDuel(sht,3);
//printf("\nLive is %d\n",l);
}
for (int i = 0; i < 3 ;i++)
{
if (sht[i].shootnum)
{
printf ("%d prob is %.2lf liveprob is %.2lf\n",sht[i].name,sht[i].sezong/sht[i].shootnum,sht[i].live/1000);
}
else
{
printf ("%d prob is %.2lf liveprob is %.2lf\n",sht[i].name,sht[i].prob,sht[i].live/1000);
}
}
return 0;
}
void Probsort(SHOOT *inshts,int shtnum)
{
bool flag=true;
SHOOT tmp={0};
for (int i = 0; i < shtnum; i++)
{
for (int j = i+1; j < shtnum; j++)
{
if (inshts[j].prob<inshts[i].prob)
{
tmp=inshts[i];
inshts[i]=inshts[j];
inshts[j]=tmp;
flag=false;
}
}
if (flag)
{
break;
}
}
}
int StartDuel(SHOOT *inshts,int shtnum)
{
bool isexit=0;
int i=0,j=0,live=-1,start=1;
while (true)
{
isexit=1;
for (i = start; i < shtnum; i++)
{
start=0;
if (inshts[i].isAlive)
{
for (j = shtnum-1; j>=0; j--)
{
if (inshts[j].isAlive&&i!=j)
{
inshts[i].shootnum++;
Shoot(inshts[j].isAlive,inshts[i].prob);
if (!inshts[j].isAlive)
{
inshts[i].sezong++;
live=i;
//printf("%d kill %d,",inshts[i].name,inshts[j].name);
if (inshts[j].shootnum)
{
inshts[j].prob=inshts[j].prob>inshts[j].sezong/inshts[j].shootnum?
inshts[j].prob:inshts[j].sezong/inshts[j].shootnum;
}
}
isexit=0;
break;
}
}
}
}
if (isexit)
{
break;
}
}
//Probsort(inshts,shtnum);
inshts[live].live++;
return inshts[live].name;
}
void Shoot(bool & targetAlive,double accuracy)
{
int i=0;
//for (; i < rand()%100; i++);
i=(rand()%100);
targetAlive=i>int(accuracy*100)?1:0;
}
|