分子量(UVa1586)
C++11代码如下:
#include<iostream>
#include<string.h>
#include<iomanip>
using namespace std;
#define maxn 85
char s[maxn]; int main() {
int T;
cin >> T;
while (T--) {
int num_c = , num_h = , num_o = , num_n = ; //分别记录各个化学元素的个数
float sum = 0.0;
cin >> s;
int n = strlen(s);
s[n] = ;
for (int i = ; i < n; i++) {
if (isalpha(s[i])) { //判断是否是字母
switch (s[i]) {
case 'C':if (isdigit(s[i + ])) { //判断字母后面2位是否为数字
if (isdigit(s[i + ])) num_c += (s[i + ] - '') * + (s[i + ] - '');
else num_c += (s[i + ] - '');
}
else num_c++; break;
case 'H':if (isdigit(s[i + ])) {
if (isdigit(s[i + ])) num_h += (s[i + ] - '') * + (s[i + ] - '');
else num_h += (s[i + ] - '');
}
else num_h++; break;
case 'O':if (isdigit(s[i + ])) {
if (isdigit(s[i + ])) num_o += (s[i + ] - '') * + (s[i + ] - '');
else num_o += (s[i + ] - '');
}
else num_o++; break;
case 'N':if (isdigit(s[i + ])) {
if (isdigit(s[i + ])) num_n += (s[i + ] - '') * + (s[i + ] - '');
else num_n += (s[i + ] - '');
}
else num_n++; break;
default:break;
}
}
else continue; //当前数组元素为数字,则直接进入下次循环
}
sum = 12.01*num_c + 1.008*num_h + 16.00*num_o + 14.01*num_n;
cout << fixed<<setprecision()<<sum << endl; //小数点后面保留三位数字
} return ;
}
分子量(UVa1586)的更多相关文章
- 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586)
习题 3-3 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586) 给出一种物质的分子式(不带括号),求分子量.本题中的分子式只包含4种原子,分别为C,H,O,N, ...
- 习题3-2 分子量(Molar Mass, ACM/ICPC Seoul 2007, UVa1586)
#include<stdio.h> #include<string.h> #include<ctype.h> double getweight(char x) { ...
- [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...
- 【OI】计算分子量 Molar mass UVa 1586 题解
题目:(由于UVa注册不了,还是用vjudge) https://vjudge.net/problem/UVA-1586 详细说明放在了注释里面.原创. 破题点在于对于一个元素的组合(元素+个数),只 ...
- 分子量 (Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)
解题思路: 1.将分子量用double 数组记录下来 2.将字符串存储在字符数组中,从头向后扫描,一直记住“字母”,对下一个字符进行判断,是否是数字,如果是数字:用一个整数记录,本代码中用的sum,同 ...
- 课后题--------求分子量-----Molar mass------
简单的化学式 求分子量问题 下面附上 代码和解析. #include<stdio.h> #include<algorithm> #include<string.h&g ...
- 分子量(JAVA语言)
package 第三章习题; /* * 给出一种物质的分子式(不带括号)求分子量. * 本题分子只包含4种原子,分别为C,H,O,N * 分子量为分别为12.01,1.008,16.00,14 ...
- 【C/C++】习题3-2 分子量/算法竞赛入门经典/字符串
给出一种物质的分子式,求分子量.只包含4种原子,分别为C,H,O,N. [知识点] 1.ASCII码 [阿拉伯数字]48~57 [大写字母]65~90 [小写字母]97~122 2.输入循环到n-1的 ...
- UVA1586
#include<stdio.h> #include<string.h> #include<ctype.h> int main(){ int n; ]; int n ...
随机推荐
- 【树状数组】【P3902】 递增
传送门 Description 给你一个长度为\(n\)的整数数列,要求修改最少的数字使得数列单调递增 Input 第一行为\(n\) 第二行\(n\)个数代表数列 Output 输出一行代表答案 H ...
- 微信小程序踩过的坑
之前用小程序开发工具做过一个项目了,最近又新开了一个项目,在登录的时候发现总是提示code不合法,找了半天也未发现原因 后来同事提醒是不是因为开发工具里设置的AppId的问题,果断将当前工具里默认Ap ...
- 用python + openpyxl处理excel(07+)文档 + 一些中文处理的技巧
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...
- Lucene 索引与检索架构图
- CCPC2018-A-Buy and Resell
Problem Description The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1 ...
- 百度搜索URL中的参数都是什么
最近,点石排名更新了一个新功能——站内搜索.其实理解起来也很简单,就是通过URL限定搜索结果为某个网站,从而参与点击(例如:https://www.baidu.com/s?wd=SEO&si= ...
- logstash 收集 IIS 日志实践
IIS日志示例: 2017-02-20 00:55:40 127.0.0.1 GET /MkWebAPI/swagger/ui/index - 80 - 127.0.0.1 Mozilla/5.0+( ...
- LintCode 395: First Will Win 2
LintCode 395: First Will Win 2 题目描述 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币 ...
- Spring Boot工程结构推荐
工程结构(最佳实践) Spring Boot框架本身并没有对工程结构有特别的要求,但是按照最佳实践的工程结构可以帮助我们减少可能会遇见的坑,尤其是Spring包扫描机制的存在,如果您使用最佳实践的工程 ...
- 2017ACM暑期多校联合训练 - Team 7 1009 HDU 6128 Inverse of sum (数学计算)
题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants ...