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

分析:由于指数最多为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. 294. Flip Game II

    题目: You are playing the following Flip Game with your friend: Given a string that contains only thes ...

  2. 如何在github上展示作品——为你的项目生成一个快速访问的网址如(DaisyWang88.github.io)

      (这里值针对Windos系统的,因为本人用的是Window系统,暂时没有条件在其他平台上测试)   1.创建命名为 <userName>.github.io的仓库.      这里的u ...

  3. Android之网络请求库

    自己学习android也有一段时间了,在实际开发中,频繁的接触网络请求,而网络请求的方式很多,最常见的那么几个也就那么几个.本篇文章对常见的网络请求库进行一个总结. HttpUrlConnection ...

  4. ubuntu搭建svn、git遇到的问题及解决办法

    不错的git笔记博客: http://www.cnblogs.com/wanqieddy/category/406859.html http://blog.csdn.net/zxncvb/articl ...

  5. 习惯&感恩

    A不喜欢吃鸡蛋,都给了B吃,刚开始B很感谢,久而久之便习惯了.习惯了,便理所当然了. 后来有一天,A将鸡蛋给了C,B就不爽了.她忘记了这个鸡蛋本来就是A的,A想给谁都可以. 为此,她们大吵一架,从此绝 ...

  6. Linux Java 环境变量设置

    对整个机器:所有用户 1,在/etc/profile 文件末尾加入 export JAVA_HOME=/var/opt/jdk1.7.0_04 export PATH=$JAVA_HOME/bin:$ ...

  7. jxl导入/导出excel

    1.jxl导入/导出excel案例,黏贴即可运行 package junit.test; import java.io.File; import java.io.IOException; import ...

  8. 用vi修改文件,保存文件时,提示“readonly option is set”的解决方法

    来源:http://superuser.com/questions/300500/ubuntu-unable-to-edit-bashrc-file-because-of-readonly This ...

  9. Java关键字static、final

    static  1. static变量     按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的 ...

  10. 如何直接在github网站上更新你fork的repo?

    玩过github的人一定会在你自己的账号上fork了一些github开源项目.这些开源项目往往更新比较活跃,你今天fork用到你自己的项目中去了,过几个星期这个fork的origin可能有一些bugf ...