Java初心者のためのスレッドです。
※前スレ
Java入門・初心者質問スレ Part.5
http://mevius.2ch.net/test/read.cgi/tech/1506675205/
探検
Java入門・初心者質問スレ Part.6
■ このスレッドは過去ログ倉庫に格納されています
2017/12/21(木) 00:24:33.33ID:E/ADCCn6
413デフォルトの名無しさん
2018/01/13(土) 20:23:18.22ID:wiN0PDfT import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class AllTopologies {
■■private final List<Set<Integer>> topologies;
■■private Set<Integer> setOfSubsets;
■■private int setOfSubsetsID;
■■private final int n;
■■private final int m;
■■private final int maxSetOfSubsetsID;
■■public AllTopologies(int n) {
■■■■topologies = new ArrayList<Set<Integer>>();
■■■■setOfSubsets = new HashSet<Integer>();
■■■■setOfSubsetsID = 0;
■■■■this.n = n;
■■■■m = (int) Math.pow(2.0, (double) n) - 1;
■■■■maxSetOfSubsetsID = (int) Math.pow(2.0, (double) m - 1) - 1;
■■■■computeAllTopologies();
■■}
■■
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class AllTopologies {
■■private final List<Set<Integer>> topologies;
■■private Set<Integer> setOfSubsets;
■■private int setOfSubsetsID;
■■private final int n;
■■private final int m;
■■private final int maxSetOfSubsetsID;
■■public AllTopologies(int n) {
■■■■topologies = new ArrayList<Set<Integer>>();
■■■■setOfSubsets = new HashSet<Integer>();
■■■■setOfSubsetsID = 0;
■■■■this.n = n;
■■■■m = (int) Math.pow(2.0, (double) n) - 1;
■■■■maxSetOfSubsetsID = (int) Math.pow(2.0, (double) m - 1) - 1;
■■■■computeAllTopologies();
■■}
■■
414デフォルトの名無しさん
2018/01/13(土) 20:23:36.74ID:wiN0PDfT ■■public void printAllTopologies() {
■■■■System.out.print("The number of topologies on the set {");
■■■■int i;
■■■■for (i = 1; i < n; i++) {
■■■■■■System.out.print(i + ", ");
■■■■}
■■■■System.out.print(i);
■■■■System.out.println("} is " + topologies.size() + ".");
■■■■System.out.println();
■■■■
■■■■System.out.print("The all topologies on the set {");
■■■■for (i = 1; i < n; i++) {
■■■■■■System.out.print(i + ", ");
■■■■}
■■■■System.out.print(i);
■■■■System.out.println("} are:");
■■■■
■■■■for (Set<Integer> s : topologies) {
■■■■■■System.out.print("[");
■■■■■■System.out.print(convertIntToSet(0));
■■■■■■System.out.print(", ");
■■■■■■for (int e : s) {
■■■■■■■■
■■■■■■■■System.out.print(convertIntToSet(e));
■■■■■■■■System.out.print(", ");
■■■■■■}
■■■■■■System.out.print(convertIntToSet(m));
■■■■■■System.out.println("]");
■■■■}
■■}
■■■■System.out.print("The number of topologies on the set {");
■■■■int i;
■■■■for (i = 1; i < n; i++) {
■■■■■■System.out.print(i + ", ");
■■■■}
■■■■System.out.print(i);
■■■■System.out.println("} is " + topologies.size() + ".");
■■■■System.out.println();
■■■■
■■■■System.out.print("The all topologies on the set {");
■■■■for (i = 1; i < n; i++) {
■■■■■■System.out.print(i + ", ");
■■■■}
■■■■System.out.print(i);
■■■■System.out.println("} are:");
■■■■
■■■■for (Set<Integer> s : topologies) {
■■■■■■System.out.print("[");
■■■■■■System.out.print(convertIntToSet(0));
■■■■■■System.out.print(", ");
■■■■■■for (int e : s) {
■■■■■■■■
■■■■■■■■System.out.print(convertIntToSet(e));
■■■■■■■■System.out.print(", ");
■■■■■■}
■■■■■■System.out.print(convertIntToSet(m));
■■■■■■System.out.println("]");
■■■■}
■■}
415デフォルトの名無しさん
2018/01/13(土) 20:23:58.33ID:wiN0PDfT ■■private Set<Integer> convertIntToSet(int s) {
■■■■Set<Integer> set = new HashSet<Integer>();
■■■■for (int i = 1; i <= n; i++) {
■■■■■■if ((s & 0x00000001) == 1) {
■■■■■■■■set.add(i);
■■■■■■}
■■■■■■s >>= 1;
■■■■}
■■■■return set;
■■}
■■
■■private boolean nextSetOfSubsets() {
■■■■if (setOfSubsetsID == maxSetOfSubsetsID) {
■■■■■■return false;
■■■■}
■■■■setOfSubsetsID++;
■■■■setOfSubsets = new HashSet<Integer>();
■■■■int t = setOfSubsetsID;
■■■■for (int i = 1; i < m; i++) {
■■■■■■if ((t & 0x00000001) == 1) {
■■■■■■■■setOfSubsets.add(i);
■■■■■■}
■■■■■■t >>= 1;
■■■■}
■■■■return true;
■■}
■■■■Set<Integer> set = new HashSet<Integer>();
■■■■for (int i = 1; i <= n; i++) {
■■■■■■if ((s & 0x00000001) == 1) {
■■■■■■■■set.add(i);
■■■■■■}
■■■■■■s >>= 1;
■■■■}
■■■■return set;
■■}
■■
■■private boolean nextSetOfSubsets() {
■■■■if (setOfSubsetsID == maxSetOfSubsetsID) {
■■■■■■return false;
■■■■}
■■■■setOfSubsetsID++;
■■■■setOfSubsets = new HashSet<Integer>();
■■■■int t = setOfSubsetsID;
■■■■for (int i = 1; i < m; i++) {
■■■■■■if ((t & 0x00000001) == 1) {
■■■■■■■■setOfSubsets.add(i);
■■■■■■}
■■■■■■t >>= 1;
■■■■}
■■■■return true;
■■}
416デフォルトの名無しさん
2018/01/13(土) 20:24:16.43ID:wiN0PDfT ■■private boolean isSetOfSubsetsTopology() {
■■■■List<Integer> list = new ArrayList<Integer>(setOfSubsets);
■■■■loop:
■■■■for (int i = 0; i < list .size(); i++) {
■■■■ for (int j = i + 1; j < list .size(); j++) {
■■■■ ■■int intersection = intersection(list.get(i), list.get(j));
■■■■ ■■if (intersection != 0 && intersection != m && !setOfSubsets.contains(intersection)) {
■■■■ ■■■■return false;
■■■■ ■■}
■■■■ ■■int union = union(list.get(i), list.get(j));
■■■■ ■■if (union != 0 && union != m && !setOfSubsets.contains(union)) {
■■ ■■■■■■return false;
■■■■ ■■}
■■■■ }
■■■■}
■■■■return true;
■■}
■■
■■private void computeAllTopologies() {
■■■■do {
■■■■■■if (isSetOfSubsetsTopology()) {
■■■■■■■■topologies.add(setOfSubsets);
■■■■■■}
■■■■} while(nextSetOfSubsets());
■■}
■■■■List<Integer> list = new ArrayList<Integer>(setOfSubsets);
■■■■loop:
■■■■for (int i = 0; i < list .size(); i++) {
■■■■ for (int j = i + 1; j < list .size(); j++) {
■■■■ ■■int intersection = intersection(list.get(i), list.get(j));
■■■■ ■■if (intersection != 0 && intersection != m && !setOfSubsets.contains(intersection)) {
■■■■ ■■■■return false;
■■■■ ■■}
■■■■ ■■int union = union(list.get(i), list.get(j));
■■■■ ■■if (union != 0 && union != m && !setOfSubsets.contains(union)) {
■■ ■■■■■■return false;
■■■■ ■■}
■■■■ }
■■■■}
■■■■return true;
■■}
■■
■■private void computeAllTopologies() {
■■■■do {
■■■■■■if (isSetOfSubsetsTopology()) {
■■■■■■■■topologies.add(setOfSubsets);
■■■■■■}
■■■■} while(nextSetOfSubsets());
■■}
417デフォルトの名無しさん
2018/01/13(土) 20:24:41.80ID:wiN0PDfT ■■private static int intersection(int subset1, int subset2) {
■■■■return subset1 & subset2;
■■}
■■private static int union(int subset1, int subset2) {
■■■■return subset1 | subset2;
■■}
■■public static void main(String[] args) {
■■■■// TODO Auto-generated method stub
■■■■AllTopologies allTopologies = new AllTopologies(4);
■■■■allTopologies.printAllTopologies();
■■}
■■
}
■■■■return subset1 & subset2;
■■}
■■private static int union(int subset1, int subset2) {
■■■■return subset1 | subset2;
■■}
■■public static void main(String[] args) {
■■■■// TODO Auto-generated method stub
■■■■AllTopologies allTopologies = new AllTopologies(4);
■■■■allTopologies.printAllTopologies();
■■}
■■
}
■ このスレッドは過去ログ倉庫に格納されています
ニュース
- 中国軍機レーダー照射、トランプ氏沈黙突く 試される日本外交 ★3 [蚤の市★]
- 元プロ野球選手・堂上隼人(43)を20代女性2人へのわいせつ未遂容疑で8回目の逮捕…これまでの被害者は10代・20代の女性11人に [Anonymous★]
- 中国軍機レーダー照射、トランプ氏沈黙突く 試される日本外交 ★4 [蚤の市★]
- 【高校野球】なぜ『7回制』は反対多数でも止まらないか… 高野連が「全員の命」守るために貫く伝統より改革の姿勢 [冬月記者★]
- JAが"政府の備蓄米買い上げ"見越して価格下げず!?「古いコメは食用向きでないなどと理由をつけ...」専門家解説 [煮卵★]
- 【テレビ】石破前首相 中国レーダー照射「フェーズ上がってる」と指摘も「日本の世論が激高するのは避ける必要が…」 [少考さん★]
- 【高市悲報】自衛隊「実は事前に現場海域で中国軍から空母での発着訓練をすると通告がありました」え…?😨 [931948549]
- 【悲報】山里亮太(南海キャンディーズ)さん [329329848]
- 統一教会っていらない田んぼ畑ビルディング(アスベスト)も引き取ってくれるの? [358382861]
- 【高市悲報】日本が🇨🇳輸出規制したフォトレジスト、早速韓国企業が中国に売り込みかけて日本の対抗手段もうなくなるwww [709039863]
- 中国父「日本の一般大衆は高市を支持しておらず、反対している人も多い。悪いのは日本国民ではなく高市!」 すまんこれほんと? [271912485]
- 高市「中国さんお願い電話で話そ、このままじゃ武力衝突になっちゃう😭」日中間の専用電話に日本側からかけるも無視される [931948549]
