poj1013——Counterfeit Dollar

本文介绍了一种通过三次称重来判断12枚金币中唯一一枚假币的方法,并确定该假币是轻还是重。利用C++实现算法逻辑,通过不同称重结果(平衡、左盘重、右盘重)来标记每枚金币的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:逐一判断每个金币是否为真。

1,当天平为oven 时,里面的金币都为真;

2,天平不是oven时,假的金币在天平了,不在里面的都为真(没想到这点~~看报告所得!),同时将天平里的金币标记。分为轻和重。

3,当一个金币既是轻,又是重时,是真的。

输出为假的金币。

#include<iostream> #include<cstdio> #include<cstring> using namespace std; bool vis[14]; class tt { public: int t; bool light,weight; tt () { t=-1; light=false ; weight=false; } }; char up[7]="up",down[7]="down",over[7]="even"; class node { public: char a[7],b[7],c[7]; }; node st[4]; int main() { int i,j,n,l1,l2,k; cin>>n; char ch; while(n--) { tt co[14]; for( i=0;i<3;i++) { scanf("%s%s%s",&st[i].a,&st[i].b,&st[i].c ); if(strcmp(st[i].c ,over)==0) { l1=strlen(st[i].a ); for(j=0;j<l1;j++) co[st[i].a [j]-'A'].t =0; l1=strlen(st[i].b ); for(j=0;j<l1;j++) co[st[i].b [j]-'A'].t =0; } else if(strcmp(st[i].c ,up)==0) { memset(vis,false ,sizeof(vis)); l1=strlen(st[i].a ); for(j=0;j<l1;j++) { k=st[i].a[j]-'A'; vis[k]=true; if(co[k].t ==-1) { co[k].weight =true; } } l1=strlen(st[i].b ); for(j=0;j<l1;j++) { k=st[i].b [j]-'A'; vis[k]=true; if(co[k].t ==-1) { co[k].light =true; } } for(j=0;j<12;j++) if(!vis[j]) co[j].t=0; } else { memset(vis,false ,sizeof(vis)); l1=strlen(st[i].a ); for(j=0;j<l1;j++) { k=st[i].a[j]-'A'; vis[k]=true; if(co[k].t ==-1) { co[k].light =true; } } l1=strlen(st[i].b ); for(j=0;j<l1;j++) { k=st[i].b [j]-'A'; vis[k]=true; if(co[k].t ==-1) { co[k].weight =true; } } for(j=0;j<12;j++) if(!vis[j]) co[j].t=0; } } for(i=0;i<12;i++) { if(co[i].light &&co[i].weight ) { co[i].t =0; } if(co[i].t ==-1) { if(co[i].light ) printf("%c is the counterfeit coin and it is light.\n",i+'A'); else printf("%c is the counterfeit coin and it is heavy.\n",i+'A'); break; } } } return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值