代码
#include <iostream>
using namespace std;
int main() {
int T, K;
cin>>T>>K;
for(int i=0; i<K; i++) {
int n1, b, t, n2;
cin>>n1>>b>>t>>n2;
if(T<t) {
cout<<"Not enough tokens. Total = "<<T<<"."<<endl;
} else {
if( (b==0 && n1>n2) || (b==1 && n1<n2)) {
T += t;
cout<<"Win "<<t<<"! Total = "<<T<<"."<<endl;
} else {
T -= t;
cout<<"Lose "<<t<<". Total = "<<T<<"."<<endl;
}
if(T==0){
cout<<"Game Over."<<endl;
break;
}
}
}
return 0;
}
注解
准确分析筹码不足,Game over,以及输赢的逻辑顺序是关键。
在下注后,首先判断筹码是否足?不足就输出筹码不足,但题意没有要求退出,因此可继续下一轮循环。否则,就判断下注是否正确,如正确增加筹码,不正确减少筹码。此时该轮结束,判断是否还剩筹码,如有,可继续下一轮,否则游戏结束。