解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论
题目1、分机号X老板脾气古怪,他们公司的电话分机号都是3位数,老板规定,所有号码必须是降序排列,且不能有重复的数位。比如:
751,520,321 都满足要求,而,
766,918,201 就不符合要求。
现在请你计算一下,按照这样的规定,一共有多少个可用的3位分机号码?
请直接提交该数字,不要填写任何多余的内容。
答案:120
public class Main {
public static void main(String[] args) {
int count = 0;
for(int a = 0;a < 10;a++)
for(int b = 0;b < 10;b++)
for(int c = 0;c < 10;c++)
if(a > b && b > c)
count++;
System.out.println(count);
}
}
题目2、五星填数
如【图1.png】的五星图案节点填上数字:1~12,除去7和11。
要求每条直线上数字和相等。
如图就是恰当的填法。
请你利用计算机搜索所有可能的填法有多少种。
注意:旋转或镜像后相同的算同一种填法。
请提交表示方案数目的整数,不要填写任何其它内容。
答案:12
public class Main {
public static int count = 0;
public void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public void check(int[] A) {
int sum1 = A[0] + A[2] + A[5] + A[8];
int sum2 = A[0] + A[3] + A[6] + A[9];
int sum3 = A[1] + A[2] + A[3] + A[4];
int sum4 = A[1] + A[5] + A[7] + A[9];
int sum5 = A[4] + A[6] + A[7] + A[8];
if(sum1 == sum2 && sum1 == sum3 && sum1 == sum4 && sum1 == sum5) {
count++;
} else
return;
}
public void dfs(int[] A, int step) {
if(step == A.length) {
check(A);
return;
} else {
for(int i = step;i < A.length;i++) {
swap(A, i, step);
dfs(A, step + 1);
swap(A, i, step);
}
}
}
public static void main(String[] args) {
Main test = new Main();
int[] A = {1,2,3,4,5,6,8,9,10,12};
test.dfs(A, 0);
System.out.println(count / 10);
}
}
题目3、显示二叉树
题目描述
排序二叉树的特征是:
某个节点的左子树的所有节点值都不大于本节点值。
某个节点的右子树的所有节点值都不小于本节点值。
为了能形象地观察二叉树的建立过程,小明写了一段程序来显示出二叉树的结构来。
class BiTree
{
private int v;
private BiTree l;
private BiTree r;
public BiTree(int v){
this.v = v;
}
public void add(BiTree the){
if(the.v < v){
if(l==null) l = the;
else l.add(the);
}
else{
if(r==null) r = the;
else r.add(the);
}
}
public int getHeight(){
int h = 2;
int hl = l==null? 0 : l.getHeight();
int hr = r==null? 0 : r.getHeight();
return h + Math.max(hl,hr);
}
public int getWidth(){
int w = (""+v).length();
if(l!=null) w += l.getWidth()