首页 > ACM题库 > HDU-杭电 > HDU 3546-Calculator[解题报告]HOJ
2014
11-05

HDU 3546-Calculator[解题报告]HOJ

Calculator

问题描述 :

Golden Adobe is the most advanced computer for scientific computing in the world. Unfortunately, it broke down. Your task is to write a super computing software to take its place. The software contains 10 registers named from A to J. There are three operations on the registers:
1) Assignment: X=Y
2) Addition: X+=Y
3) Multiplication: X*=Y
Notice that X and Y are two registers, and they may be the same.
Initially, all the registers are stored by integer 1. Your program should operate several operations and output the final result for 10 registers. You may assume that the length of each decimal number stored in the register is no longer than 5000.

输入:

The input contains only one test case including several lines.
Each line contains an operation to be calculated.
The number of operations will no more than 300000.

输出:

The input contains only one test case including several lines.
Each line contains an operation to be calculated.
The number of operations will no more than 300000.

样例输入:

A+=B
A*=A
A+=A
B+=A
C+=B
D=B

样例输出:

8
9
10
9
1
1
1
1
1
1

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		BigInteger[] r = new BigInteger[300];
		BigInteger one = BigInteger.ONE, zero = BigInteger.ZERO;
		for (int i = 'A'; i <= 'J'; i++) {
        	r[i] = one;
        }
		Scanner cin = new Scanner(System.in);
        while (cin.hasNext()) {
        	char s[] = cin.next().toCharArray();
        	if (s.length == 3)  {
    			r[s[0]] = r[s[2]];
    		} else {
    			if (s[1] == '+') {
    				r[s[0]] = r[s[0]].add(r[s[3]]);
    			} else r[s[0]] = r[s[0]].multiply(r[s[3]]);
    		}
        }
        for (int i = 'A'; i <= 'J'; i++) {
        	System.out.println(r[i]);
        }		
	}

}

  1. 代码是给出了,但是解析的也太不清晰了吧!如 13 abejkcfghid jkebfghicda
    第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3),为什么要这样拆分,原则是什么?