UVa1586 分子量+hdu1274的3ab(b)

数组练习题。求解分子量的

这道题让人想起了河南省赛,改进啊…然而这题2007年就有了…

题目解法参考hdu1274 【输出3(ab2(cd))->>abcdcdabcdcdabcdcd】网上大神的解法写了该题。链接和题解在最后

这道题没有括号,每个英文后面或许没有,或许为数字。先判断字母,然后判断应该是几个



题目太长,题目网址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4461

Input
Your program is to read from standard input. The input consists of T test cases. The number of test
cases T is given in the first line of the input. Each test case is given in a single line, which contains
a molecular formula as a string. The chemical symbol is given by a capital letter and the length of
the string is greater than 0 and less than 80. The quantity number n which is represented after the
chemical symbol would be omitted when the number is 1 (2 ≤ n ≤ 99).
Output
Your program is to write to standard output. Print exactly one line for each test case. The line should
contain the molar mass of the given molecular formula.
Sample Input
4
C
C6H5OH
NH2CH2COOH
C12H22O11
Sample Output
12.010
94.108
75.070
342.296


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
using namespace std;

char s[82];
double sum=0;
char ch='\0';
int len;
int fun(int ith){
	int k;
	double num;
	char c;
	sum=0;
 	len=strlen(s);

	for(c=s[ith];ith<len;){
	
		if(isalpha(c)){
			ch=c;			
			switch(ch){
				case 'C': num=12.01;break;
				case 'H': num=1.008;break;
				case 'O': num=16.00;break;
				case 'N': num=14.01;break;
				default : num=0;
			}
		}
		
		for(k=0,c=s[++ith];isdigit(c);c=s[++ith]){
			k=k*10+c-'0';
		}
		if(!k) k=1;
		sum+=num*k;	
		if(ith==len) {
		printf("%.3lf\n",sum);
		return 1;
	}
	}


}


int main(){
	int n,len;
	freopen("UVa1586.txt","r",stdin);
	scanf("%d",&n);
//	cout<<"n="<<n<<endl;
	
	while(n--){
		sum=0;
		scanf("%s",s);
	//	cout<<"len="<<len<<endl;
		fun(0);	
	}
	

	return 0;
	
}




hdu1274

Problem Description
在纺织CAD系统开发过程中,经常会遇到纱线排列的问题。
该问题的描述是这样的:常用纱线的品种一般不会超过25种,所以分别可以用小写字母表示不同的纱线,例如:abc表示三根纱线的排列;重复可以用数字和括号表示,例如:2(abc)表示abcabc;1(a)=1a表示a;2ab表示aab;如果括号前面没有表示重复的数字出现,则就可认为是1被省略了,如:cd(abc)=cd1(abc)=cdabc;这种表示方法非常简单紧凑,也易于理解;但是计算机却不能理解。为了使计算机接受,就必须将简单紧凑的表达方式展开。某ACM队接受了此项任务。现在你就是该ACM队的一员,请你把这个程序编写完成。
已知条件:输入的简单紧凑表达方式的长度不超过250个字符;括号前表示重复的数不超过1000;不会出现除了数字、括号、小写字母以外的任何其他字符;不会出现括号不配对等错误的情况(错误处理已由ACM其他队员完成了)。
 
Input
本题有多个测试数据组,第一行输入的就是数据组数N,接着就是N行表达式,表达式是按照前面介绍的意义书写的。
 
Output
输出时含有N行,每行对应一个输入的表达式。
 
Sample Input
  
  
2 1(1a2b1(ab)1c) 3(ab2(4ab))
 
Sample Output
  
  
abbabc abaaaabaaaababaaaabaaaababaaaabaaaab

以下转载代码:

#include <iostream>
 #include <cctype>
 #include <cstring>
 #include <string>
 using namespace std;
 
 string s;
 int fun(int ith)
 {
      int k,e;
      char c;
      for(c=s[ith++];ith<s.size()&&c!=')';c=s[ith++])//递归结束的条件是字符串结束或遇到右括号 
      {
           for(k=0;isdigit(c);c=s[ith++])
               k=k*10+c-'0';
           if(!k) k=1;
           if(c=='('){
               while(k--)
                   e=fun(ith);
               ith=e;//重置ith的值,到下层递归结束的位置 
      }
      else
      {
          while(k--)
              putchar(c);
      }
  }
  if(c==')') return ith;//返回本次读到结尾的位置 
 }
 int main()
 {
      int i,j,k,T;
      cin>>T;
      while(T--)
      {
           s.clear();
           cin>>s;
           fun(0);//进入递归 
           cout<<endl;
      }
      return 0;
 }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值