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. qq去广告

    首先呢,在文件资源管理器中选择查看"隐藏的项目"或"显示隐藏的文件.文件夹和驱动器"(入口不一样,选择显示隐藏文件的方式也不一样),随后进入 C:\Users\ ...

  2. c_str()函数

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

  3. hdu3830 (二分+LCA)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Checkers Time Limit: 2000/1000 MS (Java/O ...

  4. (原)Ubuntu16中卸载并重新安装google的Protocol Buffers

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5782992.html 目前最新的是1.6.1 1. 卸载掉老版本的Protocol: sudo apt ...

  5. (原+转)VS2013:正在从以下位置加载符号

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5158020.html 这段时间启动调试时,vs2013加载时间很长很长...然后下面网址给出了解决方法 ...

  6. 异常处理与调试5 - 零基础入门学习Delphi54

    调试(Debug) 让编程改变世界 Change the world by program [caption id="attachment_2731" align="al ...

  7. BC 65 game

    主持人一直某个数字在1到n范围,假设甲乙已经知道,甲先猜乙后,都采用最优策略,主持人说偏大还是偏小,不断缩小范围,问最后乙能会获胜的X的取值的个数. 如果n为奇数,那么仅当x=n/2乙必然获胜,若为奇 ...

  8. request.getParamer()

    eturns the value of a request parameter as a String, or null if the parameter does not exist. Reques ...

  9. Effective Java单元测试JUnit - 就是爱Java

    实作了RoleImpl class,现在要开始单元测试了,或许你会觉得奇怪,才刚做好一个class而已,它并没有商业规则,只有getter/setter与clone(),那是要测试什么呢?没错,传统上 ...

  10. WPF笔记(2.7 文字布局)——Layout

    原文:WPF笔记(2.7 文字布局)--Layout 这一节介绍的是文字布局的几个控件:1.TextBlock      最基本的文字控件可以配置5个Font属性.TextWraping属性,&quo ...