POJ 1472 Instant Complexity G++ 递归 没掌握

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stack> 
#include <algorithm> 
using namespace std;
//英语      看博友分析    抄博友程序     递归     没掌握 
//int mi[20];
//stack<string> lo;
//stack<int> op;
const int N = 12;
int deal(int* mi)
{
	char ch[100];
	scanf("%s",ch);
	if(ch[0]=='E')
	{
		return 0;
	}
	if(ch[0]=='B')
	{
		while(deal(mi));//重要 
	}else if(ch[0]=='L')
	{
		scanf("%s",ch);
		int t=-1,tmp[N]={0};//
		if(ch[0]!='n')
		{
			t=atoi(ch);
		}
		while(deal(tmp));//重要 
		if(t==-1)
		{
			for(int i=11;i>=1;i--)
			{
				tmp[i]=tmp[i-1];
			}
			tmp[0]=0;
		}else
		{
			for(int i=11;i>=0;i--)
			{
				tmp[i]*=t;
			}
		}
		for(int i=11;i>=0;i--)
		{
			mi[i]+=tmp[i];
		}
	}else
	{
		scanf("%s",ch);
		mi[0]+=atoi(ch);
		return deal(mi);
	}
	return 1;
}
int main()
{
	int T;
	cin>>T;
	int tag=0;
	while(T--)
	{
		tag++;
		int mi[N]={0};
		deal(mi); 
		/*
		int js=0;//栈实现 wa		
		while(lo.empty()!=1)
		{
			lo.pop();
		}
		while(op.empty()!=1)
		{
			op.pop();
		}
		int sum=0;
		int flag=0;//最里层 
		while(1)
		{
			string s;
			cin>>s;
			if(s=="BEGIN")
			{
				js++;
				lo.push("1");
			}else if(s=="END")
			{
				js--;
				int x;
				if(flag==0)
				{
					flag=1;
					x=0;
				}else
				{
					x=op.top();
					op.pop();					
				}
				int y=sum;				
				mi[0]=mi[0]+x+y;
				//cout<<x<<" "<<y<<" "<<mi[0]<<endl;
				string a=lo.top();
				lo.pop();
				if(a=="n")
				{
					for(int i=20;i>=1;i--)
					{
						mi[i]=mi[i-1];
					}
					mi[0]=0;
				}else
				{
					int b=0;
					for(int i=0;i<a.size();i++)
					{
						b=b*10+a[i]-'0';
					}
					for(int i=20;i>=0;i--)
					{
						mi[i]=mi[i]*b;
					}
				}
				
				//for(int i=0;i<=10;i++)
				//{
				//	cout<<mi[i]<<" ";
				//}
				//cout<<endl;
				sum=0;
			}else if(s=="LOOP")
			{
				js++;
				string a;
				cin>>a;
				lo.push(a);
				op.push(sum);
				//cout<<sum<<endl;
				sum=0;
			}else if(s=="OP")
			{
				int a;
				cin>>a;
				sum=sum+a;				
			}
			if(js==0)
			{
				break;
			}
		}*/
		cout<<"Program #"<<tag<<endl;
		cout<<"Runtime = ";
		int one=0;
		for(int i=11;i>=0;i--)
		{
			if(mi[i]!=0)
			{
				if(one==0)
				{
					one=1;
				}else
				{
					cout<<"+";
				}	
				if(i==0 )
				{
					cout<<mi[i];
				}else if(mi[i]==1)
				{
					
				}else
				{
					cout<<mi[i]<<"*";
				}					
				if(i==1)
				{
					cout<<"n";
				}else if(i==0)
				{
					
				}else
				{
					cout<<"n^"<<i;
				}				
			}
		}
		if(one==0)
		{
			cout<<0<<endl<<endl;
		}else
		{
			cout<<endl<<endl;
		}			
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值