Last active
August 29, 2015 14:11
-
-
Save kenornotes/50186603dfdf843ac6df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
//判斷 x 是否為質數 | |
int isPrime(int x) { | |
int i; | |
for (i = 2; i < x; i++) { | |
//出現 1 及 x 本身以外的因數,所以不是質數 | |
if ( x % i == 0 ) | |
return 0; | |
} | |
//全部檢查完沒有 1 及 x 本身以外的因數,是質數 | |
return 1; | |
} | |
//產生質數的亂數 | |
int generatePrimeRand() { | |
srand(time(NULL)); | |
int tmp; | |
while (1) { | |
//產生 1~100 的亂數 | |
tmp = (rand() % 100) + 1; | |
//亂數大於 1 且是質數就回傳,呃,因為 1 不是質數 | |
if (tmp > 1 && isPrime(tmp)) | |
return tmp; | |
} | |
} | |
//判定 x 是否存在 array 中 | |
int isExist(int x, int array[], int total) { | |
int i; | |
for (i = 0; i < total; i++) { | |
//存在array中 | |
if (x == array[i]) | |
return 1; | |
} | |
return 0; | |
} | |
//排序陣列 | |
void sort(int x[], int total) { | |
int i, j, min, tmp, minAt; | |
for (i = 0; i < total; i++) { | |
min = x[i]; | |
for (j = i; j < total; j++) { | |
if (x[j] < min) { | |
minAt = j; | |
min = x[j]; | |
} | |
} | |
tmp = x[minAt]; | |
x[minAt] = x[i]; | |
x[i] = tmp; | |
} | |
} | |
int main() { | |
int total, i, j, goodClass, brokenClass; | |
//輸入共需檢查幾間教室 | |
scanf("%d", &total); | |
int classid[total]; | |
//產生亂數質數陣列 | |
for (i = 0; i < total; i++) { | |
int tempRand; | |
while (1) { | |
// 產生質數亂數 | |
tempRand = generatePrimeRand(); | |
// 判定這個亂數是否已經存在了 | |
if (!isExist(tempRand, classid, total)) { | |
classid[i] = tempRand; | |
break; | |
} | |
} | |
printf("%d ", classid[i]); | |
} | |
printf("\n"); | |
//輸入完好的教室數量 | |
scanf("%d", &goodClass); | |
int goodId[goodClass]; | |
//輸入完好教室的id | |
for (i = 0; i < goodClass; i++) { | |
int tmp; | |
scanf("%d", &tmp); | |
for (j = 0; j < total; j++) { | |
if ( tmp == classid[j]) | |
classid[j] = 0; | |
} | |
} | |
//排列教室編號 | |
sort(classid, total); | |
//印出損壞的教室 | |
for (i = 0 ; i < total; i++) { | |
if(classid[i] != 0) | |
printf("%d ", classid[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment