java 任意花朵数算法
来源:互联网 发布:淘宝介入后可以撤销吗 编辑:程序博客网 时间:2024/06/11 18:25
/** * 花朵数,网友算法下载学习 * @author cxcx * */public class Arithmetic11 { private static BigInteger[] table = new BigInteger[10]; public static void main(String[] args) { long time = System.nanoTime(); find(21); time = System.nanoTime() - time; System.out.println(time / 1000000000.0 + "s"); } public static void find(int n) { for (int i = 0; i < 10; i++) table[i] = BigInteger.valueOf(i).pow(n); int[] nums = new int[n]; int index = 0; int num = 0; BigInteger sum = BigInteger.ZERO; BigInteger MIN = BigInteger.TEN.pow(n - 1); BigInteger MAX = BigInteger.TEN.pow(n).subtract(BigInteger.ONE); while (true) { if (index < nums.length && num < 10) { BigInteger temp = sum.add(table[num]); if (temp.compareTo(MAX) < 0) { nums[index] = num; index++; sum = temp; continue; } } else if (index >= nums.length && sum.compareTo(MIN) > 0) { int[] temp = getArray(sum); if (check(nums, true, temp, false)) System.out.println(sum); } else if (index <= 0) { break; } index--; num = nums[index]; sum = sum.subtract(table[num]); num++; } } public static boolean check(int[] a1, boolean copy1, int[] a2, boolean copy2) { if (a1.length != a2.length) return false; if (copy1) a1 = a1.clone(); if (copy2) a2 = a2.clone(); Arrays.sort(a1); Arrays.sort(a2); return Arrays.equals(a1, a2); } public static int[] getArray(BigInteger big) { String s = String.valueOf(big); int length = s.length(); int[] res = new int[length]; for (int i = 0; i < length; i++) res[i] = s.charAt(i) - '0'; return res; }}
算法参考:http://bbs.csdn.net/topics/360185693
- java 任意花朵数算法
- 花朵数算法--java
- java 计算 水仙花 花朵数 算法 优化
- 花朵数算法
- 花朵数--21----c算法
- 21位花朵数算法
- 算法——花朵数
- 花朵数
- 花朵数
- 花朵数
- 花朵数
- 有关花朵数一段java程序
- Java快速查找21位花朵数
- 一个求21位花朵数的Java程序
- 21位花朵数
- 花朵数的研究
- 21位花朵数
- 花朵数的研究
- oracle数据类型及函数实例
- SQL Server 2005中触发器
- struts2开发环境
- After clone Redhat machine by using vSphere, can't reach the new machine
- 茫然
- java 任意花朵数算法
- 黑马程序员之C#学习笔记:使用Stream.BeginRead方法读取FileStream的流内容
- grails Erro type :loadXML 错误
- ret,retf,iret等的区别
- ResourceHandle ResourceLoader和Client
- oracle查询连接
- VS2012 无法 Intellisense
- 字符和字符串
- C++ MFC ASCII码 字符 和十进制 或 十六进制字符串 互相转化