最終更新:ID:n2jS2H7OsQ 2014年01月12日(日) 06:21:30履歴
/* 王様ゲーム用番号割り当てプログラム */ #include <stdio.h>//標準ヘッダ #include <stdlib.h>//乱数使用のために使用 #include <time.h>//時計を使用するために使用 #include <windows.h>//タイトルバーを変更するために使用 //メインプログラム int main(void) { SetConsoleTitle("王様ゲーム番号割り当てプログラム");//タイトルバー int i,j,x,flag,temp;//iとj=ループ用,x=要素数,flag=続行か否か,temp=緊急避難 int ran[101],player[101];//ran=乱数格納用,player=プレイヤーナンバー格納用 char name[101][256];//プレイヤーネームの格納場所、とりあえず100人まで printf("プレイ人数を入力してください「100人まで」:"); scanf("%d",&x);//プレイヤー人数の入力 if(x < 101 && x > 0){} else{ printf("値が不正です。プログラムを終了します。"); exit(EXIT_FAILURE); } //名前入力 printf("プレイヤーの名前を入力します。\n"); for(i=0;i < x ;i++) { printf("プレイヤーナンバー%dの名前を入力してください:",i+1); scanf("%s",name[i]); } flag=1;//続行か否かのフラグ while(1){//続行の場合のループ printf("\n\n\n番号を割り振ります\n\n"); srand((unsigned)time(NULL));//現在時刻をSEEDにして異なる乱数の生成 //乱数生成のループ for(i=0;i < x ;i++) { ran[i] = rand();//乱数の格納 player[i] = i+1;//プレイヤーナンバーの格納 } //乱数の生成ここまで //バブルソートその1 for (i = 0; i < x ; i++) { for (j = x ; j > i; j--) { if (ran[j - 1] > ran[j]) { //乱数の交換 temp = ran[j]; ran[j]= ran[j - 1]; ran[j - 1]= temp; //プレイヤーナンバーの交換 temp = player[j]; player[j] = player[j - 1]; player[j - 1]= temp; } } } //乱数の簡略化 for(i=0;i < x+1 ; i++){ ran[i]=i; } //バブルソートその2 for (i = 0; i < x; i++) { for (j = x ; j > i; j--) { if (player[j - 1] > player[j]) { //乱数の交換 temp = ran[j]; ran[j]= ran[j - 1]; ran[j - 1]= temp; //プレイヤーナンバーの交換 temp = player[j]; player[j] = player[j - 1]; player[j - 1]= temp; } } } //内容の表示 for(i=0;i < x;i++){ printf("%d %s:",player[i],name[i]);//プレイヤー番号と名前 printf("%d \n",ran[i]);//生成した乱数 } //続けるかどうかのフラグ printf("\n続けますか?\n\nはい:1\nいいえ:0\n"); printf("1か0を入力してください:"); scanf("%d",&flag); if(flag==0) { break; } else if(flag==1){} else{ printf("値が不正です。プログラムを終了します。"); exit(EXIT_FAILURE); } } return 0; }
コメントをかく