首页 > 其他 > 详细

Uva1586

时间:2019-05-21 23:35:46      阅读:127      评论:0      收藏:0      [点我收藏+]

Molar mass UVA - 1586

An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compound is the mass of one mole of the organic compound. The molar mass of an organic compound can be computed from the standard atomic weights of the elements. When an organic compound is given as a molecular formula, Dr. CHON wants to ?nd its molar mass. A molecular formula, such as C3H4O3, identi?es each constituent element by its chemical symbol and indicates the number of atoms of each element found in each discrete molecule of that compound. If a molecule contains more than one atom of a particular element, this quantity is indicated using a subscript after the chemical symbol. In this problem, we assume that the molecular formula is represented by only four elements, ‘C’ (Carbon), ‘H’ (Hydrogen), ‘O’ (Oxygen), and ‘N’ (Nitrogen) without parentheses. The following table shows that the standard atomic weights for ‘C’, ‘H’, ‘O’, and ‘N’.
Atomic Name Carbon Hydrogen Oxygen Nitrogen Standard Atomic Weight 12.01 g/mol 1.008 g/mol 16.00 g/mol 14.01 g/mol
For example, the molar mass of a molecular formula C6H5OH is 94.108 g/mol which is computed by 6 × (12.01 g/mol) + 6 × (1.008 g/mol) + 1 × (16.00 g/mol). Given a molecular formula, write a program to compute the molar mass of the formula.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the ?rst line of the input. Each test case is given in a single line, which contains a molecular formula as a string. The chemical symbol is given by a capital letter and the length of the string is greater than 0 and less than 80. The quantity number n which is represented after the chemical symbol would be omitted when the number is 1 (2 ≤ n ≤ 99).
Output
Your program is to write to standard output. Print exactly one line for each test case. The line should contain the molar mass of the given molecular formula.

Input
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the ?rst line of the input. Each test case is given in a single line, which contains a molecular formula as a string. The chemical symbol is given by a capital letter and the length of the string is greater than 0 and less than 80. The quantity number n which is represented after the chemical symbol would be omitted when the number is 1 (2 ≤ n ≤ 99).
Output
Your program is to write to standard output. Print exactly one line for each test case. The line should contain the molar mass of the given molecular formula.
Sample Input
4 C C6H5OH NH2CH2COOH C12H22O11
Sample Output
12.010 94.108 75.070 342.296

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define maxn 100100
 4 using namespace std;
 5 
 6 double sum,w[256];
 7 
 8 int main()
 9 {
10     w[C]=12.01,w[H]=1.008,w[O]=16.00,w[N]=14.01;
11     int n;
12     scanf("%d",&n);
13     while(n--)
14     {
15         char a[105];
16         double sum=0.0;
17         scanf("%s",a);
18         int len=strlen(a);
19         for(int i=0;i<len;i++)
20         {
21             int si=0;
22             if(w[a[i]])
23             {
24                 if(a[i+1]<0||a[i+1]>9)
25                     sum+=w[a[i]];
26             else
27             {
28                 int no=i+1;
29                 while(a[no]>=1&&a[no]<=9)
30                     si=si*10+a[no++]-0;
31                 sum+=si*w[a[i]];
32             }
33         }
34     } printf("%.3lf\n",sum);
35     }
36     return 0;
37 }

思路:

一开始不知道怎么处理元素后面没有数字的情况。

看了大佬的博客,发现可以根据遍历char数组,如果字符不是字母,继续向后遍历;

如果字符是字母,则:该字符后面是字母则加上一次该字母的权值,

                                     该字符后面是数字则用no=i+1;作为游标,如果是一直向后遍历,直到碰到字母,计算数字的大小。

注意点:

想到一个问题,如果当i==len-1;&&a[len-1]是字母,那么a[i+1]是什么呢?a[i+1]位置没有赋值,后面看了大佬的博客,知道了

字符数组在部分初始化后,后面的元素自动为赋值‘\0‘,出现上述情况进入else,不满足while(a[no]>=1‘&&a[no]<=9)的情况,i++后i==len不满足i<len的条件,可以跳出循环啦!

 

Uva1586

原文:https://www.cnblogs.com/zuiaimiusi/p/10903197.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!