#include<iostream> #include<ctime> #include<cstdlib> using namespace std; int getWinNum(int * winNum) { srand((unsigned)time(NULL)); //产生随机数作为中奖号码 for(int i=0;i<6;i++) //随机产生6个普通球号码(在1-31中间的号码) { winNum[i]=rand()%31+1; for(int j=0;j<i;j++) { if(winNum[i]==winNum[j]) //若产生相同号码,则重新设置此号码 { i--; } } } winNum[6]=rand()%15+1; //随机产生特殊球号码(在1-15中间的号码) return 0; } int getWinGrade(int* inputNum,int* winNum) //判断得奖的等级 { int winCount=0; int winGrade=0; for(int i=0;i<7-1;i++) //计算所中普通球号码数量 { for(int j=0;j<7;j++) { if(inputNum[i]==winNum[j]) { winCount++; } } } if(inputNum[6]==winNum[6]&&winCount<4&&winCount>=0) //特殊奖(中了特殊球号码且中普通球小于4个) { winCount=1; } else if((inputNum[6]==winNum[6])&&(winCount==4)) //三等奖 { winCount=2; } else if((inputNum[6]==winNum[6])&&(winCount==5)) //二等奖 { winCount=3; } else if((inputNum[6]==winNum[6])&&(winCount==6)) //特等奖 { winCount=4; } else { winCount=0; //没得奖 } return winCount; } void showMes(int winGrade) { switch(winGrade) { case 1: cout<<"Congratulations!you win the autograph prize! "<<endl; break; case 2: cout<<"Congratulations!you win the third prize!"<<endl; break; case 3: cout<<"Congratulations!you win the second!"<<endl; break; case 4: cout<<"Congratulations!you win the biggest prize!"<<endl; break; default: cout<<"I'm sorry!you do not win any prize!"<<endl; break; } return; } int main() { int* inputNum=new int[7]; //输入的彩球号码 int* winNum=new int[7]; //中奖彩球号码 int winGrade; //中奖等级 cout<<"please input the seven ball-numbers(the first to sixth between 1-31 and should be different,the last between 1-15):"<<endl; for(int i=0;i<sizeof(inputNum);i++) { cin>>inputNum[i]; } getWinNum(winNum); //抽出中奖彩球号码 winGrade=getWinGrade(inputNum,winNum); //判断中奖等级 showMes(winGrade); //显示中将信息 cout<<"the winNumber are:"; //显示全部中奖彩球号码 for(i=0;i<7;i++) { cout<<winNum[i]<<" "; } cout<<endl; delete inputNum; delete winNum; return 0; }