67,549
社区成员




- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class PingpongGame {
- // 选手集
- private static final List<String> teamA = new ArrayList<String>();
- private static final List<String> teamB = new ArrayList<String>();
- // 结果集:teamA队员为key,teamB队员为value
- private static final Map<String, String> result = new HashMap<String, String>();
- // 规则集:不允许集,teamA某个队员为key,不允许的对手名单列表为value
- private static final Map<String, List<String>> noMap = new HashMap<String, List<String>>();
- // 规则集初始化
- static {
- // 队员初始化
- teamA.add("a");
- teamA.add("b");
- teamA.add("c");
- teamB.add("x");
- teamB.add("y");
- teamB.add("z");
- // a说他不和x比
- List<String> aNoList = new ArrayList<String>();
- aNoList.add("x");
- noMap.put("a", aNoList);
- // b的对手任意
- List<String> bNoList = new ArrayList<String>();
- noMap.put("b", bNoList);
- // c说他不和x,z比
- List<String> cNoList = new ArrayList<String>();
- cNoList.add("x");
- cNoList.add("z");
- noMap.put("c", cNoList);
- }
- public static void main(String[] args) {
- // 最终要得到的结果,是三队比赛场次的输出
- for (;;) {
- // 跳出循环的条件:形成三组对抗
- if (result.size() == teamA.size()) {
- break;
- }
- // 遍历A组学员
- for (String member : teamA) {
- // 若当前队员已经有对手,则跳过本次循环
- if (null != result.get(member)) {
- continue;
- }
- // 判断当前成员的不可能对手集,若teamB去掉此集合后,仅剩下一个队员,则录入结果集
- checkNoListAnd(member, noMap.get(member));
- }
- }
- // 打印结果
- for (Map.Entry<String, String> entry : result.entrySet()) {
- System.out.println("A组选手" + entry.getKey() + "的对手为B组选手" + entry.getValue());
- }
- }
- // 判断当前成员的不可能对手集,若teamB去掉此集合后,仅剩下一个队员,则录入结果集
- private static void checkNoListAnd(String member, List<String> noList) {
- // 不可能结果集为空,则不处理
- if (null == noList || 0 == noList.size()) {
- return;
- }
- // 若teamB去掉此集合后,仅剩下一个队员,则录入结果集
- List<String> teamBTmp = new ArrayList<String>(teamB);
- teamBTmp.removeAll(noList);
- if (1 == teamBTmp.size()) {
- result.put(member, teamBTmp.get(0));
- addMayBeList(teamBTmp);
- }
- }
- // 已被占用的teamB队员,加入到所有成员的不可能对手集中
- private static void addMayBeList(List<String> noBeList) {
- for (Map.Entry<String, List<String>> entry : noMap.entrySet()) {
- // 避免重复加入
- if (!entry.getValue().containsAll(noBeList)) {
- entry.getValue().addAll(noBeList);
- }
- }
- }
- }
#include <stdio.h>
#define UAN 2 //非对手
#define AN 1 //对手
#define UNKNOWN 0 //未知
char a[] = { UAN, UNKNOWN, UNKNOWN };
char b[] = { UNKNOWN, UNKNOWN, UNKNOWN};
char c[] = { UAN, UNKNOWN, UAN};
int hor_sum(char *arr)
{
return arr[0] + arr[1] + arr[2];
}
int ver_sum(int col)
{
return a[col] + b[col] + c[col];
}
int find_hor_unknown(char *arr)
{
return !arr[0]? 0 : (!arr[1]? 1:2);
}
int find_ver_unknown(int col)
{
return !a[col]? 0 : (!b[col]? 1:2);
}
void set_ua(char *arr,int pos)
{
arr[0] = arr[1] = arr[2] = UAN;
a[pos] = UAN;
b[pos] = UAN;
c[pos] = UAN;
arr[pos] = AN;
}
void show_ua()
{
int i;
for(i = 0;i < 3;i++) {
if(a[i] == AN)
printf("A Against %c\n",i + 88);
if(b[i] == AN)
printf("B Against %c\n",i + 88);
if(c[i] == AN)
printf("C Against %c\n",i + 88);
}
}
int main()
{
int i,j;
for(i = 0;i < 3;i++) {
if(hor_sum(a) == 4) {
set_ua(a,find_hor_unknown(a));
}
if(hor_sum(b) == 4) {
set_ua(b,find_hor_unknown(b));
}
if(hor_sum(c) == 4) {
set_ua(c,find_hor_unknown(c));
}
for(j = 0;j < 3;j++) {
if(ver_sum(j) == 4) {
int pos = find_ver_unknown(j);
if(pos == 0)
set_ua(a,j);
else if(pos == 1)
set_ua(b,j);
else
set_ua(c,j);
}
}
}
show_ua();
}
String[] arr1 = {"a","b","c"};
String[] arr2 = {"x","y","z"};
Map<String,List<String>> map = new LinkedHashMap<String,List<String>>();
for(int i = 0; i < arr1.length; i ++) {
for(int k = 0; k < arr2.length; k ++) {
if("a".equals(arr1[i]) && "x".equals(arr2[k])){
continue;
}
else if("c".equals(arr1[i]) && ("x".equals(arr2[k]) || "z".equals(arr2[k]))) {
continue;
}
else {
if(map.containsKey(arr1[i])) {
map.get(arr1[i]).add(arr2[k]);
}
else {
List<String> list = new ArrayList<String>();
list.add(arr2[k]);
map.put(arr1[i], list);
}
}
}
}
for(Entry<String, List<String>> entry1 : map.entrySet()) {
List<String> value1 = entry1.getValue();
for(Entry<String, List<String>> entry2 : map.entrySet()) {
List<String> value2 = entry2.getValue();
if(value1 == value2) {
continue;
}
else if(value1.size() == 1) {
map.put(entry1.getKey(), value1);
break;
}
else if(value1.size() >= value2.size()){
value1.removeAll(value2);
}
else if(value2.size() < value2.size()) {
value2.removeAll(value1);
map.put(entry2.getKey(), value2);
}
}
map.put(entry1.getKey(), value1);
System.out.println(entry1.getKey() + value1);
}
好像很麻烦的样子