Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 效率问题_Performance - Fatal编程技术网

Performance 效率问题

Performance 效率问题,performance,Performance,我正在做一个编码项目,程序的速度有问题。程序接受1到80之间的输入,该输入表示匹配杆的数量,并输出使用该数量的匹配杆可以生成多少不同的数字 例:1号可以由2根火柴棍组成,2号需要5根火柴棍 以下是程序的完整提示: 程序提示 这是我提出的算法的代码,用于计算所有可能的结果,它对低端输入的性能相当好,尽管对于大型输入(如80小时计算所有可能性)来说效率非常低。我怎样才能把这段时间减到最少?最好是在一分钟内执行的地方。。我被告知缓存是一种解决方案,尽管我不太了解缓存,也不知道如何在这个问题上实现它 n

我正在做一个编码项目,程序的速度有问题。程序接受1到80之间的输入,该输入表示匹配杆的数量,并输出使用该数量的匹配杆可以生成多少不同的数字

例:1号可以由2根火柴棍组成,2号需要5根火柴棍

以下是程序的完整提示:

程序提示

这是我提出的算法的代码,用于计算所有可能的结果,它对低端输入的性能相当好,尽管对于大型输入(如80小时计算所有可能性)来说效率非常低。我怎样才能把这段时间减到最少?最好是在一分钟内执行的地方。。我被告知缓存是一种解决方案,尽管我不太了解缓存,也不知道如何在这个问题上实现它

n表示输入,计数器对象所做的就是跟踪创建的每个可能的数字

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");
    }
}
公共类Lab1{
/**
*计算可以使用n生成的可能不同的数字的数量
*火柴棒的数量。
* 
*@param n
*表示匹配条的输入数量。
*@param计数
*计数器对象,用于跟踪可能的
*可以制作的数字。
*@param scaleValue
*由同一个数字组成的多个数字的帐户
*数字,n,火柴棒。
*/
公共静态void数字计数器(int n,计数器计数,int scaleValue){
if(n<2){
//System.out.println(n+“匹配项可以创建”+count.getCount()
//+“不同的标度值。”);
}否则{
if(count.getCount()==0){
//如果有足够的火柴棒来形成它
//只有一次帐户为0
如果(n>=7){
//计数0
计数。设置计数(10);
//计数1
数字计数器(n-2,计数,1);
//第四点
数字计数器(n-4,计数,1);
//计数6
数字计数器(n-6,计数,1);
//第7条
数字计数器(n-3,计数,1);
//计数8
数字计数器(n-7,计数,1);
//计数2、3、5和9
数字计数器(n-5,计数,4);
}else如果(n==6){
count.setCount((16*scaleValue));
}else如果(n==5){
count.setCount((10*scaleValue));
}else如果(n==4){
count.setCount((4*scaleValue));
}else如果(n==3){
count.setCount((2*scaleValue));
}else如果(n==2){
count.setCount((1*scaleValue));
}
}
//计算0后每隔一个scaleValueber的帐户
//所以
//未形成前导为0的ScaleValuer
//例:001是非法的
否则{
如果(n>=7){
count.setCount(count.getCount()+(10*scaleValue));
数字计数器(n-6,计数,标度值);
数字计数器(n-2,计数,标度值);
数字计数器(n-4,计数,标度值);
数字计数器(n-6,计数,标度值);
数字计数器(n-3,计数,标度值);
数字计数器(n-7,计数,标度值);
数字计数器(n-5,计数,(标度值*4));
}else如果(n==6){
count.setCount(count.getCount()+(16*scaleValue));
}else如果(n==5){
count.setCount(count.getCount()+(10*scaleValue));
}else如果(n==4){
count.setCount(count.getCount()+(4*scaleValue));
}else如果(n==3){
count.setCount(count.getCount()+(2*scaleValue));
}else如果(n==2){
count.setCount(count.getCount()+(1*scaleValue));
}
}
}
}
公共静态void main(字符串[]args)引发IOException{
计数器c=新计数器();
int标度=1;
int input=Integer.parseInt(args[1]);
数字计数器(输入、c、刻度);
int output=c.getCount();
System.out.print(“使用匹配条编号“+输入+”,”
+输出+“可以制作不同的数字”);
}
}

非常有趣的问题。领先的0也很棘手

缓存当然有助于缩短解决此问题的时间。如果您确信您的 当前程序正在生成正确的答案,然后添加缓存就相当简单了

声明/分配一个足够大的数组,以容纳所有匹配的计数,直到您正在计算的值为止(您说最多80个,因此您可以使用81个数组来简化计算)。 在digitCounter方法中,您要做的第一件事是检查插槽n处的数组:如果数组中有内容,请立即返回。否则,在返回之前,将n的结果存储在数组中。这样可以避免大量的重新计算,并且可以在更短的时间内为大量匹配提供答案


PS:你有10个数字来匹配5个匹配项:你确定吗?

这是我的代码,执行速度很快,尽管我有一个问题,输出稍微有点不稳定,我不确定缓存是否出了问题

/**
 * 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());
        }
    }
/**
*计算可以使用n生成的可能不同的数字的数量
*火柴棒的数量。
* 
*@param输入
*表示输入n