• UVa 1586


           这道题一看感觉挺难,因为描述文字长篇大论,仔细读一读其实就是求相对分子质量。字符串处理。不过先来想偷懒用正则表达式,但是想了半天没有什么太好的方法。于是用普通的方法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<str.length(); i++) {
    				char ch = str.charAt(i);
    				if(ch>='C' && ch<='O') {
    					if(ch == 'C') {
    						int cnt = 0;
    						int flag = 0;
    						for(int j=i+1; j<str.length(); j++) {
    							if(str.charAt(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<str.length(); j++) {
    							if(str.charAt(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<str.length(); j++) {
    							if(str.charAt(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<str.length(); j++) {
    							if(str.charAt(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
    ", mass);
    		
    		}
    
    	}
    
    }
    
  • 相关阅读:
    精选30道Java笔试题解答
    ASM
    Java Decompiler Plugin For Eclipse IDE
    AMQ5540, AMQ5541 and AMQ5542, application did not supply a user ID and password, 2035 MQRC_NOT_AUTHORIZED
    Shell脚本中的export
    Linux set unset命令
    shell中${}的妙用
    ubuntu alsa2
    ubuntu alsa
    计算机启动boot
  • 原文地址:https://www.cnblogs.com/wxisme/p/4363743.html
Copyright © 2020-2023  润新知