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

现在只考虑一层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. Unexpected CFBundleExecutable Key

    昨天晚上打包上传的时候报错: ERROR ITMS-90535: "Unexpected CFBundleExecutable Key. The bundle at 'Payload/xxx ...

  2. 使用passenger在Centos7部署Puma+Nginx+Ruby on Rails

    安装ruby环境 RVM(ruby版本管理工具)安装 gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A170311 ...

  3. Java的浮点数

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5160771. ...

  4. HDU 4336 Card Collector(容斥)

    题意:要收集n种卡片,每种卡片能收集到的概率位pi,求收集完这n种卡片的期望.其中sigma{pi} <=1; 思路:容斥原理.就是一加一减,那么如何算期望呢.如果用二进制表示,0表示未收集到, ...

  5. spring参数类型异常输出,SpringMvc参数类型转换错误输出

    spring参数类型异常输出, SpringMvc参数类型转换错误输出 >>>>>>>>>>>>>>>> ...

  6. 浅谈负载均衡之【tomcat分布式session共享】

    1)整理集成所需jar kryo-1.0.3.jar kryo-serializers-0.8.jar memcached-2.4.2.jar memcached-session-manager-1. ...

  7. IOS-图片上传到服务器

    //获取document 路径- (NSString *)getDocumentPath{    NSArray *paths = NSSearchPathForDirectoriesInDomain ...

  8. Github上图文演示仓库创建

    1.注册账号 https://github.com/ 2.注册完账号 创建仓库 a.点击按钮 Start a project b.填写信息,点击按钮 Create repository c.仓库信息( ...

  9. oracle建立表空间

    //创建临时表空间 create temporary tablespace test_temp tempfile 'E:\oracle\product\10.2.0\oradata\testserve ...

  10. easyui-combobox小Demo

    <script type="text/javascript"> $("#Function_TypeSelect").combobox({ onSel ...