zoukankan      html  css  js  c++  java
  • UVa1586

    //UVa1586 - Molar mass
    //给出一种由C, H, O, N 四种原子构成的分子式,求分子量
    //#define A1 //无法处理换行问题(scanf)
    //#define A2 //临界问题有BUG(sscanf)
    #define A3 //只考虑两位即可AC,直接暴力
    //#define A4 //考虑多位数有BUG
    
    #ifdef A1
    #include<stdio.h>
    int main(){
    	//freopen("data.in","r",stdin);
    	int T;
    	scanf("%d",&T);
    	getchar();
    	while(T--){
    		char ch, ch2 = 'A'; int num; float M, ans = 0;
    		//无法处理换行问题
    		//while((ch2=getchar()) != EOF){
    		while(scanf("%c",&ch2) == 1){
    			if(ch2 == '
    '){if(ch >= 'C')ans += M; break;}
    			else ch = ch2;
    			if(ch == 'C') M = 12.01;
    			if(ch == 'H') M = 1.008;
    			if(ch == 'O') M = 16.00;
    			if(ch == 'N') M = 14.01;
    			if(scanf("%d",&num) != 1) ans += M;
    			else ans += M*num;
    		}
    		printf("%.3f
    ", ans);
    	}
    	return 0;
    }
    #endif
    
    #ifdef A2
    #include<stdio.h>
    #include<string.h>
    #define maxn 20
    int bits(int x){
    	if(x == 0)return 1;
    	int count = 0;
    	while(x>0){ count++; x/=10;}
    	return count;
    }
    int main(){
    	freopen("data.in","r",stdin);
    	int T;
    	scanf("%d",&T);
    	getchar();
    	while(T--){
    		int num; float M, ans = 0;
    		char str[maxn];
    		fgets(str,maxn,stdin);
    		//scanf("%s",str);
    		for(int i = 0; i<strlen(str); i++){
    			if(str[i] == 'C') M = 12.01;
    			if(str[i] == 'H') M = 1.008;
    			if(str[i] == 'O') M = 16.00;
    			if(str[i] == 'N') M = 14.01;
    			if(i == strlen(str)-1 && str[i]>='C'){ ans += M; break;}
    			if(sscanf(&str[i+1],"%d",&num) != 1) ans += M;
    			else {
    				i += bits(num);
    				ans += M*num;
    			}
    		}
    		printf("%.3f
    ", ans);
    	}
    	return 0;
    }
    #endif
    
    #ifdef A3
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h> 
    #define maxn 100
    int main(){
    	//freopen("data.in","r",stdin);
    	int T;
    	scanf("%d",&T);
    	while(T--){
    		char s[maxn]; scanf("%s",s);
    		int n, a[4], len = strlen(s);
    		memset(a,0,sizeof(a));
    		for(int i = 0; i<len; i++){
    			if(s[i] == 'C') n = 0;
    			if(s[i] == 'H') n = 1;
    			if(s[i] == 'O') n = 2;
    			if(s[i] == 'N') n = 3;
    			if(s[i]>='C')
    			if(isdigit(s[i+1]) && isdigit(s[i+2])) a[n] += 10*(s[i+1]-'0')+(s[i+2]-'0');
    			else a[n] += isdigit(s[i+1])? s[i+1]-'0': 1; 
    		}
    		printf("%.3f
    ", 12.01*a[0]+1.008*a[1]+16.00*a[2]+14.01*a[3]);
    	}
    	return 0;
    }
    #endif
    
    #ifdef A4
    #include<stdio.h>
    #include<string.h>
    int co(int cot){int add42=1; while(--cot)add42 *= 10; return add42;}
    int main(){
    	freopen("data.in","r",stdin);
    	int T;
    	scanf("%d",&T);
    	getchar();
    	while(T--){
    		char s[20];int a[4],n;
    		memset(a,0,sizeof(a));
    	//获取输入
    		scanf("%s",s);
    	//处理数据
    		for(int i = 0; i < strlen(s); i++){
    			if(s[i] > 57){//存储个数
    				if(s[i] == 'C'){n=0;a[n]++;}
    				if(s[i] == 'H'){n=1;a[n]++;}
    				if(s[i] == 'O'){n=2;a[n]++;}
    				if(s[i] == 'N'){n=3;a[n]++;}
    			}else{
    				int count = 0;//记录数字位数
    				for(int j = 0; ; j++){
    					if(s[i+j] <= 57)count++;
    					else break;
    					printf("%d
    ",j);
    				}
    				while(count--){a[n] += (s[i+count]-'1')*co(count);printf("%d ",count);}
    			}
    		}
    	//输出
    		printf("%.3f
    ",12.01*a[0]+1.008*a[1]+16.00*a[2]+14.01*a[3]);
    	}
    	return 0;
    }
    #endif
    /*测试数据:
    4
    C
    C6H5OH
    NH2CH2COOH
    C12H22O11
    
    12.010
    94.108
    75.070
    342.296
    */
    
    

  • 相关阅读:
    Java实现 蓝桥杯VIP 算法训练 字符删除
    Java实现 蓝桥杯VIP 算法训练 字符删除
    Java实现 蓝桥杯VIP 算法训练 字符删除
    Java实现 蓝桥杯VIP 算法训练 字符删除
    Java实现 蓝桥杯VIP 算法训练 字符删除
    Java实现 蓝桥杯VIP 算法训练 字符串编辑
    Java实现 蓝桥杯VIP 算法训练 字符串编辑
    Java实现 蓝桥杯VIP 算法训练 字符串编辑
    Java实现 蓝桥杯VIP 算法训练 字符串编辑
    Java实现 蓝桥杯VIP 算法训练 字符串编辑
  • 原文地址:https://www.cnblogs.com/gwj1314/p/9444956.html
Copyright © 2011-2022 走看看