public static void main(String[] args) { D start = new D(); List<Node> nn = start.check(start.data); nn.forEach(n -> System.out.println(n.x + ":" + n.y)); }
private List<Node> check(List<Node> nodes){ if (nodes.size() == 1) { return nodes; } List<Node> result = new ArrayList<>(); boolean can = false; for (int i = 0; i < nodes.size(); i++) { for (int j = i+1; j < nodes.size(); j++) { if (nodes.get(i).y == nodes.get(j).x) { result.add(new Node(nodes.get(i).x, nodes.get(j).y)); i++; can = true; break; }else { if (!result.contains(nodes.get(i))) { result.add(nodes.get(i)); } if (!result.contains(nodes.get(j))) { result.add(nodes.get(j)); } } } if (i == nodes.size() -1) { if (!result.contains(nodes.get(i))) { result.add(nodes.get(i)); } } } if (can) result = check(result); return result; }
class Node{ Node(int x, int y) { this.x = x; this.y = y; }
int x; int y; }
}
frmongo
2018-08-28 13:50:45 +08:00
我写了一个,但是 fusion 是一个有缺陷的函数,需要改进
from random import choice
def combine_two(a,b): c = [None,None] if a[1]==b[0]: c[0]=a[0] c[1]=b[1] elif b[1] == a[0]: c[0]=b[0] c[1]=a[1] return c
def fusion(list): ss = list[:] nn = len(ss) for i in range(100): ele_1 = choice(ss) ele_2 = choice(ss) c = combine_two(ele_1,ele_2) if c != [None,None]: ss.remove(ele_1) ss.remove(ele_2) ss.append(c) return ss
def check(list): n = len(list) for i in range(n): for j in range(n): if i != j: if list[i][0] == list[j][0] and list[i][1] == list[j][1]: return False return True
jj = [[2,3],[3,4],[1,2],[4,7],[11,13],[1,7]] m = fusion(jj) print m n = check(m) print n
aaaaasam
2018-08-30 14:12:56 +08:00
```python list_a = [[1,2],[2,3],[3,4],[4,7],[1,7]] for i in list_a: list_test = [(i[0],x[1]) for x in list_a if i != x and i[1] == x[0]] if list_test: print(i, list_test) ```
第 1 页 / 共 1 页
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。