[Molar Mass, ACM/ICPC Seoul 2007, UVA1586]
程序员文章站
2022-06-09 16:51:10
...
[Molar Mass, ACM/ICPC Seoul 2007, UVA1586]
主要是固定位 的思想,这是最后的代码,不过中间一直wrong answer,一直找不出,原来是15写成了14.
#include <stdio.h>
#include <string.h>
int isnum(char c) {
return c>='0' && c<='9';
}
int main() {
int T;
char s[85];
//freopen("test.txt", "r", stdin);
double wei[15];
wei['C'- 'A'] = 12.01;
wei['H'- 'A'] = 1.008;
wei['O'- 'A'] = 16.00;
wei['N'- 'A'] = 14.01;
scanf("%d", &T);
while(T--) {
scanf("%s", s);
int len = strlen(s);
double sum = 0;
for(int i=0; s[i]!='\0'; ++i) {
if(i+3 == len) {
if(isnum(s[i+1]) && isnum(s[i+2])) {
sum += wei[s[i] - 'A'] * ((s[i+1] - '0')*10 + (s[i+2] - '0'));
break;
}
else if(isnum(s[i+1])) {
sum += wei[s[i] - 'A'];
i++;
}
}
if(i+2 == len && isnum(s[i+1])) {
sum += wei[s[i] - 'A'] * (s[i+1] - '0');
break;
}
if(isnum(s[i+1]) && isnum(s[i+2])) {
sum += wei[s[i] - 'A'] * ((s[i+1] - '0')*10 + (s[i+2] - '0'));
i+=2;
}
else if(isnum(s[i+1])) {
sum += wei[s[i] - 'A'] * (s[i+1] - '0');
i++;
}
else {
sum += wei[s[i] - 'A'];
}
}
printf("%.3f\n", sum);
}
return 0;
}
推荐阅读
-
分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586)
-
Molar Mass, ACM/ICPC Seoul 2007,UVa1586
-
[Molar Mass, ACM/ICPC Seoul 2007, UVA1586]
-
Molar Mass, ACM/ICPC Seoul 2007, UVa 1586
-
分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586)
-
【算法竞赛入门经典】习题3-2 分子量(Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)
-
分子量(Molar Mass, ACM/ICPC Seoul 2007, UVa1586)