为什么我的HashMap没有填充

时间:2014-01-13 14:42:01

标签: java hashmap

我的代码用于查找整数的阶乘达到100.我正在使用BigInteger,但我的问题是我的HashMap没有被填充。

public class FCTRL2 {   
static Map<Integer,BigInteger> list = new HashMap<Integer,BigInteger>();

  public static void main(String[] args){
        InputStreamReader read = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(read);
        int numberOfInput=0;
        String input=null;
        try {
            numberOfInput = Integer.parseInt(in.readLine());
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        while(numberOfInput > 0){
            try {
                input = in.readLine();
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            BigInteger inputBig = new BigInteger(input);
            **System.out.println(factorial(inputBig));** prints correct factorial
        **System.out.println(list.get(factorial(inputBig).intValue()));** prints null
            numberOfInput--;
        }
  }

  public static BigInteger factorial(BigInteger input){
    if(list.containsKey(input.intValue()))
        return list.get(input.intValue());

    if(input.equals(new BigInteger("1")))
        return new BigInteger("1");
    BigInteger output;
    output = input.multiply(factorial(input.subtract(new BigInteger("1"))));
    list.put(input.intValue(), output);
    return output;
  }

}

3 个答案:

答案 0 :(得分:2)

替换

System.out.println(list.get(factorial(inputBig).intValue()));

System.out.println(list.get(inputBig.intValue()));

因为您要在此处插入值来映射:list.put(input.intValue(), output);

答案 1 :(得分:0)

试试这个

public static BigInteger factorial(BigInteger input) {
    if (list.containsKey(input.intValue()))
        return list.get(input.intValue());

    if (input.equals(BigInteger.ONE)) {
        list.put(input.intValue(), BigInteger.ONE);
        return BigInteger.ONE;  
    }
    BigInteger output = input.multiply(factorial(input.subtract(BigInteger.ONE)));
    list.put(input.intValue(), output);
    return output;
}

numberOfInput while循环...

之后添加打印件
for (int key : list.keySet()) {
    System.out.println(list.get(key));
}

似乎正确输出

1
10
3628800
1
2
6
24
120
720
5040
40320
362880
3628800

答案 2 :(得分:0)

我做了一个测试,首先简化读取数据,专注于逻辑:

我使用BigInteger.valueOf()。

public class FCTRL2 {   

  public static void main(String[] args){

        InputStreamReader read = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(read);
        Scanner scanner = new Scanner(in);
        //simplify read data first ,here focus on calculation logic
        while(scanner.hasNext()) {
             long val = scanner.nextLong();
             BigInteger inputBig = BigInteger.valueOf(val);
             System.out.println(factorial(inputBig));
        }
        scanner.close();
  }

  public static BigInteger factorial(BigInteger input){
    if(map.containsKey(input.intValue())) {
        System.out.println("from map");
         return map.get(input.intValue());
    }
    if(input.equals(new BigInteger("1")))
        return new BigInteger("1");
    BigInteger output;
    output = input.multiply(factorial(input.subtract(new BigInteger("1"))));
    map.put(input.intValue(), output);
    System.out.println("from new calculating");
    return output;
  }
  private static Map<Integer,BigInteger> map = new HashMap<Integer,BigInteger>();
}

查看输出,似乎地图缓存了一些数据

这是你想要的逻辑吗?