public class Test {
public static void main(String[] args) {
int result = 0;
for (int hour = 0; hour < 24; hour++) {
for (int min = 0; min < 60; min++) {
String clock = "";
String sHour = "";
String sMin = "";
sHour = hour < 10 ? "0" + String.valueOf(hour) : String.valueOf(hour); //小时低于10,例如 1点,应该是“01”
sMin = min < 10 ? "0" + String.valueOf(min) : String.valueOf(min); //分钟低于10,例如 2分,应该是“02”
clock = sHour + sMin; //模拟时钟,例如:0112代表一点十二分
//若有连着三个相等的字符,统计结果+1
if (repet(clock)) {
result++;
}
}
}
System.out.println(result); //输出结果
}
/**
* 判断四位数的字符串是否有连着三个字符相等
* @param s
* @return
*/
public static boolean repet(String s){
if((s.charAt(0) == s.charAt(1) && s.charAt(1) == s.charAt(2)) ||
(s.charAt(1) == s.charAt(2) && s.charAt(2)== s.charAt(3))
) {
return true;
} else {
return false;
}
}
}
由此可见,答案确实为43
public static void main(String[] args) {
int result = 0;
for (int hour = 0; hour < 24; hour++) {
for (int min = 0; min < 60; min++) {
String clock = "";
String sHour = "";
String sMin = "";
sHour = hour < 10 ? "0" + String.valueOf(hour) : String.valueOf(hour); //小时低于10,例如 1点,应该是“01”
sMin = min < 10 ? "0" + String.valueOf(min) : String.valueOf(min); //分钟低于10,例如 2分,应该是“02”
clock = sHour + sMin; //模拟时钟,例如:0112代表一点十二分
//若有连着三个相等的字符,统计结果+1
if (repet(clock)) {
result++;
}
}
}
System.out.println(result); //输出结果
}
/**
* 判断四位数的字符串是否有连着三个字符相等
* @param s
* @return
*/
public static boolean repet(String s){
if((s.charAt(0) == s.charAt(1) && s.charAt(1) == s.charAt(2)) ||
(s.charAt(1) == s.charAt(2) && s.charAt(2)== s.charAt(3))
) {
return true;
} else {
return false;
}
}
}
由此可见,答案确实为43