首页 > 学院 > 开发设计 > 正文

POJ1013称硬币

2019-11-08 19:37:40
字体:
来源:转载
供稿:网友

问题描述:如下图

程序代码:/* *问题描述:POJ1013称硬币 */ #include <iostream> #include <cstring> using namespace std; char Left[3][7];//天平左边硬币 char Right[3][7];//天平右边硬币 char result[3][7];//存储结果 bool IsFake(char c,bool light) {//light为真表示假设假币为轻,light为假表示假币为重     for(int i = 0; i<3; ++i)//三次称量结果,循环3次     {         char *pLeft,*PRight;//指向天平两边的字符串         if(light)         {             pLeft = Left[i];             pRight = Right[i];         }         else//如果假设假币是重的,则把称量结果左右对换         {             pLeft = Right[i];             pRight = Left[i];         }         switch(result[i][0])         {         case 'u':            if(strchr(pRight,c)==NULL)                return false;            break;         case 'e':            if(strchr(pLeft,c)||strchr(pRight,c))                return false;            break;         case 'd':            if(strchr(pLeft,c)==NULL)                return false;            break;         }     }     return true; } int main() {     int t;//表示有几组数据     cin>>t;     while(t--)     {         for(int i = 0; i<3; ++i)            cin>> Left[i] >>Right[i] >>result[i];         for(char c= 'A';c<='L';c++)         {             if(IsFake(c,true))             {                 cout<<"Output:"<<endl;                 cout<<c<<" is the counterfeit coin and it is light./n";                 break;             }             else if (IsFake(c,false))             {                 cout<<"Output:"<<endl;                 cout<<c<<" is the counterfeit coin and it is heary./n";                 break;             }         }     }     return 0; }运行结果:


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表