题目1 : The Lastest Time
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
What is latest time you can make with 4 digits A, B, C and D?
For example if the 4 digits are 1, 0, 0, 0, you can make 4 times with them: 00:01, 00:10, 01:00, 10:00. The lastest time will be 10:00. Note a valid time is between 00:00 and 23:59.
输入
One line with 4 digits A, B, C and D, separated by a space. (0 <= A, B, C, D <= 9)
输出
Output the lastest time in the format "hh:mm". If there is no valid time output NOT POSSIBLE.
样例输入
0 9 0 0
样例输出
09:00
import java.util.Scanner;
public class Main_207 {
private static int[] maxTime = new int[]{0,0,0,0};
private static boolean hasValidate= false;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] nums = new int[4];
for(int i = 0; i < 4; i ++){
nums[i] = sc.nextInt();
}
System.out.println(solve(nums));
sc.close();
}
public static String solve(int[] nums){
permute(nums, 0);
if(hasValidate){
return String.format("%d%d:%d%d", maxTime[0],maxTime[1], maxTime[2],maxTime[3]);
}else
return "NOT POSSBLE";
}
public static void permute(int []nums, int start){
if(start == 4){
if(!isValid(nums)){
return ;
}
hasValidate = true;
if(lessTo(maxTime , nums)){
System.arraycopy(nums,0,maxTime, 0 ,nums.length);
}
}
for(int i = start ; i < 4; i++){
swap(nums,start,i);
permute(nums, start+1);
swap(nums,start,i);
}
}
public static void swap(int[]a, int i, int j){
if(i == j ) return ;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
public static boolean isValid(int []time){
int hh = time[0] * 10 + time[1];
int mm = time[2] * 10 + time[3];
if(hh < 24 && mm < 60){
return true;
}else{
return false;
}
}
public static boolean lessTo(int[]t1, int[]t2){
int m1= (t1[0] * 10 + t1[1]) * 60 + t1[2]* 10 + t1[3];
int m2 = (t2[0] * 10 + t2[1]) * 60 + t2[2]* 10 + t2[3];
if(m1 < m2){
return true;
}
return false;
}
}