中兴笔试LED
import java.util.*;
/**
* @author ${USER}
* @date ${DATE}
* @since 1.8
**/
public class Main {
private static int[] weight = new int[7];
static {
for (int i = 1; i < weight.length; i++) {
int temp = 1;
for (int j = 1; j <= (i - 1); j++) {
temp *= 2;
}
weight[i] = temp;
}
} private static void backtrack(List<Integer> allResult,int result,int length,int startIdx,int leftNum){
if(leftNum==0){
if(length==4 && result>=0 && result<=11){
allResult.add(result);
}
if(length==6 && result>=0 && result<=59){
allResult.add(result);
}
return;
}
for (int i = startIdx; i <= length; i++) {
result += weight[i];
leftNum--;
backtrack(allResult,result,length,i+1,leftNum);
leftNum++;
result -= weight[i];
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
ArrayList<String> all = new ArrayList<>();
ArrayList<Integer> hours = new ArrayList<>();
ArrayList<Integer> minutes = new ArrayList<>();
// 注意,从0开始
for (int i = 0; i <= n; i++) {
hours.clear();
minutes.clear();
backtrack(hours,0,4,1,i);
int j = n-i;
backtrack(minutes,0,6,1,j);
for (Integer hour : hours) {
for (Integer minute : minutes) {
String s = minute.toString();
if(s.length()==1){
s = "0"+s;
}
String temp = hour.toString()+":"+s;
all.add(temp);
}
}
}
// 为了输出顺序保持一致,可不加
Collections.sort(all, new Comparator<String>() { @Override public int compare(String o1, String o2) {
if(o1.length()<o2.length())return 1;
String[] a = o1.split(":");
String[] b = o2.split(":");
if(Integer.parseInt(a[0])<Integer.parseInt(b[0])){
return 1;
}
if((Integer.parseInt(a[0])==Integer.parseInt(b[0]) )&&
Integer.parseInt(a[1])<Integer.parseInt(b[1])){
return 1;
}
return -1;
}
});
System.out.println(all);
}
}
#中兴笔试0829##中兴笔试题难度不一样,你ac了吗#