分子量(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 ...
随机推荐
- Python数据生成pdf文件
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- 760A 水
LINK 第一天单独一行 其余7天一行 问某月有多少行 ...... /** @Date : 2017-04-02-21.35 * @Author : Lweleth (SoungEarlf@gmai ...
- Python学习笔记(三十一)正则表达式
---恢复内容开始--- 摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 ...
- 什么是Qt Widget?
Qt帮助文档如此解释: The user interface contains visual elements that are called widgets in Qt. Examples of w ...
- 【BZOJ】4316: 小C的独立集 静态仙人掌
[题意]给定仙人掌图,求最大独立集(选择最大的点集使得点间无连边).n<=50000,m<=60000. [算法]DFS处理仙人掌图 [题解]参考:[BZOJ]1023: [SHOI200 ...
- 微信小程序开发(四)线程架构和开发步骤
线程架构 从前面的章节我们可以知道,.js文件是页面逻辑处理层.我们可以按需在app.js和page.js中添加程序在生命周期的每个阶段相应的事件.如在页面的onLoad时进行数据的下载,onShow ...
- CodeForces 1011B
Description Natasha is planning an expedition to Mars for nn people. One of the important tasks is t ...
- thinkphp 漂亮的分页样式
---恢复内容开始--- 首先:需要两个文件 page.class.php page.css 1.在TP原有的 page.class.php 文件稍作修改几条代码就可以了, 修改过的地方我会注释, 2 ...
- 21.Merge Two Sorted Lists---《剑指offer》面试17
题目链接:https://leetcode.com/problems/merge-two-sorted-lists/description/ 题目大意: 给出两个升序链表,将它们归并成一个链表,若有重 ...
- 144.Binary Tree Preorder Traversal---二叉树先序、中序非递归遍历
题目链接 题目大意:返回二叉树的先序遍历list.中序见94,后序见145. 法一:普通递归遍历,只是这里多了一个list数组,所以分成了两个函数.代码如下(耗时1ms): public List&l ...