📄 chem_momodi.java
字号:
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Fen {
static Scanner cin = new Scanner(System.in);
static int n;
static double[] wei;
static double[][] wei2;
static BigInteger[] cnt;
static BigInteger[][] cnt2;
static String[] inp;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int ca = cin.nextInt();
while (ca-- > 0) {
n = cin.nextInt();
if (n == -1) {
break;
}
wei = new double[26];
wei2 = new double[26][26];
inp = new String[n];
for (int i = 0; i < n; ++i) {
String s = cin.next();
inp[i] = s;
double add = cin.nextDouble();
if (s.length() == 1) {
wei[s.charAt(0) - 'A'] = add;
} else {
wei2[s.charAt(0) - 'A'][s.charAt(1) - 'a'] = add;
}
}
int m = cin.nextInt();
while (m-- > 0) {
cnt = new BigInteger[26];
cnt2 = new BigInteger[26][26];
for (int i = 0; i < 26; ++i) {
cnt[i] = V(0);
for (int j = 0; j < 26; ++j) {
cnt2[i][j] = V(0);
}
}
String s = cin.next();
int[] sta = new int[s.length()];
int[] val = new int[s.length()];
int L = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == '(') {
sta[L++] = i;
} else if (s.charAt(i) == ')') {
val[sta[--L]] = getInt(s, i + 1);
}
}
BigDecimal[] tmp = new BigDecimal[s.length()];
L = 1;
tmp[0] = D(1);
BigDecimal ans = D(0);
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == '(') {
tmp[L] = tmp[L - 1].multiply(D(val[i]));
++L;
} else if (s.charAt(i) == ')') {
--L;
} else if (isUpper(s.charAt(i))) {
if (i + 1 < s.length() && isLower(s.charAt(i + 1))) {
ans = ans.add(D(wei2[s.charAt(i) - 'A'][s.charAt(i + 1) - 'a']).multiply(
tmp[L - 1]).multiply(D(getInt(s, i + 2))));
cnt2[s.charAt(i) - 'A'][s.charAt(i + 1) - 'a'] = cnt2[s.charAt(i) - 'A'][s.charAt(i + 1) - 'a'].add(tmp[L - 1].multiply(D(getInt(s, i + 2))).toBigInteger());
} else {
ans = ans.add(D(wei[s.charAt(i) - 'A']).multiply(tmp[L - 1]).multiply(
D(getInt(s, i + 1))));
cnt[s.charAt(i) - 'A'] = cnt[s.charAt(i) - 'A'].add(tmp[L - 1].multiply(D(getInt(s, i + 1))).toBigInteger());
}
}
}
System.out.printf("%.2f", ans);
for (int i = 0; i < n; ++i) {
if (inp[i].length() == 1) {
BigInteger k = cnt[inp[i].charAt(0) - 'A'];
if (k.signum() > 0) {
System.out.print(" " + inp[i] + "[" + k + "]");
}
} else {
BigInteger k = cnt2[inp[i].charAt(0) - 'A'][inp[i].charAt(1) - 'a'];
if (k.signum() > 0) {
System.out.print(" " + inp[i] + "[" + k + "]");
}
}
}
System.out.println();
}
}
}
static BigDecimal D(double d) {
return BigDecimal.valueOf(d);
}
static BigInteger V(int v) {
return BigInteger.valueOf(v);
}
static boolean isUpper(char s) {
return s >= 'A' && s <= 'Z';
}
static boolean isLower(char s) {
return s >= 'a' && s <= 'z';
}
static boolean isDigit(char s) {
return s >= '0' && s <= '9';
}
static int getInt(String s, int w) {
if (w >= s.length() || !isDigit(s.charAt(w))) {
return 1;
}
int ans = 0;
for (int i = w; i < s.length(); ++i) {
if (isDigit(s.charAt(i))) {
ans = ans * 10 + s.charAt(i) - '0';
} else {
break;
}
}
return ans;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -