自學C 四月十八日_乾貨_微元演算法

Bite_magican發表於2019-04-18

//5位運動員參加了10米臺跳水比賽,有人讓他們預測比賽結果
//A選手說:B第二,我第三;
//B選手說:我第二,E第四;
//C選手說:我第一,D第二;
//D選手說:C最後,我第三;
//E選手說:我第四,A第一;
//比賽結束後,每位選手都說對了一半,請程式設計確定比賽的名次。
#include<stdio.h>
#include<windows.h>
int checkRank(int * player, int n){
int i, res = 0;
for (i = 0; i < n; i++){
res |= 1 << player[i];
}
return res == 0x3e;
}
int main(){
int player[5] = { 0 };
for (player[0] = 1; player[0] <= 5; player[0]++){
for (player[1] = 1; player[1] <= 5; player[1]++){
for (player[2] = 1; player[2] <= 5; player[2]++){
for (player[3] = 1; player[3] <= 5; player[3]++){
for (player[4] = 1; player[4] <= 5; player[4]++)
if ((player[0] == 3) + (player[1] == 2) == 1 &&
(player[1] == 2) + (player[4] == 4) == 1 &&
(player[2] == 1) + (player[3] == 2) == 1 &&
(player[2] == 5) + (player[3] == 3) == 1 &&
(player[4] == 4) + (player[0] == 1) == 1 &&
checkRank(player, 5)){
printf(
“a是第%d\n”
“b是第%d\n”
“c是第%d\n”
“d是第%d\n”
“e是第%d\n”,
player[0], player[1], player[2], player[3], player[4]);
}

			}
		}
	}
}
system ("pause");
return 0;

}

相關文章