Last active
August 29, 2015 14:21
-
-
Save A-pZ/d8f9ab2dc72361eff3d9 to your computer and use it in GitHub Desktop.
某Q5のレガシーな回答
This file contains hidden or 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
/** | |
* | |
*/ | |
package q5; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
import lombok.extern.log4j.Log4j2; | |
/** | |
* @author A-pZ | |
* | |
*/ | |
@Log4j2 | |
public class Calc5 { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) throws Exception { | |
Calc5 cal = new Calc5(); | |
cal.exec(); | |
} | |
// 対象の数列 | |
public static List<Integer> values = Arrays.asList(1,2,3,4,5,6,7,8,9); | |
// 演算子 | |
public static List<String> operations = Arrays.asList("+" , "-" , ""); | |
// 期待される値 | |
public static final Integer exceptValue = 100; | |
/** | |
* | |
* @throws Exception | |
*/ | |
public void exec() throws Exception { | |
List<String> opePatterns = operationPatterns(); | |
List<String> calcExp = caluculationExpression(opePatterns); | |
allCalc(calcExp); | |
} | |
/** | |
* 演算の全パターンを出力する | |
* @return | |
*/ | |
public List<String> operationPatterns() throws Exception { | |
List<String> patterns = new LinkedList<String>(); | |
// 3進数の8桁全部パターン | |
double full = Math.pow( operations.size() , values.size() -1 ); | |
// 演算子の羅列を数値に変換。3種類あるので3進数 | |
for ( int i=0;i<full;i++ ) { | |
String str = Integer.toString(i, operations.size()); | |
// 演算子のみならんだ8文字の3進数ができる | |
patterns.add(String.format("%8s", str).replace(' ','0' )); | |
} | |
return patterns; | |
} | |
/** | |
* 1~9の数字の並びに、全ての演算子パターンを入れた式の一覧を取得する。 | |
* @param patterns 演算子パターンの3進数表現 | |
* @return 計算する式のList | |
* @throws Exception | |
*/ | |
public List<String> caluculationExpression(List<String> patterns) throws Exception { | |
List<String> calcExp = new ArrayList<String>(); | |
for ( String pattern:patterns ) { | |
//log.debug("pattern - {}" , pattern); | |
StringBuilder sb = new StringBuilder(); | |
for ( int i=0;i<pattern.length();i++ ) { | |
char operationChar = pattern.charAt(i); | |
sb.append( String.valueOf(values.get(i))).append( operations.get( Character.getNumericValue(operationChar)) ); | |
} | |
sb.append(values.size()); | |
calcExp.add(sb.toString()); | |
} | |
return calcExp; | |
} | |
/** | |
* 全て計算する | |
* @param calcExp | |
* @throws Exception | |
*/ | |
public void allCalc(List<String> calcExps) throws Exception { | |
for ( String expr :calcExps ) { | |
int sum = 0; | |
String tmp = ""; | |
boolean reset = false; | |
expr = "+" + expr; | |
// 演算子こみの数字を末尾から1文字ずつ解析 | |
for ( int i=expr.length();i>0;i--) { | |
char s = expr.charAt(i-1); | |
if ( reset ) { | |
reset = false; | |
tmp = ""; | |
} | |
if ( s >= '0' && s<= '9') { | |
tmp = String.valueOf(s).concat(tmp); | |
} else if ( s =='+') { | |
sum += Integer.parseInt(tmp); | |
reset = true; | |
} else if ( s == '-') { | |
sum -= Integer.parseInt(tmp); | |
reset = true; | |
} | |
} | |
if ( sum == exceptValue) { | |
log.debug("expr : {} = {}" ,expr,sum ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment