首先是题目 T524105
早上出门买早餐,小琼的手机down掉了,没有办法使用移动支付。没辙,只能用纸币了。店家找给小琼一大堆硬币。但是小琼的疑心病犯了,一直怀疑店家找给他的有假币... ...
小琼共获得12枚硬币。其中有11枚真币和1枚假币。假币看起来和真币没有区别,但是重量不同。但小琼不知道假币比真币轻还是重。于是他向朋友借了一架天平,希望称三次就能找出假币并且确定假币是轻是重。例如:如果小琼用天平称两枚硬币,发现天平平衡,说明两枚都是真的。如果小琼用一枚真币与另一枚银币比较,发现它比真币轻或重,说明它是假币。经过精心安排每次的称量,保证小琼在称三次后确定假币。
第一行有一个数字 N,表示有 N 组测试样例。
对于每组测试用例:
输入有三行,每行表示一次称量的结果。小琼事先将硬币标号为 A−L ,每次称量的结果用三个以空格隔开的字符串表示:天平左边放置的硬币 天平右边放置的硬币 平衡状态。其中平衡状态用up, down, 或 even表示, 分别为右端高、右端低和平衡。天平左右的硬币数总是相等的。
输出哪一个标号的硬币是假币,并说明它比真币轻还是重(heavy or light)。
1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
K is the counterfeit coin and it is light.
然后是我的代码: #include<bits/stdc++.h> using namespace std;
int main(){ int N; cin>>N; cin.ignore(); int coin[12]={10,10,10,10,10,10,10,10,10,10,10,10}; int coin1[12]={0,0,0,0,0,0,0,0,0,0,0,0}; char coins[18]; int xf=0,xy=0,xy1=0; int f=0; for(int i=0;i<N*3;i++){
cin.getline(coins,19);//输入左边硬币、右边硬币、结果
for(f=0;f<19;f++){//确定结果位置
if(coins[f]==' '&&xf==1){
xy=f;//第二个空格位置
}
else if(coins[f]==' '){
xf=1;
xy1=f;//第一个空格位置
}
}
xf=0;
if(coins[xy+1]=='e'){//平衡
for(int j=0;j<xy1;j++){
coin[int(coins[j])-65]=0;
}
for(int j=xy1+1;j<xy;j++){
coin[int(coins[j])-65]=0;
}
}
else if(coins[xy+1]=='u'){//左重右轻
for(int j=0;j<xy1;j++){//重
if(coin[int(coins[j])-65]==0){//已被标记为真币
continue;
}
if(coin[int(coins[j])-65]==-1){//已被怀疑为轻币,确认为真币
coin[int(coins[j])-65]=0;
continue;
}
coin1[int(coins[j])-65]=1;//出现过的硬币标记
coin[int(coins[j])-65]=1;//被怀疑为重币的
}
for(int j=xy1+1;j<xy;j++){//轻
if(coin[int(coins[j])-65]==0){//已被标记为真币
continue;
}
if(coin[int(coins[j])-65]==1){//已被怀疑为重币,确认为真币
coin[int(coins[j])-65]=0;
continue;
}
coin1[int(coins[j])-65]=1;//出现过的硬币标记
coin[int(coins[j])-65]=-1;//被怀疑为轻币的
}
for(f=0;f<12;f++){
if(coin1[f]==0){//未出现过的硬币
coin[f]=0;//未出现在一轻一重情况下的硬币为真
}
coin1[f]=0;//归零,重复
}
}
else if(coins[xy+1]=='d'){//左轻右重
for(int j=0;j<xy1;j++){//轻
if(coin[int(coins[j])-65]==0){//已被标记为真币
continue;
}
if(coin[int(coins[j])-65]==1){//已被怀疑为重币,确认为真币
coin[int(coins[j])-65]=0;
continue;
}
coin1[int(coins[j])-65]=1;//出现过的硬币标记
coin[int(coins[j])-65]=-1;//被怀疑为轻币的
}
for(int j=xy1+1;j<xy;j++){//重
if(coin[int(coins[j])-65]==0){//已被标记为真币
continue;
}
if(coin[int(coins[j])-65]==-1){//已被怀疑为轻币,确认为真币
coin[int(coins[j])-65]=0;
continue;
}
coin1[int(coins[j])-65]=1;//出现过的硬币标记
coin[int(coins[j])-65]=1;//被怀疑为重币的
}
for(f=0;f<12;f++){
if(coin1[f]==0){//未出现过的硬币
coin[f]=0;//未出现在一轻一重情况下的硬币为真
}
coin1[f]=0;//归零,重复
}
}
// for(f=0;f<12;f++){cout<<coin[f]<<endl;} } for(int k=0;k<12;k++) { if(coin[k]==-1){ cout<<char(k+65)<<" is the counterfeit coin and it is light.";return 0; }
if(coin[k]==1){
cout<<char(k+65)<<" is the counterfeit coin and it is heavy.";return 0;
// K is the counterfeit coin and it is light. }
}
return 0;
} 对于给出的例子可以完美输出,但是一直是WA,找不到原因,个人感觉好像是没问题的,求解