導航:首頁 > 源碼編譯 > 隨機分組小程序源碼

隨機分組小程序源碼

發布時間:2023-05-16 05:32:24

A. C語言:隨機生成100個數,要求分成兩組,一組50個數,讓這兩組的和相差盡量小。

以下函數通過系統時間隨機生成1-1000之間的隨機數,分成兩組後使其總和差值最小。
演算法分析:見源程序中的桐咐畢注釋,演算法可能有些許額誤差,共參考。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//******************產生長度為length的不為0的可重復隨機數組******************//
void radomArray(int *array,int length)
{
srand((unsigned)time(NULL));//使用系統時間作為隨機數的種子產生隨機數
int i=0,j=0;
while(j<length)
{
i=rand()%1000;/簡斗/0-1000的隨機數,可做修改
if(i!=0)//隨機數不為0
{
array[j]=i;
j++;
}
}
}

//******************給數組排序,從大到小******************//
void sequence(int * array,int len)
{
int tmp=0;
for(int m=0;m<len;m++)
{
for(int n=0;n<len;n++)
{
if(array[m]>array[n])
{
tmp=array[n];array[n]=array[m];array[m]=tmp;
}
else continue;
}
}
}

//******************將數組拆分為長度為len_01和長度為len_02的兩列並總和差值最小******************//
/*演算法分析:將有序數組(大到小)中數據依次分別放入數組1、數組2中,並分別統計其現有數據的總和,分別存入dat_01,dat_02中,

初始化判斷標准dat_01,dat_02為0;
根據數組現有數據總和dat_01和dat_02的關系,確定數據存入數組1還是數組2。
判斷過程:如果數組1現有數據總和小於數組2,同時數組1未到上限,將下一個數據存入數組1;
如果數組1現有數據綜合不小於數組2,同時數組2未局芹到上限,將下一個數據存入數組2。
可能漏洞:數組1或數組2提前到達數組上限,導致原數組中剩餘數據直接轉入另一數組中,出現異常結果。*/
void seperate(int *arr,int * arr_01,int *arr_02,int len_01,int len_02)
{
int i=0,j=0,dat_01=0,dat_02=0;
for(int k=0;k<len_01+len_02;k++)
{
if(dat_01<dat_02&&i<len_01)//如果數組1數據總和小於數組2,同時數組1未到上限,將下一個數據存入數組1
{
arr_01[i]=arr[k];
dat_01+=arr[k];
i++;
}
else if(dat_01>=dat_02&&j<len_02)//如果數組1數據不小於數組2,同時數組2未到上限,將下一個數據存入數

組2
{
arr_02[j]=arr[k];
dat_02+=arr[k];
j++;
}

}
}

//******************計算並返回長度為length的數組的數字總和******************//
int sum(int a[],int length)
{
int total=0;
for(int i=0;i<length;i++)
{
total+=a[i];
}
return total;
}

//******************主函數開始******************//
void main()
{
int data1=0,data2=0,tmp=0;
int len=100,len_01=50,len_02=50;//修改此處len,len_01,len_02即可修改初始數組長度,注意len_01+len_02=len,同

時最好len_01=len_02,防止出現異常。
int data[100],data_1[50],data_2[50];//上述len,len_01,len_02修改後,此處數組長度應相應修改。
radomArray(data,len);//產生100個數據的隨機數組
sequence(data,len);//給隨機數組從小到大排序

printf("產生的隨機數組從小到大排列為:\n");
for(tmp=0;tmp<len;tmp++)//輸出隨機數組
printf("%d\t",data[tmp]);
printf("\n");

seperate(data,data_1,data_2,len_01,len_02);//將數組分為兩塊,使其總和差值最小

printf("第一塊數組為:\n");
for(tmp=0;tmp<len_01;tmp++)
{
printf("%d\t",data_1[tmp]);
}
printf("\n");
printf("第二塊數組為:\n");
for(tmp=0;tmp<len_02;tmp++)
{
printf("%d\t",data_2[tmp]);
}
printf("\n");

printf("最原始數組的數據總和為:%d\n",sum(data,len));
printf("拆分後數組的數據總和為:%d\n",sum(data_1,len_01)+sum(data_2,len_02));
printf("第一塊數組的數據總和為:%d\n",sum(data_1,len_01));
printf("第二塊數組的數據總和為:%d\n",sum(data_2,len_02));
printf("兩塊數組數據的的差值為:%d\n",sum(data_1,len_01)-sum(data_2,len_02));
}
運行結果:
產生的隨機數組從小到大排列為:
993 979 970 962 957 935 914 902 888 883
871 866 862 859 843 840 829 827 824 815
810 808 805 796 784 768 738 730 725 722
721 712 711 709 704 702 693 670 670 650
631 627 623 623 614 607 606 598 577 574
547 498 495 495 481 478 451 448 447 429
410 388 383 381 380 379 377 364 344 337
308 271 266 257 233 208 204 200 189 165
162 155 150 145 143 119 117 114 114 110
108 69 64 61 48 45 22 16 11 3

第一塊數組為:
979 970 957 902 883 871 859 840 829 815
810 805 768 738 725 721 709 702 670 670
631 623 607 606 574 498 495 481 451 447
410 383 379 377 337 271 266 233 200 189
155 145 119 117 114 108 61 45 11 3

第二塊數組為:
993 962 935 914 888 866 862 843 827 824
808 796 784 730 722 712 711 704 693 650
627 623 614 598 577 547 495 478 448 429
388 381 380 364 344 308 257 208 204 165
162 150 143 114 110 69 64 48 22 16

最原始數組的數據總和為:51116
拆分後數組的數據總和為:51116
第一塊數組的數據總和為:25559
第二塊數組的數據總和為:25557
兩塊數組數據的的差值為:2
Press any key to continue

B. 如何使用c語言對數據隨機分組

#include棗鄭<stdio.h>
#include<stdlib.h>
#include檔岩漏<time.h>

intmain()
{
inta[50];//存放50個球
inti,cnt;
for(i=0;i<50;i++)//標號
a[i]=i+1;
cnt=0;
srand(time(NULL));
while(cnt<25)//隨機取25個
{
i=rand()%50;
if(a[i]!=0)//防止重復計數
{
cnt++;
a[i]=0;//標記為已取
}
}
//以下為輸出分組結果
printf("array1: "行爛);
cnt=0;
for(i=0;i<50;i++)
{
if(a[i]==0)
{
cnt++;
printf("%3d",i+1);
if(cnt%5==0)//每輸出5個一換行
printf(" ");
}
}
printf(" ");
printf("array2: ");
for(i=0;i<50;i++)
{
if(a[i])
{
cnt++;
printf("%3d",a[i]);
if(cnt%5==0)
printf(" ");
}
}
printf(" ");
return0;
}

C. 用c# 編寫一個隨機分組的程序

這么寫就行了,下圖是運行結果

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Threading;

namespaceConsoleApplication

{

classProgram

{

staticvoidMain(string[]args)

{

intnumber=42;//人數

intgroups=7;//組數

唯皮枯string[]strArr=Group(number,groups);

for(inti=0;i<strArr.Length;i++)

Console.WriteLine("第"+(i+1)+"組"+strArr[i]);

Console.ReadKey();

}

staticstring[]Group(intnumber,intgroups)

{

List<int>list=newList<int>();

intnum=number/7;

string[]strArr=newstring[groups];

for(inti=1;i<=number;i++)

list.Add(i);

for(inti=0;i<groups;i++)

{

for(intj=0;j<num;j++)

{

intvalue=list[newRandom((int)DateTime.Now.Ticks).Next(0,list.Count)];

list.Remove(value);

握亮strArr[i]+=value.ToString("D2")+"";

指洞Thread.Sleep(20);

}

}

returnstrArr;

}

}

}

D. 用vb:編一個隨機分組程序

我想了一下,編了個滿足你要求的,並加上詳細的註解,運行的結果是對的,如圖雀賣,我運行了三次,每次編排的結果都不一樣。

PrivateSubCommand1_Click()

List1.Clear

Randomize'隨機數初始化,這個非常重要!

DimXX(1To8)AsInteger,YY(1To8,1To6)AsInteger

Fori=1To48'對人進行循環

Z=False'此人未分配

DoWhile(Z=False)'只要此人未分配,就一直循環

A=Rnd(1)

B=Int(7*A+1+0.5)'先把i分配到B組,B的值肯定在1,2,3,。。。8之間

If橘胡XX(B)<6Then'當B組的人數不足6人時,就分給B組

XX(B)=XX(B)+1'這個組的人數+1

YY(B,XX(B))=i

頃伍逗Z=True'此人已分配

EndIf

Loop

Nexti

Fori=1To8

List1.AddItem"第"&Str(i)&"組:"

Forj=1To6

List1.AddItemYY(i,j)

Nextj

Nexti

EndSub

E. 用C語言編寫一個隨機分組的程序

int main(){
time_t ts;
srand((unsigned int)time(&ts));
scanf("%d",n);
for(int i=0;i<n;i++){
int a=rand()%100;
if(條沒橘件){

}
if(條答察物清液件){

}
if(條件){

}
}
}

F. 微信隨機分組小程序技巧

1、使用微信小程序的介面來獲取用戶的openid,然後根據openid來生成一個隨機的分組;
2、可以使用微信小程序的消息推送功能,將用戶分組的信息推送給用戶;
3、可以使彎腔用微信小程序的資料庫功能,將用戶的openid和分組信息存儲起來,以便以後使用;
4、可以使用微信小程序的雲函數功能,實現定時自動分組,以及定時自動推送分組信息;
5、可以使用微信小程序的客服消息功能,實現用戶主動查詢自己的分組信息;
6、可以使用微信小程序的客服消息功能,實現用戶主動申請更改分組信息;
7、可以使用微信小程序的客服消息功能,實現用戶主動申請取消分組信息;槐鬧碧
8、可以使用微信小程序的客服消息功能,實現用戶主動申請查看其他用戶鉛舉的分組信息;
9、可以使用微信小程序的客服消息功能,實現用戶主動申請查看某一分組的所有用戶信息;
10、可以使用微信小程序的客服消息功能,實現用戶主動申請查看某一分組的所有用戶的openid。

G. C#:設計一個程序,能夠隨機分組,並輸出分組名單。

這類似於數組..首先用聲明一個數組和list在使用行滾游兩個for循環用數組添加每個的元素在用list把數組添加進去(數組也可用list代替)最後備源用list循環顯示到控制台我只提供思路具體應該你可以實現的檔銷

H. 50個人怎麼隨機分組python

1、首先在python程序上輸入總人數50人(N),和所需要的分組數n,即可進行隨機分組。
2、如果N不是n的整數倍,則n個小組中的人數會不相同。以上就是50個人在python程序隨機分組的方法。

I. 求C語言小程序源代碼,300行左右

黑白棋游戲
#include "graphics.h" /*圖形系統頭文件*/
#define LEFT 0x4b00 /*游標左鍵值*/
#define RIGHT 0x4d00 /*游標右鍵值*/
#define DOWN 0x5000 /*游標下鍵值*/
#define UP 0x4800 /*游標上鍵值*/
#define ESC 0x011b /* ESC鍵值*/
#define ENTER 0x1c0d /* 回車鍵值*/
int a[8][8]={0},key,score1,score2;/*具體分數以及按鍵與存放棋子的變數*/
char playone[3],playtwo[3];/*兩個人的得分轉換成字元串輸出*/
void playtoplay(void);/*人人對戰函數*/
void DrawQp(void);/*畫棋盤函數*/
void SetPlayColor(int x);/*設置棋子第一次的顏色*/
void MoveColor(int x,int y);/*恢復原來棋盤狀態*/
int QpChange(int x,int y,int z);/*判斷棋盤的變化*/
void DoScore(void);/*處理分數*/
void PrintScore(int n);/*輸出成績*/
void playWin(void);/*輸出勝利者信息*/
/******主函數*********/
void main(void)
{
int gd=DETECT,gr;
initgraph(&gd,&gr,"c:\\tc"); /*初始化圖形系統*/
DrawQp();/*畫棋盤*/
playtoplay();/*人人對戰*/
getch();
closegraph();/*關閉圖形系統*/
}
void DrawQp()/*畫棋盤*/
{
int i,j;
score1=score2=0;/*棋手一開始得分都為0*/
setbkcolor(BLUE);
for(i=100;i<=420;i+=40)
{
line(100,i,420,i);/*畫水平線*/
line(i,100,i,420); /*畫垂直線*/
}
setcolor(0);/*取消圓周圍的一圈東西*/
setfillstyle(SOLID_FILL,15);/*白色實體填充模式*/
fillellipse(500,200,15,15); /*在顯示得分的位置畫棋*/
setfillstyle(SOLID_FILL,8); /*黑色實體填充模式*/
fillellipse(500,300,15,15);
a[3][3]=a[4][4]=1;/*初始兩個黑棋*/
a[3][4]=a[4][3]=2;/*初始兩個白棋*/
setfillstyle(SOLID_FILL,WHITE);
fillellipse(120+3*40,120+3*40,15,15);
fillellipse(120+4*40,120+4*40,15,15);
setfillstyle(SOLID_FILL,8);
fillellipse(120+3*40,120+4*40,15,15);
fillellipse(120+4*40,120+3*40,15,15);
score1=score2=2; /*有棋後改變分數*/
DoScore();/*輸出開始分數*/
}
void playtoplay()/*人人對戰*/
{
int x,y,t=1,i,j,cc=0;
while(1)/*換棋手走棋*/
{
x=120,y=80;/*每次棋子一開始出來的坐標,x為行坐標,y為列坐標*/
while(1) /*具體一個棋手走棋的過程*/
{
PrintScore(1);/*輸出棋手1的成績*/
PrintScore(2);/*輸出棋手2的成績*/
SetPlayColor(t);/*t變數是用來判斷棋手所執棋子的顏色*/
fillellipse(x,y,15,15);
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*跳出遊戲*/
break;
else
if(key==ENTER)/*如果按鍵確定就可以跳出循環*/
{
if(y!=80&&a[(x-120)/40][(y-120)/40]!=1
&&a[(x-120)/40][(y-120)/40]!=2)/*如果落子位置沒有棋子*/
{
if(t%2==1)/*如果是棋手1移動*/
a[(x-120)/40][(y-120)/40]=1;
else/*否則棋手2移動*/
a[(x-120)/40][(y-120)/40]=2;
if(!QpChange(x,y,t))/*落子後判斷棋盤的變化*/
{
a[(x-120)/40][(y-120)/40]=0;/*恢復空格狀態*/
cc++;/*開始統計嘗試次數*/
if(cc>=64-score1-score2) /*如果嘗試超過空格數則停步*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
break;
}
else
continue;/*如果按鍵無效*/
}
DoScore();/*分數的改變*/
break;/*棋盤變化了,則輪對方走棋*/
}
else/*已經有棋子就繼續按鍵*/
continue;
}
else /*四個方向按鍵的判斷*/
if(key==LEFT&&x>120)/*左方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x-=40;
fillellipse(x,y,15,15);
}
else
if(key==RIGHT&&x<400&&y>80)/*右方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x+=40;
fillellipse(x,y,15,15);
}
else
if(key==UP&&y>120)/*上方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y-=40;
fillellipse(x,y,15,15);
}
else
if(key==DOWN&&y<400)/*下方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y+=40;
fillellipse(x,y,15,15);
}
}
if(key==ESC)/*結束游戲*/
break;
if((score1+score2)==64||score1==0||score2==0)/*格子已經占滿或一方棋子為0判斷勝負*/
{
playWin();/*輸出最後結果*/
break;
}
t=t%2+1; /*一方走後,改變棋子顏色即輪對方走*/
cc=0; /*計數值恢復為0*/
} /*endwhile*/
}
void SetPlayColor(int t)/*設置棋子顏色*/
{
if(t%2==1)
setfillstyle(SOLID_FILL,15);/*白色*/
else
setfillstyle(SOLID_FILL,8);/*灰色*/
}
void MoveColor(int x,int y)/*走了一步後恢復原來格子的狀態*/
{
if(y<100)/*如果是從起點出發就恢復藍色*/
setfillstyle(SOLID_FILL,BLUE);
else/*其他情況如果是1就恢復白色棋子,2恢復黑色棋子,或恢復藍色棋盤*/
switch(a[(x-120)/40][(y-120)/40])
{
case 1:
setfillstyle(SOLID_FILL,15);break; /*白色*/
case 2:
setfillstyle(SOLID_FILL,8);break; /*黑色*/
default:
setfillstyle(SOLID_FILL,BLUE); /*藍色*/
}
}
int QpChange(int x,int y,int t)/*判斷棋盤的變化*/
{
int i,j,k,kk,ii,jj,yes;
yes=0;
i=(x-120)/40; /*計算數組元素的行下標*/
j=(y-120)/40; /*計算數組元素的列下標*/
SetPlayColor(t);/*設置棋子變化的顏色*/
/*開始往8個方向判斷變化*/
if(j<6)/*往右邊*/
{
for(k=j+1;k<8;k++)
if(a[i][k]==a[i][j]||a[i][k]==0)/*遇到自己的棋子或空格結束*/
break;
if(a[i][k]!=0&&k<8)
{
for(kk=j+1;kk<k&&k<8;kk++)/*判斷右邊*/
{
a[i][kk]=a[i][j]; /*改變棋子顏色*/
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j+1) /*條件成立則有棋子改變過顏色*/
yes=1;
}
}
if(j>1)/*判斷左邊*/
{
for(k=j-1;k>=0;k--)
if(a[i][k]==a[i][j]||!a[i][k])
break;
if(a[i][k]!=0&&k>=0)
{
for(kk=j-1;kk>k&&k>=0;kk--)
{
a[i][kk]=a[i][j];
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j-1)
yes=1;
}
}
if(i<6)/*判斷下邊*/
{
for(k=i+1;k<8;k++)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k<8)
{
for(kk=i+1;kk<k&&k<8;kk++)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i+1)
yes=1;
}
}
if(i>1)/*判斷上邊*/
{
for(k=i-1;k>=0;k--)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k>=0)
{
for(kk=i-1;kk>k&&k>=0;kk--)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i-1)
yes=1;
}
}
if(i>1&&j<6)/*右上*/
{
for(k=i-1,kk=j+1;k>=0&&kk<8;k--,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]&&k>=0&&kk<8)
{
for(ii=i-1,jj=j+1;ii>k&&k>=0;ii--,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j>1)/*左下*/
{
for(k=i+1,kk=j-1;k<8&&kk>=0;k++,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k<8&&kk>=0)
{
for(ii=i+1,jj=j-1;ii<k&&k<8;ii++,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
if(i>1&&j>1)/*左上*/
{
for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k>=0&&kk>=0)
{
for(ii=i-1,jj=j-1;ii>k&&k>=0;ii--,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j<6)/* 右下*/
{
for(k=i+1,kk=j+1;kk<8&&kk<8;k++,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&kk<8&&k<8)
{
for(ii=i+1,jj=j+1;ii<k&&k<8;ii++,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
return yes;/*返回是否改變過棋子顏色的標記*/
}
void DoScore()/*處理分數*/
{
int i,j;
score1=score2=0;/*重新開始計分數*/
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(a[i][j]==1)/*分別統計兩個人的分數*/
score1++;
else
if(a[i][j]==2)
score2++;
}
void PrintScore(int playnum)/*輸出成績*/
{
if(playnum==1)/*清除以前的成績*/
{
setfillstyle(SOLID_FILL,BLUE);
bar(550,100,640,400);
}
setcolor(RED);
settextstyle(0,0,4);/*設置文本輸出樣式*/
if(playnum==1)/*判斷輸出哪個棋手的分,在不同的位置輸出*/
{
sprintf(playone,"%d",score1);
outtextxy(550,200,playone);
}
else
{
sprintf(playtwo,"%d",score2);
outtextxy(550,300,playtwo);
}
setcolor(0);
}
void playWin()/*輸出最後的勝利者結果*/
{
settextstyle(0,0,4);
setcolor(12);
if(score2>score1)/*開始判斷最後的結果*/
outtextxy(100,50,"black win!");
else
if(score2<score1)
outtextxy(100,50,"white win!");
else
outtextxy(60,50,"you all win!");
}

五子棋游戲
/*五子棋*/
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<bios.h>
#include<conio.h>

#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define SPACE 0x3920

#define BILI 20
#define JZ 4
#define JS 3
#define N 19

int box[N][N];
int step_x,step_y ;
int key ;
int flag=1 ;

void draw_box();
void draw_cicle(int x,int y,int color);
void change();
void judgewho(int x,int y);
void judgekey();
int judgeresult(int x,int y);
void attentoin();

void attention()
{
char ch ;
window(1,1,80,25);
textbackground(LIGHTBLUE);
textcolor(YELLOW);
clrscr();
gotoxy(15,2);
printf("游戲操作規則:");
gotoxy(15,4);
printf("Play Rules:");
gotoxy(15,6);
printf("1、按左右上下方向鍵移動棋子");
gotoxy(15,8);
printf("1. Press Left,Right,Up,Down Key to move Piece");
gotoxy(15,10);
printf("2、按空格確定落棋子");
gotoxy(15,12);
printf("2. Press Space to place the Piece");
gotoxy(15,14);
printf("3、禁止在棋盤外按空格");
gotoxy(15,16);
printf("3. DO NOT press Space outside of the chessboard");
gotoxy(15,18);
printf("你是否接受上述的游戲規則(Y/N)");
gotoxy(15,20);
printf("Do you accept the above Playing Rules? [Y/N]:");
while(1)
{
gotoxy(60,20);
ch=getche();
if(ch=='Y'||ch=='y')
break ;
else if(ch=='N'||ch=='n')
{
window(1,1,80,25);
textbackground(BLACK);
textcolor(LIGHTGRAY);
clrscr();
exit(0);
}
gotoxy(51,12);
printf(" ");
}
}
void draw_box()
{
int x1,x2,y1,y2 ;
setbkcolor(LIGHTBLUE);
setcolor(YELLOW);
gotoxy(7,2);
printf("Left, Right, Up, Down KEY to move, Space to put, ESC-quit.");
for(x1=1,y1=1,y2=18;x1<=18;x1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x1+JZ)*BILI,(y2+JS)*BILI);
for(x1=1,y1=1,x2=18;y1<=18;y1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x2+JZ)*BILI,(y1+JS)*BILI);
for(x1=1;x1<=18;x1++)
for(y1=1;y1<=18;y1++)
box[x1][y1]=0 ;
}

void draw_circle(int x,int y,int color)
{
setcolor(color);
setlinestyle(SOLID_LINE,0,1);
x=(x+JZ)*BILI ;
y=(y+JS)*BILI ;
circle(x,y,8);
}

void judgekey()
{
int i ;
int j ;
switch(key)
{
case LEFT :

if(step_x-1<0)
break ;
else
{
for(i=step_x-1,j=step_y;i>=1;i--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(i<1)break ;
step_x=i ;
judgewho(step_x,step_y);
break ;
}
case RIGHT :

if(step_x+1>18)
break ;
else
{
for(i=step_x+1,j=step_y;i<=18;i++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(i>18)break ;
step_x=i ;
judgewho(step_x,step_y);
break ;
}
case DOWN :

if((step_y+1)>18)
break ;
else
{
for(i=step_x,j=step_y+1;j<=18;j++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(j>18)break ;
step_y=j ;
judgewho(step_x,step_y);
break ;
}
case UP :

if((step_y-1)<0)
break ;
else
{
for(i=step_x,j=step_y-1;j>=1;j--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(j<1)break ;
step_y=j ;
judgewho(step_x,step_y);
break ;
}
case ESC :
break ;

case SPACE :
if(step_x>=1&&step_x<=18&&step_y>=1&&step_y<=18)
{
if(box[step_x][step_y]==0)
{
box[step_x][step_y]=flag ;
if(judgeresult(step_x,step_y)==1)
{
sound(1000);
delay(1000);
nosound();
gotoxy(30,4);
if(flag==1)
{
setbkcolor(BLUE);
cleardevice();
setviewport(100,100,540,380,1);
/*定義一個圖形窗口*/
setfillstyle(1,2);
/*綠色以實填充*/
setcolor(YELLOW);
rectangle(0,0,439,279);
floodfill(50,50,14);
setcolor(12);
settextstyle(1,0,5);
/*三重筆劃字體, 水平放?5倍*/
outtextxy(20,20,"The White Win !");
setcolor(15);
settextstyle(3,0,5);
/*無襯筆劃字體, 水平放大5倍*/
outtextxy(120,120,"The White Win !");
setcolor(14);
settextstyle(2,0,8);
getch();
closegraph();
exit(0);
}
if(flag==2)
{
setbkcolor(BLUE);
cleardevice();
setviewport(100,100,540,380,1);
/*定義一個圖形窗口*/
setfillstyle(1,2);
/*綠色以實填充*/
setcolor(YELLOW);
rectangle(0,0,439,279);
floodfill(50,50,14);
setcolor(12);
settextstyle(1,0,8);
/*三重筆劃字體, 水平放大8倍*/
outtextxy(20,20,"The Red Win !");
setcolor(15);
settextstyle(3,0,5);
/*無襯筆劃字體, 水平放大5倍*/
outtextxy(120,120,"The Red Win !");
setcolor(14);
settextstyle(2,0,8);
getch();
closegraph();
exit(0);
}
}
change();
break ;
}
}
else
break ;
}
}

void change()
{
if(flag==1)
flag=2 ;
else
flag=1 ;
}

void judgewho(int x,int y)
{
if(flag==1)
draw_circle(x,y,15);
if(flag==2)
draw_circle(x,y,4);
}

int judgeresult(int x,int y)
{
int j,k,n1,n2 ;
while(1)
{
n1=0 ;
n2=0 ;
/*水平向左數*/
for(j=x,k=y;j>=1;j--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*水平向右數*/
for(j=x,k=y;j<=18;j++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}

/*垂直向上數*/
n1=0 ;
n2=0 ;
for(j=x,k=y;k>=1;k--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*垂直向下數*/
for(j=x,k=y;k<=18;k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}

/*向左上方數*/
n1=0 ;
n2=0 ;
for(j=x,k=y;j>=1,k>=1;j--,k--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*向右下方數*/
for(j=x,k=y;j<=18,k<=18;j++,k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}

/*向右上方數*/
n1=0 ;
n2=0 ;
for(j=x,k=y;j<=18,k>=1;j++,k--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*向左下方數*/
for(j=x,k=y;j>=1,k<=18;j--,k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}
return(0);
break ;
}
}

void main()
{
int gdriver=VGA,gmode=VGAHI;
clrscr();
attention();
initgraph(&gdriver,&gmode,"c:\\tc");
/* setwritemode(XOR_PUT);*/
flag=1 ;
draw_box();
do
{
step_x=0 ;
step_y=0 ;
/*draw_circle(step_x,step_y,8); */
judgewho(step_x-1,step_y-1);
do
{
while(bioskey(1)==0);
key=bioskey(0);
judgekey();
}
while(key!=SPACE&&key!=ESC);
}
while(key!=ESC);
closegraph();
}

J. 求一個c++網球比賽計分程序的 隨機分組 的代碼 從32個人當中隨機分成四組 求!!!!!

#include<iostream>
#include<deque>
#include<vector>
#include<stdlib.h>

usingnamespacestd;

#defineTeamCount4
#defineNumberCount8

voidRandGroup(deque<int>&deqSrc,vector<vector<int>>&rVecDec)
{
srand(time(NULL));
intnSrcSize=deqSrc.size();
vector<int>vecSample;
for(inti=0;i<nSrcSize;i++)
{
//取隊員
intnPlayer=deqSrc.front();
deqSrc.pop_front();

vecSample.clear();
//獲取不滿NumberCount的隊伍集合
for(intj=0;j虧派<TeamCount;j++)
{
if(rVecDec[j].size()<NumberCount)
vecSample.push_back(j);
}
//獲取該隊員的隨機隊列編號
intnTmp=銷兄賀rand();
nTmp=rand()%(vecSample.size());
rVecDec[vecSample[nTmp]].push_back(nPlayer);
}
}

intmain()
{
//生成32個隊員
deque<int>deq;
for(inti=0;i<TeamCount*NumberCount;i++)
{
intnPlayer=0;
cin>>塵慶nPlayer;
deq.push_back(nPlayer);
}
//隨機分組
vector<vector<int>>vecRes;
for(inti=0;i<TeamCount;i++)
{
vector<int>vecTmp;
vecRes.push_back(vecTmp);
}
RandGroup(deq,vecRes);

return0;
}



朋友,請【採納答案】,您的採納是我答題的動力,如果沒有明白,請追問。謝謝。

閱讀全文

與隨機分組小程序源碼相關的資料

熱點內容
軟通動力程序員節2021 瀏覽:845
安卓系統如何卸載安裝包 瀏覽:870
簡訊刪除助手文件夾 瀏覽:688
java辦公自動化 瀏覽:340
php中超鏈接 瀏覽:253
linux默認路由設置 瀏覽:36
linux如何掛載iso 瀏覽:432
vs程序換文件夾後不能編譯 瀏覽:557
安卓源碼編譯輸入腳本沒反應 瀏覽:47
phpmysql自增 瀏覽:167
把ppt保存為pdf 瀏覽:533
汽車密封件加密配件 瀏覽:887
黑馬程序員15天基礎班 瀏覽:560
java調整格式 瀏覽:521
香港雲伺服器租用價 瀏覽:78
linuxsublime3 瀏覽:560
imac混合硬碟命令 瀏覽:279
沈陽用什麼app租房車 瀏覽:858
00後高中生都用什麼app 瀏覽:239
戴爾塔式伺服器怎麼打開獨立顯卡 瀏覽:808