题目如下:
给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数
要求下排每个数都是先前上排那十个数在下排出现的次数。
上排的十个数如下:
【0,1,2,3,4,5,6,7,8,9】
举一个例子,
数值:0,1,2,3,4,5,6,7,8,9
分配:6,2,1,0,0,0,1,0,0,0
0在下排出现了6次,1在下排出现了2次,
2在下排出现了1次,3在下排出现了0次....
以此类推..
运行下面的源代码将打印从7 到 30 的运行结果,从而发现了一个有意思的规律。
N为自然数,(1) 当 N>=1 且 N<4 时, 找不到符合的数组。 (2) 当N=4 ,答案数组为 {1 ,2 , 1 , 0 } (3) 当N=5 或 N=6 时,找不到符合的数组。
(4) 当N>=7 时, {N-4,2,1,{x-7个0},1,0,0,0 },组合成一个梯形图案。
public class TestNumber {
public static void main(String[] args) {
int[] temp;
for (int a = 7; a <= 30; a++) {
temp = TestNumber.generateArray(a);
if (temp != null) {
System.out.println();
for (int i : temp) {
System.out.print(i + "\t");
}
}
}
}
public static int[] generateArray(int n) {
if (n < 1) {
System.out.println("请输入不小于1的整数");
return null;
}
int[] top = new int[n];
int[] bottom = new int[n];
for (int i = 0; i < top.length; i++)
top[i] = i;
for (int m = 0; m < n + 2; m++) {
boolean flag = true;
for (int i = 0; i < n; i++) {
int count = getNumCount(i, bottom);
if (bottom[i] != count) {
bottom[i] = count;
flag = false;
}
}
if (flag)
break;
if (n + 1 == m && !flag) {
System.out.println("找不到结果");
return null;
}
}
return bottom;
}
public static int getNumCount(int inputNum, int[] array) {
int count = 0;
for (int i = 0; i < array.length; i++) {
if (inputNum == array[i]) {
count++;
}
}
return count;
}
}
上述的梯形
3 2 1 1 0 0 0
4 2 1 0 1 0 0 0
5 2 1 0 0 1 0 0 0
6 2 1 0 0 0 1 0 0 0
7 2 1 0 0 0 0 1 0 0 0
8 2 1 0 0 0 0 0 1 0 0 0
9 2 1 0 0 0 0 0 0 1 0 0 0
10 2 1 0 0 0 0 0 0 0 1 0 0 0
11 2 1 0 0 0 0 0 0 0 0 1 0 0 0
12 2 1 0 0 0 0 0 0 0 0 0 1 0 0 0
13 2 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0
14 2 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
15 2 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
16 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
17 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
18 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
19 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
20 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
21 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
22 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
23 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
24 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0