《算法竞赛入门经典(第2版)》 作者:刘汝佳
第三章习题3-2 给出一种物质的分子式,求分子量
https://uva.onlinejudge.org//index.php?option=com_onlinejudge&Itemid=8&category=827
---------------------------------------E3-2.cpp--------------------------------------
#include <stdio.h>
#include <string.h>
#define LOCAL
#define maxn 85
static double a[maxn] = {12.01, 1.008, 16.00, 14.01};
int main()
{
#ifdef LOCAL
freopen("E3-2.in", "r", stdin);
#endif
int kase;
scanf("%d", &kase);
while(kase--)
{
char s[maxn];
scanf("%s", s);
double ans = 0.0, t = 0.0;
for(int i = 0; i < strlen(s); i++)
{
int count = 1;
if(s[i] == 'C')
{
t = a[0];
}
else if(s[i] == 'H')
{
t = a[1];
}
else if(s[i] == 'O')
{
t = a[2];
}
else if(s[i] == 'N')
{
t = a[3];
}
else if(s[i+1] - '0' <= 9 && s[i+1] != 0) // 连续两个数字,注意字符串末尾的0
{
count = (s[i]-'0')*10 + (s[i+1]-'0')- 1 ;
i++;
}
else
{
count = s[i]-'0' - 1; // 一个数字
}
ans += t * count;
}
printf("%.3lf\n", ans); // 格式控制必须与题目要求完全一致
}
return 0;
}
--------------------------------------E3-2.in的分界线-------------------------------------------------
100
O61H78C11CN81CO47O22NN24H26NC96O92CN12CCN26C22C95O33H50CC
N77N31N12C97C18H50O69HO24O72OOCONC11HC60NN22O95H23O61
NNOOCOON6C50HNN
HCH14HHOOC29O59O51HOO60H73N
CNNCC93N19CH98N32CHCNH23H41H26N35C84OO45O95O46
HHHC44HCO30ONN94HO11C85NHNHN88HN96O95
NOON
HNO90C83N82CHN83ONCNHOOOO14O
O11N81NH
NCC79HNOH2HH4NH40C22H94H10N39C28NO33C22O4
HN64O43O6O55HN55C
H58NHHN85NCOOCHC78HOO54CH87O34HO33O96N17NONH
O13N48N31CH12C15HO32NONONNHC58COOC24C38O66NCN
OOHHH70H93C49N65OCC15N53N53OHC5N30O18CC22CN24CCC58C4
N91CO15C59C75CCO15NH81H55N32C51HH
O4N25N51NN23HN25C65HCH65N
O92HO57O50H
C54ON16CNC73HCCN56NN34H29N13N95H52OOO19O71H42C24N25C64C63
HN10NOO46HN74H35C29NOH18HC60O28H81C44OHCC
O24C32OH18N86O25O
O63N46ONH79H49N92NHNCNCCHHO80HOC11H
HHHON66NO69O84CN71O41CCCN37
NHNHC28O92C42C94N63
O50CCCOH39
H62CN
H62
C38CON70C51HCNCN94NCC
N14N10O
CC11HCH21HN80C18NN28H85N79N45HO2
H4NHCCN94O6H28OON3N2O31C60HC56N85O
O39NCNH29NNCCC47HNN4H94ONHC33N21OC37O99H75C95H
C3NNOC29N31HC36H74
C16H2NCOCCHNNCC82OO26O11ON77HH
O27CC7CN37
C69OC83N25N62CCHH34O3H95HCN14N74NN
O91C92OOC12NOH54N60O61N32H54NHN67CO22H3O83N38
HHO45N53OHCO36HCO24CC49H4C46OO49H90C45
C32C67HH23O61H24O72N76O15O86NN
OC99NO34N45CC97H27HN65C5O30HOOHNONN91CHH25N43NN
OOH56H38C99C82HOCC13H30N40NC8N24CON76C42H5O34H29OOCO
NC22NH44NC56CN16NNOH55C
ON19O77NOC66HN65H2NOH21N43H7C57C92N86H99O76H
NON16COO82NCC72ONOHN75C64ON97ON74OC98N
CCNHO6ON18C6NONH70O93CO98HHCHO47C11CNO91N
O54C60OHH24N89H95C50
N56N22NO45O67H28HHN71H57NO93ONH97OHHCC31
CC73OHCCH65C57HHON52NN23OH49OC89
H33O56N92N70NC10HCH64OO37ON
C32C60H11C11C37CN76CC16CN35CHCC95O54NH62OH18OC49C51OC78
CO50HO39O4CCH35CN26H5OH53HHH28ON55HNOH
H21OH6O32NHO43N51CNO72OOH49H35CH82ON
C35NO47N26NHC46OH27HCO63H82ONCNNOH33
O63O87CCO10H11OO77
N88
HO
C56O9H76
OC77C88NNN9HHC42O47N10H14C46
OHCN47HO82ONNHN45N21C74N69CN69O
C57HCO16N24NCO50H84H54NOOO45OH57
C37N73C28C84N51OO52O79N96
NO31N50NOH86C97OHOC82CNN37HN29HHCO
CC70OC71C77NN21HO53N75CC57NH75HO71CN13
C97OOO78H54N66C64
HN87HH63C10O23N82H73
N20O15CN79CNNHCNH63C92O16H46OH66O48C39CN98O
H31C
H53C22O26OH6N47C33H16H68N98ON87
H66N84OHO3C12ON73O92CH51H
NO36H49NO28H
HO23OO80HO94CN42N20O68NC74H48HH71OHHH
N32C5OO54OHH73O82O28CO85O43O82C24
HO
HOC38CONO38OC19CNH
N36HOC36H14OH41N38NHO8H72ONH11HCN72ON16N20O
C80CNHC19ONHNN69O18
HCO46H8C55NO68OH26OOHN78ONNCNO70N58OCC84C
N5O92CHOOC9NO58CNO13HCN19
H76CC42C23CH16C33H78C55O98HO
H26CNH76
N
OH57HOCO40C44H25OCH58NH41HC25O39NC81O32NH46CN83N81
OH90C19O35H18HH74HN89HO98NC72OO22O3H41O39CN14
O38NH32C70H34HCHN44NC52O46H2H35C67ON71
O30HNNO88HN72N6HO
H22HNHNNN7ON87
OC6NC85
O17HCHN46O95NNH26HO16C
H8H53NN37HC23O19NHN25
O3O73C49HO42NCOHH78CCHHO
HN61H23HN46HC46OHC65CHO62C86
H93CH70H41NCO47NH75
O11H17OHOOHON72NNNHOCN16OHHC53N
HO
O58H21O77NH27H93OCCO88
H2NH94N23H36NO
C95C88H51N69C19N82N69O67CC
C44OO92NN
HH99N69HH90O60
HC78N
COOC16C41N35NOHO58N48ON39OCN24O