這道題一看感覺挺難,因為描述文字長篇大論,仔細讀一讀其實就是求相對分子質量。字符串處理。不過先來想偷懶用正則表達式,但是想了半天沒有什麼太好的方法。於是用普通的方法AC了。如果有大神有正則表達式的好方法留言一下,我也好學習學習。
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main1586 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
while(n-- > 0) {
String str = scan.next();
double mass = 0;
for(int i=0; i='C' && ch<='O') {
if(ch == 'C') {
int cnt = 0;
int flag = 0;
for(int j=i+1; j='0' && str.charAt(j)<='9') {
cnt = cnt * 10 + (int)(str.charAt(j) - '0');
flag ++;
}
else break;
}
if(flag != 0)
mass += (double)cnt * 12.01;
else
mass += 12.01;
}
if(ch == 'H') {
int cnt = 0;
int flag = 0;
for(int j=i+1; j='0' && str.charAt(j)<='9') {
cnt = cnt * 10 + (int)(str.charAt(j) - '0');
flag ++;
}
else break;
}
if(flag != 0)
mass += (double)cnt * 1.008;
else
mass += 1.008;
}
if(ch == 'O') {
int cnt = 0;
int flag = 0;
for(int j=i+1; j='0' && str.charAt(j)<='9') {
cnt = cnt * 10 + (int)(str.charAt(j) - '0');
flag ++;
}
else break;
}
if(flag != 0)
mass += (double)cnt * 16.00;
else
mass += 16.00;
}
if(ch == 'N') {
int cnt = 0;
int flag = 0;
for(int j=i+1; j='0' && str.charAt(j)<='9') {
cnt = cnt * 10 + (int)(str.charAt(j) - '0');
flag ++;
}
else break;
}
if(flag != 0)
mass += (double)cnt * 14.01;
else
mass += 14.01;
}
}
}
System.out.printf("%.3f\n", mass);
}
}
}