日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 1013 Counterfeit Dollar 称硬币

發布時間:2023/12/13 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 1013 Counterfeit Dollar 称硬币 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

12個硬幣,有一個假的 或輕或重,找出假硬幣

開始用的模擬,考慮很多情況

后來,lmy說輕的-1,重的+1,學數學的看什么都是數字,orz

模擬寫的兩個差不多的代碼:

(一)

#include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; int vs[15],cnt[15]; int main() {int CASE,ca,len,i,x,sum;char s1[15],s2[15],s3[8];scanf("%d",&CASE);while(CASE--){sum=0;memset(vs,0,sizeof(vs));memset(cnt,0,sizeof(cnt));for(ca=1;ca<=3;ca++){scanf("%s%s%s",s1,s2,s3);len=strlen(s1);if(s3[0]=='e')//一定是真的{for(i=0;i<len;i++){x=s1[i]-'A';vs[x]=1;x=s2[i]-'A';vs[x]=1;}}if(s3[0]=='u')//前重后輕{sum++;len=strlen(s1);for(i=0;i<len;i++){x=s1[i]-'A';if(!vs[x]){vs[x]=3;cnt[x]=1;}else if(vs[x]==2)vs[x]=1;else if(vs[x]==3)cnt[x]++;x=s2[i]-'A';if(!vs[x]){vs[x]=2;cnt[x]=1;}else if(vs[x]==3)vs[x]=1;else if(vs[x]==2)cnt[x]++;}}if(s3[0]=='d')//前輕后重{sum++;len=strlen(s1);for(i=0;i<len;i++){x=s1[i]-'A';if(!vs[x]){vs[x]=2;cnt[x]++;}else if(vs[x]==3)vs[x]=1;else if(vs[x]==2)cnt[x]++;x=s2[i]-'A';if(!vs[x]){vs[x]=3;cnt[x]++;}else if(vs[x]==2)vs[x]=1;else if(vs[x]==3)cnt[x]++;}}}int mark;for(i=0;i<12;i++){if(vs[i]!=1&&cnt[i]==sum)//vs不能去掉{mark=i;break;}}if(vs[mark]==3)printf("%c is the counterfeit coin and it is heavy.\n",mark+'A');if(vs[mark]==2)printf("%c is the counterfeit coin and it is light.\n",mark+'A');}return 0; }

  

  (二)

#include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; int vs[15],cnt[15]; int main() {int CASE,ca,len,i,x,sum;char s1[15],s2[15],s3[8];scanf("%d",&CASE);while(CASE--){sum=0;memset(vs,0,sizeof(vs));memset(cnt,0,sizeof(cnt));for(ca=1;ca<=3;ca++){scanf("%s%s%s",s1,s2,s3);len=strlen(s1);if(s3[0]=='e')//一定是真的{for(i=0;i<len;i++){x=s1[i]-'A';vs[x]=1,cnt[x]=0;//必須寫上cnt[x]=0,因為可能第二組或第三組x=s2[i]-'A';vs[x]=1,cnt[x]=0;}}if(s3[0]=='u')//前重后輕{sum++;len=strlen(s1);for(i=0;i<len;i++){x=s1[i]-'A';if(!vs[x]){vs[x]=3;cnt[x]=1;}else if(vs[x]==2){vs[x]=1;cnt[x]=0;}else if(vs[x]==3)cnt[x]++;x=s2[i]-'A';if(!vs[x]){vs[x]=2;cnt[x]=1;}else if(vs[x]==3){vs[x]=1;cnt[x]=0;}else if(vs[x]==2)cnt[x]++;}}if(s3[0]=='d')//前輕后重{sum++;len=strlen(s1);for(i=0;i<len;i++){x=s1[i]-'A';if(!vs[x]){vs[x]=2;cnt[x]++;}else if(vs[x]==3){vs[x]=1;cnt[x]=0;}else if(vs[x]==2)cnt[x]++;x=s2[i]-'A';if(!vs[x]){vs[x]=3;cnt[x]++;}else if(vs[x]==2){vs[x]=1;cnt[x]=0;}else if(vs[x]==3)cnt[x]++;}}}int mark;for(i=0;i<12;i++){if(cnt[i]==sum)//vs[i]!=1去掉了{mark=i;break;}}if(vs[mark]==3)printf("%c is the counterfeit coin and it is heavy.\n",mark+'A');if(vs[mark]==2)printf("%c is the counterfeit coin and it is light.\n",mark+'A');}return 0; }

  (三)轉成數學加減后的簡單代碼!!!

#include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; int cnt[15]; bool vs[15]; int main() {int CASE,ca,len,i,x,sum;char s1[15],s2[15],s3[8];scanf("%d",&CASE);while(CASE--){sum=0;memset(cnt,0,sizeof(cnt));memset(vs,0,sizeof(vs));for(ca=1;ca<=3;ca++){scanf("%s%s%s",s1,s2,s3);len=strlen(s1);if(s3[0]=='u')//前重后輕{sum++;for(i=0;i<len;i++){x=s1[i]-'A';if(!vs[x])cnt[x]++;}for(i=0;i<len;i++){x=s2[i]-'A';if(!vs[x])cnt[x]--;}}if(s3[0]=='d')//前輕后重{sum++;for(i=0;i<len;i++){x=s1[i]-'A';if(!vs[x])cnt[x]--;}for(i=0;i<len;i++){x=s2[i]-'A';if(!vs[x])cnt[x]++;}}if(s3[0]=='e')//相等{for(i=0;i<len;i++){x=s1[i]-'A';cnt[x]=0;vs[x]=1;}for(i=0;i<len;i++){x=s2[i]-'A';vs[x]=1;cnt[x]=0;}}}int mark;for(i=0;i<12;i++){if(!vs[i])if(cnt[i]==sum||cnt[i]==-sum){mark=i;break;}}if(cnt[mark]>0)printf("%c is the counterfeit coin and it is heavy.\n",mark+'A');if(cnt[mark]<0)printf("%c is the counterfeit coin and it is light.\n",mark+'A');}return 0; }

  

轉載于:https://www.cnblogs.com/sdau10kuaile/archive/2012/04/14/2446956.html

總結

以上是生活随笔為你收集整理的POJ 1013 Counterfeit Dollar 称硬币的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。