Description

Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins.         Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively.         By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.       

Input

The first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A--L. Information on a weighing will be given by two strings of letters and then one of the words ``up'', ``down'', or ``even''. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.       

Output

For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.       

Sample Input

1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even

Sample Output

K is the counterfeit coin and it is light. 

这道题如果找不到方法那么分好多种情况一种一种来分析很麻烦
开始写的代码就没有找到方法,将题目样例中的情况分析完结果还有好多种情况 以下代码只考虑了样例情况 分析第二种情况时 发现太麻烦了 就停下来了
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int t;
char f1(char* a,char* b,char* c,char* d,char* e,char* f) //a、b、c、d四个even,e、f为up||down
{
int n1=strlen(e),n2=strlen(f);
for(int i=;i<n1;i++){
if(strchr(a,e[i])==NULL&&strchr(b,e[i])==NULL&&strchr(c,e[i])==NULL&&strchr(d,e[i])==NULL){
t=;
return e[i];
}
}
for(int i=;i<n2;i++){
if(strchr(a,f[i])==NULL&&strchr(b,f[i])==NULL&&strchr(c,f[i])==NULL&&strchr(d,f[i])==NULL){
t=;
return f[i];
}
}
}
/*char f2(char* a,char* b,char* c,char* d,char* e,char* f) //a、b为even,其他为up||down
{ }*/
int main()
{
int n,i;
cin>>n; while(n--)
{
char str[][];
char k;
for(i=;i<;i++)cin>>str[i];
for(;i<;i++)cin>>str[i];
for(;i<;i++)cin>>str[i];
//for(i=0;i<9;i++)cout<<i<<" "<<str[i]<<endl;
if(str[][]=='e'&&str[][]=='e'&&(str[][]=='u'||str[][]=='d')){
k=f1(str[],str[],str[],str[],str[],str[]);
cout<<k<<" is the counterfeit coin and it is ";
if(str[][]=='u'){
if(t==)cout<<"heavy. ";
else cout<<"light. ";
}
else{
if(t==)cout<<"light. ";
else cout<<"heavy. ";
}
}
else if(str[][]='e'&&str[][]=='e'&&(str[][]=='u'||str[][]=='d')){
k=f1(str[],str[],str[],str[],str[],str[]);
cout<<k<<" is the counterfeit coin and it is ";
if(str[][]=='u'){
if(t==)cout<<"heavy. ";
else cout<<"light. ";
}
else{
if(t==)cout<<"light. ";
else cout<<"heavy. ";
}
}
else if(str[][]=='e'&&str[][]=='e'&&(str[][]=='u'||str[][]=='d')){
k=f1(str[],str[],str[],str[],str[],str[]);
cout<<k<<" is the counterfeit coin and it is ";
if(str[][]=='u'){
if(t==)cout<<"heavy. ";
else cout<<"light. ";
}
else{
if(t==)cout<<"light. ";
else cout<<"heavy. ";
}
}
/* else if(str[2][0]=='e'&&str[5][0]!='e'&&str[8][0]!='e'){
}
else if(str[5][0]=='e'&&str[2][0]!='e'&&str[8][0]!='e'){
}
else if(str[8][0]=='e'&&str[5][0]!='e'&&str[2][0]!='e'){
}
*/
}
//system("pause");
return ;
}

找找方法  换一种简单的方法来做

给所有银币赋值1

从A开始给银币赋值0或 2   天平两端相加判断是否符合even up down的条件 若符合 则得出结果

#include<iostream>
#include<string>
using namespace std;
int s[];
int f(string a,string b,int n)
{
int p=,q=;
for(int j=;j<n;j++){
p+=s[a[j]-'A'];
q+=s[b[j]-'A'];
}
if(p==q)return ;
if(p>q)return ;
else return -;
}
int main()
{
int n,i;
cin>>n;
while(n--)
{
for(i=;i<;i++)s[i]=;
string str[];
int k;
bool flag=false;
for(i=;i<;i++)cin>>str[i];
for(;i<;i++)cin>>str[i];
for(;i<;i++)cin>>str[i];
int n1=str[].length(),n2=str[].length(),n3=str[].length();
for(i=;i<;i++){
s[i]=;
k=f(str[],str[],n1);
if(!((k==&&str[][]=='e')||(k==&&str[][]=='u')||(k==-&&str[][]=='d'))){
s[i]=;
continue;
}
k=f(str[],str[],n2);
if(!((k==&&str[][]=='e')||(k==&&str[][]=='u')||(k==-&&str[][]=='d'))){
s[i]=;
continue;
}
k=f(str[],str[],n3);
if((k==&&str[][]=='e')||(k==&&str[][]=='u')||(k==-&&str[][]=='d')){
flag=true;
cout<<(char)(i+'A')<<" is the counterfeit coin and it is light. "<<endl;
}
s[i]=;
}
if(!flag){
for(i=;i<;i++){
s[i]=;
k=f(str[],str[],n1);
if(!((k==&&str[][]=='e')||(k==&&str[][]=='u')||(k==-&&str[][]=='d'))){
s[i]=;
continue;
}
k=f(str[],str[],n2);
if(!((k==&&str[][]=='e')||(k==&&str[][]=='u')||(k==-&&str[][]=='d'))){
s[i]=;
continue;
}
k=f(str[],str[],n3);
if((k==&&str[][]=='e')||(k==&&str[][]=='u')||(k==-&&str[][]=='d')){
cout<<(char)(i+'A')<<" is the counterfeit coin and it is heavy. "<<endl;
}
s[i]=;
}
}
}
//system("pause");
return ;
}

D - Counterfeit Dollar(第二季水)的更多相关文章

  1. F - The Fun Number System(第二季水)

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  2. S - 骨牌铺方格(第二季水)

    Description          在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.         例如n=3时,为2× 3方格,骨牌的铺放方案有三种, ...

  3. R - 一只小蜜蜂...(第二季水)

    Description          有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数.         其中,蜂房的结构如下所示.     ...

  4. I - Long Distance Racing(第二季水)

    Description Bessie is training for her next race by running on a path that includes hills so that sh ...

  5. Y - Design T-Shirt(第二季水)

    Description Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA ...

  6. N - Robot Motion(第二季水)

    Description A robot has been programmed to follow the instructions in its path. Instructions for the ...

  7. B - Maya Calendar(第二季水)

    Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old ...

  8. T - 阿牛的EOF牛肉串(第二季水)

    Description          今年的ACM暑期集训队一共有18人,分为6支队伍.其中有一个叫做EOF的队伍,由04级的阿牛.XC以及05级的COY组成.在共同的集训生活中,大家建立了深厚的 ...

  9. E - Number Sequence(第二季水)

    Description A single positive integer i is given. Write a program to find the digit located in the p ...

随机推荐

  1. c_str()函数

    #include <string.h> const char *c_str(); 返回字符串地址,是一个c函数,返回类型const char*c_str()函数返回一个指向正规C字符串的指 ...

  2. 13年山东省赛 Mountain Subsequences(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mountain Subsequences Time Limit: 1 Sec   ...

  3. (原)Matlab的svmtrain和svmclassify

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5554551.html 参考网址: http://www.cnblogs.com/zhangchaoya ...

  4. VLC的相关文档以及javascript接口

    参看下面链接:VLC相关文档

  5. activiti笔记二:用户任务

    1, assignee 代替humanPerformer  功能 2, cadidateUsers代替potentialOwner功能 3, candidateGroups代替potentialOwn ...

  6. rub、sass和compass的安装

    长话短说,直接进入正题. [ruby安装] ruby下载网址:http://rubyinstaller.org/downloads/ 选择适合自己电脑的版本: 下载完成后直接双击安装,记得勾选加入环境 ...

  7. 2015年阿里巴巴蚂蚁金服校招JAVA研发工程师内推电话面试

    没想到阿里校招如此之早,虽然早已进入复习备战状态,但还是感觉有些措手不及...找了个在蚂蚁金服做HR的同学帮忙了内推,然后在最近的几天匆匆忙忙地复习JAVA(之前都把精力放在了数据结构.算法等基础上了 ...

  8. 数据挖掘(data mining),机器学习(machine learning),和人工智能(AI)的区别是什么? 数据科学(data science)和商业分析(business analytics)之间有什么关系?

    本来我以为不需要解释这个问题的,到底数据挖掘(data mining),机器学习(machine learning),和人工智能(AI)有什么区别,但是前几天因为有个学弟问我,我想了想发现我竟然也回答 ...

  9. C#基础之------进制转换

    /************************ File:控制台进制类型转换实现 Time:2014年8月12日 Author:小X ****************************/ 代 ...

  10. org.springframework.dao.EmptyResultDataAccessException

    public Wcrash getWcrashInfo(int id) { String sql = "select plateform_id,android_version,app_ver ...