Created
August 29, 2018 00:55
-
-
Save lctseng/fd9256b5ef84fc4d31c0542a424e89ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.*; | |
public class 方劑單味藥組合 | |
{ | |
private static Set<Set<String>> getPowerSet(List<String> 藥物集合) | |
{ | |
int n = 藥物集合.size(); | |
Set<Set<String>> powerSet = new HashSet<Set<String>>(); | |
for(long i = 0; i < (1 << n); i++) | |
{ | |
SortedSet<String> element = new TreeSet<String>(java.text.Collator.getInstance(Locale.TRADITIONAL_CHINESE)); | |
for(int j = 0; j < n; j++ ) | |
{ | |
if( (i >> j) % 2 == 1 ) | |
element.add(藥物集合.get(j)); | |
} | |
powerSet.add(element); | |
} | |
return powerSet; | |
} | |
public static void main(String[] args) | |
{ | |
if(args.length == 0) | |
{ | |
System.out.println("One file name required!"); | |
return; | |
} | |
BufferedReader bufferedReader = null; | |
try | |
{ | |
int i, j, k; | |
bufferedReader = new BufferedReader(new FileReader(args[0])); | |
String line = null; | |
//栝蔞桂枝湯方:栝蔞根(二兩)、桂枝(三兩)、芍藥(三兩)、甘草(二兩)、生薑(三兩)、大棗(十二枚) | |
while ((line = bufferedReader.readLine()) != null) | |
{ | |
int 冒號_index = line.indexOf(":"); | |
if(冒號_index > 0) | |
{ | |
String 方名 = line.substring(0, 冒號_index); | |
String[] 藥名_劑量 = line.substring(冒號_index + 1).split("、"); | |
List<String> 藥物集合 = new ArrayList<String>(); | |
for(j = 0; j < 藥名_劑量.length; j++) | |
{ | |
int 左括號_index = 藥名_劑量[j].indexOf("("); | |
if(左括號_index > 0) | |
{ | |
藥物集合.add(藥名_劑量[j].substring(0, 左括號_index)); | |
} | |
else | |
{ | |
藥物集合.add(藥名_劑量[j]); // 只有藥名沒有括號 | |
} | |
} | |
Set<Set<String>> 藥物所有組合之集合 = getPowerSet(藥物集合); | |
for(Set<String> set : 藥物所有組合之集合) | |
{ | |
System.out.print(set.size() + "\t"); | |
for(String s : set) | |
{ | |
System.out.print(s + "_"); | |
} | |
System.out.println("\t" + line); | |
} | |
藥物所有組合之集合.clear(); | |
} | |
} | |
} | |
catch (FileNotFoundException e) | |
{ | |
e.printStackTrace(); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
finally | |
{ | |
try | |
{ | |
if (bufferedReader != null) | |
bufferedReader.close(); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment