分子量 (Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)

解题思路:
1.将分子量用double 数组记录下来
2.将字符串存储在字符数组中,从头向后扫描,一直记住“字母”,对下一个字符进行判断,是否是数字,如果是数字:用一个整数记录,本代码中用的sum,同时下标++。
进行判断,查看是否对数字进行了记录,即查看sum是否进入了while循环并被赋值,如果没有被赋值,说明下一个字符不是数字,直接对W(总记录)值进行赋值,为当前字符的权值(分子量),即double数组的中的值。如果被赋值,说明字符后面是一个数字,sum中存放了该“数字”,也是对w赋值,不过此时是赋sum倍的当前字符的权值(分子量)。
3.最后整个字符串循环完毕,输出总记录W即可。
#include <stdio.h>
int main(void)
{
double darr[200];
int n,sum,p;
double w;
char c,carr[1000];
darr['C']=12.01;
darr['H']=1.008;
darr['O']=16.00;
darr['N']=14.01;
scanf("%d",&n);
while(n--)
{
p=0;
w=0;
scanf("%s",carr);
while(carr[p]!='\0')
{
sum=0;
c=carr[p];
p++;
while(carr[p]>='0' && carr[p]<='9')
{
sum=sum*10+carr[p]-'0';
p++;
}
if(sum)
w=w+sum*(darr[c]);
else
w+=(darr[c]);
}
printf("%.3f\n",w);
}
return 0;
}
分子量 (Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)的更多相关文章
- 分子量(Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)
#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[20]; scanf ...
- UVa 1586 - Molar Mass - ACM/ICPC Seoul 2007 - C语言
关键在于判断数字是两位数还是单位数,其他部分没有难度. #include"stdio.h" #include"string.h" #include"c ...
- 习题3-2 分子量(Molar Mass, ACM/ICPC Seoul 2007, UVa1586)
#include<stdio.h> #include<string.h> #include<ctype.h> double getweight(char x) { ...
- 数数字 (Digit Counting,ACM/ICPC Danang 2007,UVa 1225)
思路: 利用java 特性,将数字从1 一直加到n,全部放到String中,然后依次对strring扫描每一位,使其carr[str.charAt(i)-'0']++; 最后输出carr[i],即可. ...
- 得分(Score,ACM/ICPC Seoul 2005,UVa 1585)
#include<stdio.h> int main(void) { char b; int t,cou,sum; scanf("%d",&t); getcha ...
- 生成元(Digit Generator ,ACM/ICPC Seoul 2005 ,UVa 1583)
生成元:如果 x 加上 x 各个数字之和得到y,则说x是y的生成元. n(1<=n<=100000),求最小生成元,无解输出0. 例如:n=216 , 解是:198 198+1+9+8=2 ...
- 得分(Score, ACM/ICPC Seoul 2005,UVa 1585)
#include<cstdio>#include<cstdlib>#include<cstring>int main(){ char s[80];//输入OOXXO ...
- 生成元(Digit Generator,ACM/ICPC Seoul 2005,UVa 1583)
#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;int t, n, a, ...
- 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586)
习题 3-3 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586) 给出一种物质的分子式(不带括号),求分子量.本题中的分子式只包含4种原子,分别为C,H,O,N, ...
随机推荐
- 静态时序分析(static timing analysis) --- 时序路径
时序分析工具会找到且分析设计中的所有路径.每一个路径有一个起点(startpoint)和一个终点(endpoint).起点是设计中数据被时钟沿载入的那个时间点,而终点则是数据通过了组合逻辑被另一个时间 ...
- C语言 读取文件中特定数据
//读取文件数据 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct jia ...
- DropDownList中显示无限级树形结构
效果图: 数据库表: DirID:目录的ID,ParentID:目录的父路径ID,Name:目录的名字主要代码: using System;using System.Collections;using ...
- PC网站应用接入微信登录
参考文档: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&ve ...
- Windows 2008如何绑定MAC防范ARP攻击!
Windows 2008如何绑定MAC防范ARP攻击! 阅读(1974)暂无评论时间:2010-11-23 22:52:13 在Windows server 2003时代,通过arp 这命令即 ...
- Mac 下安装tomcat
一. 下载tomcat 首先要到tomcat官网去下载安装包,官网下载地址如下:http://tomcat.apache.org/download-70.cgi , 注意请下载飞windows版本.和 ...
- requirejs自己的学习
1.最新版本的RequireJS压缩后只有14K. 2.模块化,不在使用全局变量,都用块级作用域包装. 3.防止js加载阻止页面渲染. 4.避免出现多个javascript的标签.
- The entity type <type> is not part of the model for the current context
这是在网站里遇到的一个错误,自动生成的不能手动添加, reference: http://stackoverflow.com/questions/19695545/the-entity-type-xx ...
- [BZOJ1406][AHOI2007]密码箱(数论)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1406 分析: (x+1)(x-1)是n的倍数 于是可以把n分解成n=ab,则a为(x+ ...
- WCF入门(11)
前言 终于出太阳了. 可惜风太大,凉在阳台上的衣服全被吹到了地上,外加几双袜子被吹掉了,gone. 第11集 Difference between DataContract and MessageCo ...