十进制扩展程序运行的大投入非常慢 [英] Decimal expansion program running very slow for large inputs

查看:144
本文介绍了十进制扩展程序运行的大投入非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个程序来计算数量的十进制扩展三万三千一百零二分之一十万三千九百九十三,我想打印出所有这取决于数量的用户输入尾随小数。它运行快的所有数达 10 ^ 5 ,但如果输入 10 ^ 6 来计划大约需要5分钟打印出一个答案。我怎么能加快速度?我已经尝试了两种不同的方法使用的BigDecimal ,另一个使用字符串和没有一个是工作效率。

 公共静态无效的主要(字串[] args)抛出NumberFormatException异常,
        IOException异常{
    // BigDecimal的NUM1 =新的BigDecimal(103993);
    // BigDecimal的NUM2 =新的BigDecimal(33102);
    字符串repNum =415926530119026040722614947737296840070086399613316;
    // pw.println(num.toString());
    字符串SNUM =3.1;
    // pw.println(repNum.length());
    int的情况下的Integer.parseInt =(br.readLine());
    INT分解;
    的for(int i = 0; I<案件;我++){
        SNUM =3.1;
        DEC =的Integer.parseInt(br.readLine());

        如果(月== 0)
            pw.println(3);
        否则,如果(DEC或LT = 52){
            SNUM + = repNum.substring(0,DEC  -  1);
            pw.println(SNUM);
        } 其他 {
            而(DEC或GT; 52){
                SNUM + = repNum;
                十二月 -  = 51;
            }
            SNUM + = repNum.substring(0,DEC  -  1);
            pw.println(SNUM);

        }

        // pw.println(num1.divide(NUM2,DEC,
        // RoundingMode.FLOOR)的ToString());
    }
}
 

而不是创造的数字的长字符串的解决方案

刚刚打印出来的数字。例如:

 而(DEC或GT; 52){
            System.out.print(repNum);
            十二月 -  = 51;
        }
        pw.println(repNum.substring(0,DEC  -  1));
 

通过将创建一个长字符串的循环性能非常糟糕,因为字符串是不可变的。程序花费所有时间到新创建新的字符串,一个比另一个长,和复制从旧的字符,基本上执行的倒楣的画家算法

I am writing a program to calculate the decimal expansion on the number 103993/33102 and I want to print out all of the trailing decimals depending on what number the user inputs. It runs quickly for all number up to 10^5 but if input 10^6 to program takes around 5 minutes to print out an answer. How can I speed things up? I have tried two different approaches one using BigDecimal and the other using strings and neither one is working efficiently.

public static void main(String[] args) throws NumberFormatException,
        IOException {
    // BigDecimal num1 = new BigDecimal(103993);
    // BigDecimal num2 = new BigDecimal(33102);
    String repNum = "415926530119026040722614947737296840070086399613316";
    // pw.println(num.toString());
    String sNum = "3.1";
    // pw.println(repNum.length());
    int cases = Integer.parseInt(br.readLine());
    int dec;
    for (int i = 0; i < cases; i++) {
        sNum = "3.1";
        dec = Integer.parseInt(br.readLine());

        if (dec == 0)
            pw.println("3");
        else if (dec <= 52) {
            sNum += repNum.substring(0, dec - 1);
            pw.println(sNum);
        } else {
            while (dec > 52) {
                sNum += repNum;
                dec -= 51;
            }
            sNum += repNum.substring(0, dec - 1);
            pw.println(sNum);

        }

        // pw.println(num1.divide(num2, dec,
        // RoundingMode.FLOOR).toString());
    }
}

解决方案

Instead of creating a long string of digits just print out the digits. For example:

        while (dec > 52) {
            System.out.print(repNum);
            dec -= 51;
        }
        pw.println(repNum.substring(0, dec - 1));

Creating a long string in a loop by concatenating is really bad for performance because strings are immutable. The program spends all its time creating new strings, one longer than the other, and copying characters from the old to the new, essentially implementing Schlemiel the Painter's algorithm.

这篇关于十进制扩展程序运行的大投入非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆