Brain StormC/C++ codeGive you a integer N and N is not equal to 10^i and i belongs to {0, 1, 2, 3,
Brain Storm
C/C++ code
Give you a integer N and N is not equal to 10^i and i belongs to {0, 1, 2, 3, ...}.for example, N = 2, we get following data:1th: 2*2 = 42nd: 4*4 = 163rd: 16*16 = 2564th: 256*256 = 655365th: 65536*65536 = 42949672966th: 4294967296*4294967296 = 18446744073709551616construct a set with all the number character appeared in all result of power operation.please show me the minimum times of power operation to get a set equal to {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.and prove your idea.
public int test(long a) { // 判断a是否是10的n次方 if (a == 1) { return -1; } String temp = Long.toString(a); if (temp.charAt(0) == '1' && Long.parseLong(temp.substring(1)) == 0) { return -1; }
Set<Integer> set = new HashSet<Integer>(); // 记录求幂次数 int count = 0; boolean flag = true; while (flag) { a = a * a; List<Integer> list = toArray(a); for (int j = 0; j < list.size(); j++) { int t = list.get(j); set.add(t); if (set.size() == 10) { flag = false; break; } } count++; } return count; }
/** * 该方法将整数拆分为一个个0-9的数,保存到list中 * * @param num * @return */ public List<Integer> toArray(long num) { List<Integer> list = new ArrayList<Integer>(); while (num > 0) { int t = (int) (num % 10); list.add(t); num = num / 10; } return list; } [解决办法] 牛叉叉的题目,正在思考ing [解决办法] 我觉得可以通过从{4,5,9,6}开始,结果可能是这几个数开始得出的结果+1,因为任何数的平方末尾必然是 1,4,5,9,6,N不等于1.故从{4,5,9,6}开始