https://www.codewars.com/kata/54d512e62a5e54c96200019e/train/javahtml
package codewars; import java.util.*; import java.util.regex.*; public class PrimeDecomp{ public static String primeDecomp(int n){ StringBuilder sb = new StringBuilder(); int i = 2; int count = 0; while(n >= i){ if(n % i == 0){ if(count > 0){ sb.append("*"); } n = n / i; sb.append(i); count++; }else{ i++; } } return sb.toString(); } public static String factors(int n){ StringBuilder sb = new StringBuilder(); String result = PrimeDecomp(n); String regex = "(\\d+)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(result); Map<Integer,Integer> map = new TreeMap<Integer,Integer>(); while(m.find()){ Integer temp = Integer.valueOf(m.group(1)); if(map.containsKey(temp)){ map.put(temp,map.get(temp) + 1); }else{ map.put()temp,1; } } for(Map.Entry<Integer,Integer> x : map.entrySet()){ if(x.getValue() == 1){ sb.append(String.format("(%d)",x.getKey())); }else{ sb.append(String.format("(%d**%d)",x.getKey(),x.getValue())); } } return sb.toString(); } }
Reference:java
Java 因式分解app