枚举 PROBLEM2 银币称量问题

/*some coins weigh 3 times can  identify which one is conterfeitcounterfeit. */
#include <cstdlib>
#include <iostream>


using namespace std;
/**store the input*/
char left1[3][7];   //用数组的时候记得给数组确定范围 
char right1[3][7];
char result1[3][6]; 
bool isheavy(char);   
bool islight(char);

int main()
{
    int n=0;
    scanf("%d",&n);
    while(n>0)
    {
        for(int i=0;i<3;i++)
        {
               cin>>left1[i]>>right1[i]>>result1[i];
        }
        /**numerate all coins and judge if it is light or heavy*/
        for(char c='a';c<='l';c++)   
        {
                 if(isheavy(c))
                 {
                               printf("%c is the counterfeit and it is heavy",c);
                               break;
                 }
                 if(islight(c))
                 {
                               printf("%c is the counterfeit and it is light",c);
                               break;
                 }
        }
        n--;   
    }   
    system("PAUSE");
    return EXIT_SUCCESS;
}
/**judge if the coin x satisfy the three weighings*/
bool isheavy(char x)
{
     for(int i=0;i<3;i++)
     {
             switch(result1[i][0])
             {
                  //if the coin x not exists in the down side, coin x is not the heavy, couterfeit one 
                  case 'd': if(strchr(right1[i],x)==NULL)  return false;
                  break;
                  case 'u': if(strchr(left1[i],x)==NULL)  return false;
                  break;
                  case 'e': if(strchr(left1[i],x)!=NULL||strchr(right1[i],x)!=NULL)  return false;
                  break;
             }
     }
     return true;
}

bool islight(char x)
{
     for(int i=0;i<3;i++)
     {
             switch(result1[i][0])
             {
                  case 'd' : if(strchr(left1[i],x)==NULL)  return false;
                  break;
                  case 'u' : if(strchr(right1[i],x)==NULL) return false;
                  break;
                  case 'e' : if(strchr(left1[i],x)!=NULL||strchr(right1[i],x)!=NULL) return false;
                  break;
             }
     }
     return true;
}
 
这次在字符串输入上花了很多时间。以后要注意c中对数组要给定其大小。
string a[0]形式的字符串数组需要用cin>>输入
char* a;  cin>>a  是错误的形式,因为a是一个指针。 
char* a; 用来定义动态数组   
a=new char[5];  cin>>a;
posted @ 2010-04-17 20:05  love && peace  阅读(132)  评论(0编辑  收藏  举报