2014
02-13

Molecular Weight Calculator

Even though not good at memorizing, Alex is fond of Chemistry. When solving problems, he always needs to calculate the
molecular weight and quantity of each kind of elements. Lazy Alex hates that and turns to you for help.

Given a chemical formula, calculate its molecular weight and quantity of each kind of elements.

The atom is represented by either a single capital letter or a capital letter and a lower case letter. E.g. C, Ca, H, O. Formulas
may contain brackets: ‘(‘,’)', which is always followed by the quantity of that atomic mission. The number after an atom or a
bracket represents the quantity of that atomic mission. E.g. CH4, CaCO3, Fe(SCN)3. Brackets may be nested.

The input consists of multiple test cases. The first line of input contains an integer T, which is the number of test cases.

Each test case is on several lines. The first line contains an integer, N, indicating the quantity of known elements.
Each of the following n lines is consisting of a element description, start with a element name and followed by the weight
W (floating point number), separated by a space.
The (n+1)-th line consists an integer M, indicating the quantity of formulas.
Each of the following M lines is consisting of a formula.

[Technical Specification]
1.T is an integer, and T <=10.
2.N is an integer, and 1 <= N <= 200.
3.W is a floating point number with two fractional digits, and 1 <= W <= 400.
4.M is an integer, and 1 <= M <= 100.
5.Numbers in the formula is always in the range of [2,9] inclusive.
6.The formula?s length does not exceed 500.
7.There’s no blank line between two test cases.

The input consists of multiple test cases. The first line of input contains an integer T, which is the number of test cases.

Each test case is on several lines. The first line contains an integer, N, indicating the quantity of known elements.
Each of the following n lines is consisting of a element description, start with a element name and followed by the weight
W (floating point number), separated by a space.
The (n+1)-th line consists an integer M, indicating the quantity of formulas.
Each of the following M lines is consisting of a formula.

[Technical Specification]
1.T is an integer, and T <=10.
2.N is an integer, and 1 <= N <= 200.
3.W is a floating point number with two fractional digits, and 1 <= W <= 400.
4.M is an integer, and 1 <= M <= 100.
5.Numbers in the formula is always in the range of [2,9] inclusive.
6.The formula?s length does not exceed 500.
7.There’s no blank line between two test cases.

1
7
H 1.00
C 12.00
O 16.00
N 14.00
S 32.00
Ca 40.00
Fe 56.00
4
CO2
CH4
CaCO3
Fe(SCN)3 

44.00 C[1] O[2]
16.00 H[4] C[1]
100.00 C[1] O[3] Ca[1]
230.00 C[3] N[3] S[3] Fe[1] 

http://acm.hit.edu.cn/hoj/problem/view?id=2695

#include <stdio.h>
#include <math.h>

double solve_t(double d, double h);
double solve_d(double t, double h);
double solve_h(double t, double d);

int main()
{
double num1, num2;
char ch1, ch2;
double t, d, h;

while (scanf("%c", &ch1)!=EOF)
{
getchar();
if (ch1 == 'E')
break;
scanf("%lf %c %lf", &num1, &ch2, &num2);
getchar();
if (ch1 == 'T' && ch2 == 'D')
{
t = num1, d = num2;
h = solve_h(t, d);
}
else if(ch1 == 'D' && ch2 == 'T')
{
t = num2, d = num1;
h = solve_h(t, d);
}

else if(ch1 == 'T' && ch2 == 'H')
{
t = num1, h = num2;
d = solve_d(t, h);
}
else if(ch1 == 'H' && ch2 == 'T')
{
t = num2, h = num1;
d = solve_d(t, h);
}

else if(ch1 == 'D' && ch2 == 'H')
{
d = num1, h = num2;
t = solve_t(d, h);
}
else if(ch1 == 'H' && ch2 == 'D')
{
d = num2, h = num1;
t = solve_t(d, h);
}
printf("T %.1lf D %.1lf H %.1lf\n", t, d, h);
}

return 0;
}

double solve_t(double d, double h)
{
double e, t;

e = 6.11 * exp (5417.7530 * ((1/273.16) - (1/(d+273.16))));
t = h - (0.5555)*(e - 10.0);
return t;
}

double solve_d(double t, double h)
{
double e, d;

e = (h - t)/0.5555 +10;
d = 1 / (1 / 273.16 - ( log(e) - log(6.11) ) / 5417.7530) - 273.16;
return d;
}

double solve_h(double t, double d)
{
double e, h;

e = 6.11 * exp (5417.7530 * ((1/273.16) - (1/(d+273.16))));
h = t + (0.5555)*(e - 10.0);
return h;
}

1. 这道题目虽然简单，但是小编做的很到位，应该会给很多人启发吧！对于面试当中不给开辟额外空间的问题不是绝对的，实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟，今天看到小编这篇文章相见恨晚。

2. bottes vernies blanches

I appreciate the efforts you men and women place in to share blogs on such sort of matters, it was certainly useful. Keep Posting!