题意:就是让你求出时间复杂度。

分析:由于指数最多为10次方,所以可以想到用一个数组保存各个指数的系数,具体看代码实现吧!

代码实现:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; char str[][];
char key[];
int num; struct node{
int sum[];
void init()
{
memset(sum,,sizeof(sum));
}
}; void init()//把字符串提取出来
{
int i,t=,num=;
scanf("%s",key);
while(t)
{
scanf("%s",key);
strcpy(str[num++],key);
if(key[]=='E')
t--;
else if(key[]=='L')
t++;
}
} struct node xiangcheng(struct node a,struct node b)//相乘
{
int i,j;
struct node res;
res.init();
for(i=;i<=;i++)
for(j=;j<=;j++)
res.sum[i+j]=res.sum[i+j]+a.sum[i]*b.sum[j];
return res;
} struct node add(struct node a,struct node b)//相加
{
int i;
for(i=;i<=;i++)
a.sum[i]+=b.sum[i];
return a;
} struct node dfs()
{
int i=;
struct node a;
a.init();
while()//碰到END才结束
{
i=;
if(str[num][]=='L')
{
num=num+;
struct node b;
b.init();
if(str[num][]=='n')
b.sum[]++;
else
{
int k=;
i=;
while(str[num][i]!='\0')
{
k=k*+str[num][i]-'';
i++;
}
b.sum[]=b.sum[]+k;
}
num=num+;
b=xiangcheng(b,dfs());
a=add(a,b);
} else if(str[num][]=='O')
{
num=num+;
if(str[num][]=='n')
a.sum[]++;
else
{
int k=;
i=;
while(str[num][i]!='\0')
{
k=k*+str[num][i]-'';
i++;
}
a.sum[]=a.sum[]+k;
}
} else if(str[num][]=='E')
return a;
num++;
}
} int main()
{
int T;
int i,j,flag;
struct node a;
scanf("%d",&T);
getchar();
for(j=;j<=T;j++)
{
num=;
init();
a=dfs();
printf("Program #%d\nRuntime = ",j);
i=;flag=;
while(a.sum[i]==) i--;
for(;i>;i--)
{
if(a.sum[i]==)
continue;
if(flag==)
{
if(a.sum[i]>)
printf("%d*",a.sum[i]);
printf("n^%d",i);
flag++;
}
else
{
printf("+");
if(a.sum[i]>)
printf("%d*",a.sum[i]);
printf("n^%d",i);
}
}
if(a.sum[])
{
if(flag)
printf("+");
if(a.sum[]>)
printf("%d*",a.sum[]);
printf("n");
flag++;
}
if(a.sum[])
{
if(flag)
printf("+");
printf("%d",a.sum[]);
flag++;
}
if(flag==)
printf("");
printf("\n\n");
}
return ;
}

poj 1472(递归模拟)的更多相关文章

  1. POJ 1472:Instant Complexity 模拟时间复杂度

    Instant Complexity Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1908   Accepted: 658 ...

  2. POJ 1472 Instant Complexity 应该叫它编程题。。

    题目:http://poj.org/problem?id=1472 这个题目是分到“模拟题”一类的,我觉得模拟的成分比较少,主要考察编程能力.独立写完这个题特别兴奋...所以我必须好好说一说,独家哦. ...

  3. poj 3077Rounders(模拟)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://po ...

  4. POJ 1068 Parencodings 模拟 难度:0

    http://poj.org/problem?id=1068 #include<cstdio> #include <cstring> using namespace std; ...

  5. POJ 1036 Rails 模拟堆栈

    水题,主要是思路清晰,判断明确. 记x为A站最前方的车,y表示下一列要进入B站的车厢,初识时,x=1;y=a1;C=[]; 在调度过程中: if(y==0)那么调度成功,退出模拟过程:否则 if(x= ...

  6. POJ 1001 Exponentiation 模拟小数幂

    模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...

  7. POJ 1008 简单模拟题

    e.... 虽然这是一道灰常简单的模拟题.但是米做的时候没有读懂第二个日历的计时方法.然后捏.敲完之后华丽的WA了进一个点.坑点就在一年的最后一天你是该输出本年的.e ...但是我好想并没有..看di ...

  8. Crashing Robots POJ 2632 简单模拟

    Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is neede ...

  9. P3719 [AHOI2017初中组]rexp——递归模拟

    P3719 [AHOI2017初中组]rexp 没有什么算法的题做起来真不适应,这道题深深讽刺了我想用栈维护匹配括号个数的想法: 递归解决就行了: 时刻注意函数返回值是什么,边界条件是什么: #inc ...

随机推荐

  1. mq_open

    NAME mq_open - 打开一个消息队列 (REALTIME) SYNOPSIS #include <mqueue.h> mqd_t mq_open(const char *name ...

  2. AcmeAir安装AI探针--企业版

    通过脚本安装AI探针请点击通过脚本自动安装探针 一.安装企业版AI探针准备工作: 1. 准备好可用的docker版AcmeAir应用 2. 准备好可用的企业版AIServer 3. 下载好合适版本的J ...

  3. CentOS系统安装tomcat

    按照以下步骤操作即可wget http://mirrors.noc.im/apache/tomcat/tomcat-9/v9.0.0.M6/bin/apache-tomcat-9.0.0.M6.tar ...

  4. iOS 深入Objective-C的动态特性

    深入Objective-C的动态特性 Objective-C具有相当多的动态特性,基本的,也是经常被提到和用到的有动态类型(Dynamic typing),动态绑定(Dynamic binding)和 ...

  5. VS 解决方案目录结构设置

    涉及到的配置: 项目编译时使用的中间目录: 项目链接的输出目录: 项目编译结束后,拷贝生成的 dll 和 lib 到指定目录: 项目编译结束后,拷贝需要的 dll 到输出目录: 设置项目的工作目录: ...

  6. JBOSS内存溢出处理

    JBOSS内存溢出处理 前几天公司一个项目的服务器坏了,就换了一个备份服务器顶替一下,但是没有跑一会就宕机了,一直报java.lang.OutOfMemoryError....一看到这里,就知道是内存 ...

  7. android ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType)

    实例 <ImageView android:id="@+id/image" android:layout_width="fill_parent" andr ...

  8. [转载] 推荐的C++书籍以及阅读顺序

    2014-06-17 转载自 oiramario 的文章 推荐的C++书籍以及阅读顺序 当读者有一定c/c++基础 推荐的阅读顺序: level 1 从<<essential c++> ...

  9. leetcode:Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  10. 《OD学hadoop》第一周0626

    一.磁盘管理 Linux添加新硬盘.分区.格式化.自动挂载 http://lxsym.blog.51cto.com/1364623/321643 给Linux系统新增加一块硬盘 http://www. ...