体育彩票的模拟生成和兑奖c程序

来源:互联网 发布:淘宝钻展位置 编辑:程序博客网 时间:2024/05/01 03:51

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define MM 7
#define NN 36


typedef int ElemTp;
typedef struct
{
 ElemTp elem[MM+1];
 int len;
} VoteTp;
typedef struct
{
 ElemTp elem[NN+1];
 int len;
} SourceTp;
SourceTp source;
InitializeVote(VoteTp *vote);
InitializeSource(SourceTp *source);
void SeqInsertVote(VoteTp *vote,int i,ElemTp x);
int SeqLocate(VoteTp v,ElemTp x);
VoteTp CreateAVote();
int RightNum(VoteTp vote,VoteTp answervote);
main()
{
 VoteTp vote;
 VoteTp answervote;
 int k,i;
 randomize();
 InitializeSource(&source);
 answervote=CreateAVote();
 printf("/nPress Any Key to Continue ,0 to exit !");
 printf("/n Answer Numbers: ");
 for (i=1;i<=MM;i++)
  printf("%3d ",answervote.elem[i]);
 printf("/n Your Vote Numbers      ---->>Right Numbers /n");
 while (getchar()!='0')
 {
  vote=CreateAVote();
  for (i=1;i<=MM;i++)
   printf("  %-2d  ",vote.elem[i]);
  k=RightNum(vote,answervote);
  printf("  ---->>  %d /n",k);
 }
}
InitializeVote(VoteTp *vote)
{
 vote->len=0;
}
InitializeSource(SourceTp *Source)
{
 int i;
 for(i=1;i<=NN;i++)
  Source->elem[i]=i;
 Source->len=NN;
}

int SeqLocate(VoteTp v,ElemTp x)
{
 int j=1;
 while(j<=v.len&&v.elem[j]!=x)
  j++;
 if(j<=v.len)
  return j;
 else
  return 0;
}
void SeqInsertVote(VoteTp *vote,int i,ElemTp x)
{
 VoteTp v;
 int j;
 v=*vote;
 if((i<1)||(i>v.len+1))
  printf(" error number!");
 else
 {
  for(j=v.len;j>=i;j--)
   v.elem[j+1]=v.elem[j];
  v.elem[i]=x;
  v.len=v.len+1;
 }
 *vote=v;
}
int RightNum(VoteTp vote,VoteTp answervote)
{
 int i,k;
 k=0;
 for (i=1;i<=MM;i++)
  if(SeqLocate(vote,answervote.elem[i])>0)
   k++;
 return(k);
}
VoteTp CreateAVote()
{
 VoteTp vote;
 ElemTp k,temp;
 int i;
 InitializeVote(&vote);
 source.len=NN;
 for(i=1;i<=MM;i++)
 {
  k=random(source.len)+1;
  SeqInsertVote(&vote,vote.len+1,source.elem[k]);
  temp=source.elem[k];
  source.elem[k]=source.elem[source.len];
  source.elem[source.len]=temp;
  source.len=source.len-1;
 }
 return vote;
}

原创粉丝点击