UVa1586 Molar Mass
2015-08-23 22:47
423 查看
Input
Your program is to read from standard input. The input consists ofT test cases. The number of test cases T is given in the first 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#include <iostream> #include <string.h> using namespace std; #define maxn 100 float molar(char c) { if(c == 'C') return 12.01; else if(c == 'H') return 1.008; else if(c == 'O') return 16.00; else if(c == 'N') return 14.01; } int main() { int T; cin >> T; while(T--) { char s[maxn]; float total = 0; scanf("%s", s); for(int i = 0; i < strlen(s); i++) { if(s[i] == 'C' || s[i] == 'H' || s[i] == 'O' || s[i] == 'N') total += molar(s[i]); else { if((s[i] <= '9' && s[i] >= '1') && (s[i+1] <='0' || s[i+1] > '9')) total += molar(s[i-1]) * (s[i]-'0'-1);//注意ASCII码中数字与正常数字,应减去'0' else { total += molar(s[i-1]) * ((s[i]-'0')*10 + (s[i+1]-'0'-1)); //cout << ((s[i]-'0')*10 + (s[i+1]-'0'-1)) << endl; i++; } } } printf("%.3f\n", total); } return 0; }
相关文章推荐
- NBUT 1225 NEW RDSP MODE I
- Qt中如何添加.qrc文件
- [HDOJ1045]Fire Net
- DesignPattern_Java:Abstract Factory Pattern
- csdn初次尝试写博客
- Linux学习日记--基础命令(2)--文件查看,命令历史,命令展开
- SVN服务器搭建和使用(三) (转载)
- KVC基础
- [已解决] jquery只能取得表格的第一行的第一个单元格数值
- Java泛型应用示例
- Linux命令:head,tail命令使用方法
- CentOS7 安装boost
- elisp url库简要说明
- pg_config executable not found
- java新手笔记24 Math/String对象
- Android开发大牛CSDN博客整理
- SVN服务器搭建和使用(二) (转载)
- 玩转linux第三天之history、alias、unalias 、mkdir、rmdir、tree命令(一)
- HashMap与hashCode以及equals
- ibatis传入list对象