链接:http://poj.org/problem?id=1013
网上有个测试案例很好,大家搜一下
package demo_01_07;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Scanner;
public class POJ_1013 {
/**
* 先从even中找出正确的字母 再从up、down中找counterfeit
*/
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
int count;
count = Integer.parseInt(sc.next());
while (count-- != 0) {
String s1[] = new String[3];
String s2[] = new String[3];
String s[] = new String[3];
int cnt = 3;
while (cnt-- != 0) {
s1[cnt] = sc.next();
s2[cnt] = sc.next();
s[cnt] = sc.next();
}
String str_even = "";// 存储even
String str_no = "";// 存储非even
for (int i = 0; i < 3; i++) {
if (s[i].equals("even")) {
str_even += s1[i];
str_even += s2[i];
}
}
for(int i=0; i<3; i++){
if (!s[i].equals("even")) {
for(int j=0; j<s1[i].length(); j++){
if(str_even.indexOf(s1[i].charAt(j))==-1){//如果不存在于str_even中
str_no += s1[i].charAt(j);
}
}
for(int j=0; j<s2[i].length(); j++){
if(str_even.indexOf(s2[i].charAt(j))==-1){//如果不存在于str_even中
str_no += s2[i].charAt(j);
}
}
}
}
//遍历str_no中每个字母,假设其中某个是假的
for(int i=0; i<str_no.length(); i++){
char ch = str_no.charAt(i);//记录该字母下标
//到最后判断qing、zhong是否都变化,若都变化,则查找失败
int qing = 0;//轻,初始值,
int zhong = 0;//重,初始值
//从这三组中查找
for(int j=0; j<3; j++){
//找到嫌疑字母,只存在于左边
if(s1[j].indexOf(ch)!=-1 && s2[j].indexOf(ch)==-1){
if(s[j].equals("up")){
zhong = 1;
}else{
qing = 1;
}
}
//找到嫌疑字母,只存在于右边
if(s2[j].indexOf(ch)!=-1 && s1[j].indexOf(ch)==-1){
if(s[j].equals("up")){
qing = 1;
}else{
zhong = 1;
}
}
//若该字母不存在于两边,那么应该两边even,否则该字母是正确的
if(s1[j].indexOf(ch)==-1 && s2[j].indexOf(ch)==-1){
if(!s[j].equals("even")){
//将两者设为1,能跳过最后的判断
qing = 1;
zhong = 1;
}
}
}
//如果qing、zhong只有一个变化
if(qing + zhong == 1)
{
if(qing == 1) System.out.println(ch+" is the counterfeit coin and it is light.");
else System.out.println(ch+" is the counterfeit coin and it is heavy.");
break;
}
}
}
}
}
网上有个测试案例很好,大家搜一下
package demo_01_07;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Scanner;
public class POJ_1013 {
/**
* 先从even中找出正确的字母 再从up、down中找counterfeit
*/
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
int count;
count = Integer.parseInt(sc.next());
while (count-- != 0) {
String s1[] = new String[3];
String s2[] = new String[3];
String s[] = new String[3];
int cnt = 3;
while (cnt-- != 0) {
s1[cnt] = sc.next();
s2[cnt] = sc.next();
s[cnt] = sc.next();
}
String str_even = "";// 存储even
String str_no = "";// 存储非even
for (int i = 0; i < 3; i++) {
if (s[i].equals("even")) {
str_even += s1[i];
str_even += s2[i];
}
}
for(int i=0; i<3; i++){
if (!s[i].equals("even")) {
for(int j=0; j<s1[i].length(); j++){
if(str_even.indexOf(s1[i].charAt(j))==-1){//如果不存在于str_even中
str_no += s1[i].charAt(j);
}
}
for(int j=0; j<s2[i].length(); j++){
if(str_even.indexOf(s2[i].charAt(j))==-1){//如果不存在于str_even中
str_no += s2[i].charAt(j);
}
}
}
}
//遍历str_no中每个字母,假设其中某个是假的
for(int i=0; i<str_no.length(); i++){
char ch = str_no.charAt(i);//记录该字母下标
//到最后判断qing、zhong是否都变化,若都变化,则查找失败
int qing = 0;//轻,初始值,
int zhong = 0;//重,初始值
//从这三组中查找
for(int j=0; j<3; j++){
//找到嫌疑字母,只存在于左边
if(s1[j].indexOf(ch)!=-1 && s2[j].indexOf(ch)==-1){
if(s[j].equals("up")){
zhong = 1;
}else{
qing = 1;
}
}
//找到嫌疑字母,只存在于右边
if(s2[j].indexOf(ch)!=-1 && s1[j].indexOf(ch)==-1){
if(s[j].equals("up")){
qing = 1;
}else{
zhong = 1;
}
}
//若该字母不存在于两边,那么应该两边even,否则该字母是正确的
if(s1[j].indexOf(ch)==-1 && s2[j].indexOf(ch)==-1){
if(!s[j].equals("even")){
//将两者设为1,能跳过最后的判断
qing = 1;
zhong = 1;
}
}
}
//如果qing、zhong只有一个变化
if(qing + zhong == 1)
{
if(qing == 1) System.out.println(ch+" is the counterfeit coin and it is light.");
else System.out.println(ch+" is the counterfeit coin and it is heavy.");
break;
}
}
}
}
}