正则表达式判断是否素数、解线性方程
编程技术  /  houtizong 发布于 3年前   49
public static boolean prime(int n) { return !new String(new char[n]).matches(".?|(..+?)\\1+");}
public static void sovleLinearEquation(int result, int...params) { if (result <= 0) { throw new IllegalArgumentException("result must be positive"); } if (params == null || params.length == 0) { throw new IllegalArgumentException("params must not be empty"); } //^(.*)\1{A−1}(.*)\2{B−1}$ StringBuilder p = new StringBuilder("^"); for (int i = 0, len = params.length; i < len; i++) { int param = params[i]; if (param <= 0) { throw new IllegalArgumentException("param must be positive:" + param); } p.append("(.*)\\").append(i + 1).append("{").append(param - 1).append("}"); } p.append("$"); Pattern pattern = Pattern.compile(p.toString()); Matcher matcher = pattern.matcher(new String(new char[result])); if (matcher.find()) { StringBuilder exp = new StringBuilder(); for (int i = 0, len = params.length; i < len; i++) { exp.append(" + ").append(params[i]).append(" * ").append(matcher.group(i + 1).length()); } if (exp.length() > 0) { exp.append(" = " + result); System.out.println(exp.substring(3)); } } else { System.out.println("unsovleable"); } }
sovleLinearEquation(51, 17, 12);sovleLinearEquation(115, 11, 2, 5);==》17 * 3 + 12 * 0 = 5111 * 10 + 2 * 0 + 5 * 1 = 115
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接