我正在编写一个编码项目,我对程序的速度有问题。该程序输入1到80之间的输入,此输入表示许多匹配杆,并输出可以使用该数量的匹配杆制作多少个不同的数字。
Ex:数字1可以用2根火柴棍形成,数字2需要5根火柴棒
以下是该计划的完整提示:http://i.imgur.com/z93R4Ia.png
提示程序
以下是我提出的用于计算所有可能输出的算法的代码,它对于输入的低端功能相当好,尽管对于大输入(如80小时计算所有输入)而言效率非常低可能性。如何将这个时间减少到最低限度?最好能在不到一分钟的时间内执行..我被告知缓存是一种解决方案,虽然我不太了解缓存,也不知道如何在这个问题中实现缓存
n代表输入,所有Counter对象都跟踪创建的每个可能的数字
public class Lab1 {
/**
* Counts the number of possible different numbers that can be made with n
* number of match sticks.
*
* @param n
* represents the input number of match sticks.
* @param count
* Counter object that keeps track of the number of possible
* numbers that can be made.
* @param scaleValue
* accounts for multiple numbers being made out of the same
* number, n, match sticks.
*/
public static void digitCounter(int n, Counter count, int scaleValue) {
if (n < 2) {
// System.out.println(n + "matchs can create " + count.getCount()
// + " different scaleValuebers.");
} else {
if (count.getCount() == 0) {
// If there are enough match sticks to form it
// accounts for 0 only once
if (n >= 7) {
// counts 0
count.setCount(10);
// counts 1
digitCounter(n - 2, count, 1);
// count 4
digitCounter(n - 4, count, 1);
// counts 6
digitCounter(n - 6, count, 1);
// counts 7
digitCounter(n - 3, count, 1);
// counts 8
digitCounter(n - 7, count, 1);
// counts 2, 3, 5 and 9
digitCounter(n - 5, count, 4);
} else if (n == 6) {
count.setCount((16 * scaleValue));
} else if (n == 5) {
count.setCount((10 * scaleValue));
} else if (n == 4) {
count.setCount((4 * scaleValue));
} else if (n == 3) {
count.setCount((2 * scaleValue));
} else if (n == 2) {
count.setCount((1 * scaleValue));
}
}
// Accounts for every other scaleValueber after 0 is accounted for
// so
// scaleValuebers with leading 0's are not formed
// Ex: 001 is illegal
else {
if (n >= 7) {
count.setCount(count.getCount() + (10 * scaleValue));
digitCounter(n - 6, count, scaleValue);
digitCounter(n - 2, count, scaleValue);
digitCounter(n - 4, count, scaleValue);
digitCounter(n - 6, count, scaleValue);
digitCounter(n - 3, count, scaleValue);
digitCounter(n - 7, count, scaleValue);
digitCounter(n - 5, count, (scaleValue * 4));
} else if (n == 6) {
count.setCount(count.getCount() + (16 * scaleValue));
} else if (n == 5) {
count.setCount(count.getCount() + (10 * scaleValue));
} else if (n == 4) {
count.setCount(count.getCount() + (4 * scaleValue));
} else if (n == 3) {
count.setCount(count.getCount() + (2 * scaleValue));
} else if (n == 2) {
count.setCount(count.getCount() + (1 * scaleValue));
}
}
}
}
public static void main(String[] args) throws IOException {
Counter c = new Counter();
int scale = 1;
int input = Integer.parseInt(args[1]);
digitCounter(input, c, scale);
int output = c.getCount();
System.out.print("With a match stick number of " + input + ", "
+ output + " different numbers can be made");
}
}
答案 0 :(得分:1)
非常有趣的问题。而领先的0,它也很棘手。
缓存肯定可以帮助减少这个问题的时间。如果你肯定那你的 当前程序正在生成正确的答案,然后添加缓存非常简单。
声明/分配一个足够大的数组来保存所有匹配计数,直到你正在计算的数量(你说多达80,所以你可以使用81的数组来简化)。 在你的digitCounter方法中,你要做的第一件事是检查插槽n处的数组:如果它有内容,立即返回。否则,在它返回之前,将你得到的结果存储在数组中。这避免了大量的重新计算,并且应该在更短的时间内为大量的比赛提供答案。
PS:5场比赛你有10个数字:你确定吗?
答案 1 :(得分:0)
这是我的代码执行得非常快,虽然我有问题,输出稍微关闭,我不确定它的缓存是否出错?
/**
* Counts the number of possible different numbers that can be made with n
* number of match sticks.
*
* @param input
* represents the input number of match sticks.
* @param count
* Counter object that keeps track of the number of possible
* numbers that can be made.
* @param scaleValue
* accounts for multiple numbers being made out of the same
* number, n, match sticks. Such as 2, 3, 5, and 9 all take 5
* match sticks.
*/
public static void digitCounter(int input, Counter count, int scaleValue) {
if (input > 0) {
if (cachedData[input - 1] == -1) {
if (input < 2) {
// Leaves recursive loop
} else {
if (count.getCount() == 0) {
// If there are enough match sticks to form it
// accounts for 0 only once
if (input >= 7) {
// counts 0
count.setCount(10);
// counts 1
digitCounter(input - 2, count, 1);
// count 4
digitCounter(input - 4, count, 1);
// counts 6
digitCounter(input - 6, count, 1);
// counts 7
digitCounter(input - 3, count, 1);
// counts 8
digitCounter(input - 7, count, 1);
// counts 2, 3, 5 and 9
digitCounter(input - 5, count, 4);
} else if (input == 6) {
count.setCount((16 * scaleValue));
} else if (input == 5) {
count.setCount((10 * scaleValue));
} else if (input == 4) {
count.setCount((4 * scaleValue));
} else if (input == 3) {
count.setCount((2 * scaleValue));
} else if (input == 2) {
count.setCount((1 * scaleValue));
}
}
// Accounts for every other number after 0 is accounted for
// so
// numbers with leading 0's are not formed
// Ex: 001 is illegal
else {
if (input >= 7) {
count.setCount(count.getCount() + (10 * scaleValue));
digitCounter(input - 6, count, scaleValue);
digitCounter(input - 2, count, scaleValue);
digitCounter(input - 4, count, scaleValue);
digitCounter(input - 6, count, scaleValue);
digitCounter(input - 3, count, scaleValue);
digitCounter(input - 7, count, scaleValue);
digitCounter(input - 5, count, (scaleValue * 4));
} else if (input == 6) {
count.setCount(count.getCount() + (16 * scaleValue));
} else if (input == 5) {
count.setCount(count.getCount() + (10 * scaleValue));
} else if (input == 4) {
count.setCount(count.getCount() + (4 * scaleValue));
} else if (input == 3) {
count.setCount(count.getCount() + (2 * scaleValue));
} else if (input == 2) {
count.setCount(count.getCount() + (1 * scaleValue));
}
}
}
} else {
count.setCount((cachedData[input - 1] * scaleValue)
+ count.getCount());
}
}