对于这种小问题,我使用的是看管表,而不是逻辑表。 缩略语
#include <iostream>
#include <array>
constexpr std::array<const char*,3> names = {"Rock","Paper","Scissors"};
constexpr std::array<const char*,3> outcomes = {"Draw","win","Lose"};
constexpr std::array<std::array<int,3>,3> results = {{{0,2,1},{1,0,2},{2,1,0}}};
int main() {
for ( int j=0; j<3; ++j ) {
for ( int k=0; k<3; ++k ) {
int result = results[j][k];
std::cout << names[j] << " x " << names[k] << ": " << outcomes[result] << std::endl;
}
}
}
印刷
Rock x Rock: Draw
Rock x Paper: Lose
Rock x Scissors: win
Paper x Rock: win
Paper x Paper: Draw
Paper x Scissors: Lose
Scissors x Rock: Lose
Scissors x Paper: win
Scissors x Scissors: Draw
Godbolt: https://godbolt.org/z/Gxa5vKsGv