给出一段程序,求运行时间。

现在只考虑一层LOOP,不妨用数组a[i]来表示n的i次方的系数。如果输入OP m,那么就在a[0]上加m,遇到END,就说明循环结束了,需要在系数上乘以循环次数。如果次数为数字,那么每个系数都乘以它;如果为n,那么全部右移一位(是指把n^2的系数给n^3),记得a[0] = 0。

当有多层LOOP时,递归调用即可。

输出的时候需要注意几个错误:如 1*n^3 (应该为n^3), 2*n^1 (应该为n^2), 注意n^0的系数,只要不是0,就要输出。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
#include<set>
#include<map>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}} using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 100000000;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 15;
const int M = 1111111;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1}; char s1[N], s2[N];
int ans[N]; void solve(int *a, int p, int q)
{
int n;
while(RS(s1), s1[0] != 'E')
{
if(s1[0] == 'O')
{
RI(n);
a[0] += n;
}
else
{
RS(s2);
if(s2[0] == 'n')n = -1;
else sscanf(s2, "%d", &n);
int b[N] = {0};
solve(b, p + 1, n);
REP(i, N)a[i] += b[i];
}
}
if(q == -1)
{
FD(i, 12, 1)a[i] = a[i - 1];
a[0] = 0;
}
else FD(i, 12, 0)a[i] *= q;
} void print()
{
int f = 0;
FD(i, 12, 1)if(ans[i])
{
if(!f)f = 1;
else putchar('+');
if(ans[i] > 1)printf("%d*", ans[i]);
if(i == 1)putchar('n');
else printf("n^%d", i);
}
if(ans[0])
{
if(!f)f = 1;
else putchar('+');
printf("%d", ans[0]);
}
if(!f)printf("0");
printf("\n\n");
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout); int t, cas = 1;
RI(t);
while(t--)
{
CLR(ans, 0);
RS(s1);
solve(ans, 0, 1);
printf("Program #%d\nRuntime = ", cas++);
print();
}
return 0;
}

UVA 586 Instant Complexity的更多相关文章

  1. Instant Complexity - POJ1472

    Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Description Analyzing the run-time comple ...

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

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

  3. Instant Complexity(模拟,递归)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1535   Accepted: 529 Description Analyz ...

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

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

  5. 三部曲二(基本算法、动态规划、搜索)-1004-Instant Complexity

    Instant Complexity Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) ...

  6. poj1472[模拟题]

    Instant Complexity Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2017   Accepted: 698 ...

  7. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  8. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  9. 概率论 --- Uva 11181 Probability|Given

    Uva 11181 Probability|Given Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.acti ...

随机推荐

  1. ADO.Net技术

    Connection对象 1.连接数据库 通过SqlConnection对象的State属性判断数据库的连接状态: public override ConnectionState State{ get ...

  2. Oracle 特殊字符模糊查询的方法

    最近在写DAO层的时候,遇到一个问题,就是使用like进行模糊查询时,输入下划线,无法精确查到数据,而是返回所有的数据. 这让我很好奇,百度之后才发现,原来是因为有些特殊字符需要进行转义才可以进行查询 ...

  3. Liferay环境搭建实录

    1. 安装Liferay IDE:打开Eclipse的Help-->Eclipse Marketplace,在下图所示搜索框中输入liferay,回车搜索: 搜索结果如下图所示: 点击insta ...

  4. 【Windows】Windows中的数据类型以及命名

    一.大写标示符 Windows中的很多标识符都是以两个或者三个大写字母作为前缀的,且其后紧跟一个下划线.这些标识符都是常量数值,前缀表明该常量的一般类别.如下 前缀 常量 CS(Class Style ...

  5. jquery文本折叠

    /** * Created by dongdong on 2015/4/28. */(function($){ var defaults = { height:40, //文本收起后的高度 speed ...

  6. [转] 小tip: 使用CSS将图片转换成黑白(灰色、置灰) ---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2547 //zxx: ...

  7. linux制作文件系统

    1.获取文件系统源码并解压 这里使用的源码是天嵌提供的“root_qtopia_2.2.0_2.6.30.4_20100601.tar.bz2” #tar xvf root_qtopia_2..0_2 ...

  8. chrome:// 的秘密!!一些有用的命令!

    chrome:// .......命令 集结 Chrome 有很多的特性在界面菜单中是没有体现的,可以通过 chrome:// 命令来访问 我搜集了下面这些!!!当然也是在网上找的!有的我自己也不知道 ...

  9. WKWebView-b

    上一篇文章我们使用了JavaScriptCore框架重写了之前的示例,iOS8苹果偏爱HTML5,重构了UIWebVIew,给我们带来了WKWebView,使其性能.稳定性.功能大幅度提升,也更好的支 ...

  10. 既然HTTP1.1协议里每个连接默认都是持久连接,那么为何当今所有报文都在使用Connetion:Keep-Alive

    说白了,如果你发起时有,那么服务器支持,回应时也会有,不支持,也就没有了.所以一般客户端都会默认带着发,服务端返回不返回就是服务端的事了. 1. 支不支持长连接,关键在于服务端是否支持. 如果服务端不 ...