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

分析:由于指数最多为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. RESTful登录设计(基于Spring及Redis的Token鉴权)

    转载自:http://www.scienjus.com/restful-token-authorization/ http://m.blog.csdn.net/article/details?id=4 ...

  2. Android核心分析之二十八Android GDI之Surface&Canvas

    Surface&Canvas Canvas为在画布的意思.Android上层的作图几乎都通过Canvas实例来完成,其实Canvas更多是一种接口的包装.drawPaints ,drawPoi ...

  3. web.xml配置bug之提示The content of element type "web-app" must match "(icon?,display- name?,description?,distributable?,

    错误:配置web.xml时,出现红色叉叉,提示 The content of element type "web-app" must match "(icon?,disp ...

  4. sin=in.readLine();

    import java.io.*; public class LineIO{ public static void main(String[] args) { String sin,inputStri ...

  5. Android笔记——简单解析XML

    两部分,Xml资源文件和Java对Xml解析的实现 ----------------------------------------------------------- 版权声明:本文为博主原创文章 ...

  6. DB2_SQL_常用知识点&实践

    DB2_SQL_常用知识点&实践 一.删除表中的数据(delete或truncate) 1 truncate table T_USER immediate; 说明:Truncate是一个能够快 ...

  7. JBoss 性能优化(解决Jboss内存紧张的问题)

    修改$JBOSS_HOME/bin/run.conf文件   JAVA_OPTS="-Xms 520m -Xmx 1220m -Xss 15120k +XX:AggressiveHeap&q ...

  8. svn执行clean up命令时报错或被锁定的解决方法

    用SVN经常出现被锁定而无法提交的问题,选择解锁又提示没有文件被锁定,很是头疼.这里整理了一下SVN 被锁定的几种解决方法: 1.出现这个问题后使用“清理”即"Clean up"功 ...

  9. uboot命令分析+实现【转】

    转自:http://xouou.iteye.com/blog/2150061 先贴一个重要结构,位于uboot/include/command.h,这个结构代表每个uboot命令 struct cmd ...

  10. iPhone 已停用

    如果你的iPhone上出现了如下的显示,你可以参考苹果官网上的  iOS设备已停用 如果你看到了这篇文章,你比我幸运多了. 参考这一个条目,你也许就不会丢失里面的数据了. 可怜的我,出现这个问题时还没 ...