我正在练习CCC 2017 J4问题,但遇到了时间限制错误。我的代码超出了问题的时间限制(1秒)。问题链接如下(见问题J4): CCC 2017
我还附上了问题的代码:
public static void main(String[] args) {
int starttime = new Scanner(System.in).nextInt();
int hr = 12, hr1 = 0, hr2 = 0;
int mn = 1, mn1 = 0, mn2 = 0;
int counter = 0;
int diff1 = 0, diff2 = 0, diff3 = 0;
for (int i = 0; i < starttime; i++, mn++) {
if (mn >= 60) {
mn -= 60;
if (hr + 1 < 13) {
hr += 1;
} else {
hr = (hr + 1) % 12;
}
}
mn1 = mn % 10;
mn2 = mn / 10 % 10;
hr1 = hr % 10;
hr2 = hr / 10 % 10;
diff1 = mn1 - mn2;
diff2 = hr1 - hr2;
diff3 = mn2 - hr1;
if (hr2 == 0) {
if (diff1 == diff3) {
counter += 1;
}
} else if (diff1 == diff2 && diff2 == diff3) {
counter += 1;
}
}
System.out.println(counter);
}
答案 0 :(得分:2)
通常有两种方法可以提高代码的速度。如果您不熟悉Big O表示法,请花一点时间阅读它。
答案 1 :(得分:1)
有一个更好的算法是O(1)。你只需找到一个聪明的解决方案就可以了。
这是一个javascript代码段:
let seriesInMinutesFrom12oClock = [34]; // You'll need an linked list in java (or any type of list)
// So 12:34 is 34min and 1:23 and 83min ...
// Lets start with 12:34 already in the array since it's the only serie in the 12:00-12:59 timespan. Now we
// don't even have to deal with the fact that 12 really means zero.
for(let hours = 1; hours < 12; hours++) { // All the hours from 1 to 11 (no need to do 12, it's already done)
for(let minutes = 0; minutes < 60; minutes++) { // 0 to 59
let hoursTens = parseInt(hours / 10, 10); // Java equivalent of " int hoursTens = (int) (hours/10); "
let hoursDigits = hours % 10;
let minutesTens = parseInt(minutes / 10, 10);
let minutesDigits = minutes % 10;
let isArithmeticSerie = false;
if (hoursTens == 0) {
// Example (1:23)
// 3 - 2 == 2 - 1
isArithmeticSerie = (minutesDigits - minutesTens) == (minutesTens - hoursDigits);
} else {
// Example (12:34)
// (4 - 3) == (3 - 2) && (3 - 2) == (2 - 1)
isArithmeticSerie = (minutesDigits - minutesTens) == (minutesTens - hoursDigits) && (minutesTens - hoursDigits) == (hoursDigits - hoursTens);
}
if (isArithmeticSerie)
seriesInMinutesFrom12oClock.push( // Add to list of series we found
hoursTens * 600 +
hoursDigits * 60 +
minutesTens * 10 +
minutesDigits
);
}
}
// Now you have your list for one cycle of 12hrs... You do that at the beginning before you ask for the input.
// Your program can start reading user input
let numberOfMinutesToObserve = 180; // minutes (read from System.in in java)
let numberOf12HrsCycles = numberOfMinutesToObserve / (12 * 60);
let numberOfFullCycles = parseInt(numberOf12HrsCycles, 10);
let leftOverMinutes = (numberOf12HrsCycles - numberOfFullCycles) * 12 * 60; // Last incomplete cycle (in minutes)
// The number of series is the number of full cycles times the number of series by cycles
let numberOfSeries = seriesInMinutesFrom12oClock.length * numberOfFullCycles;
// Add to it the number of arithmetic series that are in the last (incomplete) cycle of 12hrs
numberOfSeries += seriesInMinutesFrom12oClock.filter(mins => mins <= leftOverMinutes).length;
// The above line means: "find the number of series found before the end of the last cycle".
console.log(numberOfSeries);
&#13;