两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
public class Test2_5_ztk {
public static void main(String args[]){
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i!=j){ //a的对手跟b的对手不是一个人
for(int k=1;k<=3;k++){
if(i!=k&&j!=k){ //a的对手还有b的对手跟c的对手不是同一个人
if(i!=1&&k!=1&&k!=3){ //a的对手不是x,c的对手不是x和z
char m='x',n='x',p='x';
for(int a=1;a<i;a++)
m++;
for(int a=1;a<j;a++)
n++;
for(int a=1;a<k;a++)
p++;
System.out.println("a vs "+m);
System.out.println("b vs "+n);
System.out.println("c vs "+p);
}
}
}
}
}
}
}
}
实用型转换方式的题目,套用循环,最后进行一个if判断进行输出,将a,b,c对照成1,2,3.类似x,y,z形式,最后进行转换输出。
还找到思路相似,对字符串掌握较好的解题方法:
public class Ch02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(char a='x';a<='z';a++) {
for(char b='x';b<='z';b++) {
//A和B的对手不相同
if(a!=b) {
for(char c='x';c<='z';c++) {
//A和C的对手不相同B和C的对手不相同
if(a!=c&&b!=c) {
//A的对手不是X,C的对手不是X,C的对手不是Z
if(a!='x'&&c!='x'&&c!='z') {
System.out.println("a vs "+a);
System.out.println("b vs "+b);
System.out.println("c vs "+c);
}
}
}
}
}
}
}
}
————————————————
版权声明:本文为CSDN博主「xuehyunyu」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xuehyunyu/article/details/76774335