当前位置: 代码迷 >> 综合 >> Molar mass UVA - 1586
  详细解决方案

Molar mass UVA - 1586

热度:52   发布时间:2023-10-13 15:05:08.0

问题类型:字符串,条件判断。

03pie’s solution for [UVA-1586]:

#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<vector>
#include<fstream>
#include<list>
using namespace std;#define ms(s) memset(s,0,sizeof(s))typedef unsigned long long ULL;
typedef long long LL;const double PI = 3.141592653589;
const int INF = 0x3fffffff;float v(char a)
{float q;if(a=='C')  q=12.01;if(a=='H')  q=1.008;if(a=='O')  q=16.00;if(a=='N')  q=14.01; return q;} 
#define maxn 90
int main(){// freopen("F://inp.txt","r",stdin);char gs[maxn];int T;scanf("%d",&T);while(T--){scanf("%s",gs);int len=strlen(gs);float toll=0;int nubl=0,p=0;for(int i=0;i<len;i++){char car;if(isalpha(gs[i]))//是字母 {if(gs[i+1]=='\0'||isalpha(gs[i+1])){
// printf("tool=%.3f\n",toll);toll+=(v(gs[i]));
// printf("tool=%.3f\n",toll); }}if(isdigit(gs[i]))//是数字 {if(isalpha(gs[i-1]))//前一位是字母 {car=gs[i-1];nubl=(gs[i]-'0');}if(isdigit(gs[i-1]))//前一位是数字{nubl=nubl*10+(gs[i]-'0');} if(isalpha(gs[i+1])||gs[i+1]=='\0')//后一位是字母 ,和结束 {toll+=nubl*v(car);}}}printf("%.3f\n",toll);} return 0;
}