UVA 586 Instant Complexity
给出一段程序,求运行时间。
现在只考虑一层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的更多相关文章
- Instant Complexity - POJ1472
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Description Analyzing the run-time comple ...
- POJ 1472:Instant Complexity 模拟时间复杂度
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1908 Accepted: 658 ...
- Instant Complexity(模拟,递归)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1535 Accepted: 529 Description Analyz ...
- POJ 1472 Instant Complexity 应该叫它编程题。。
题目:http://poj.org/problem?id=1472 这个题目是分到“模拟题”一类的,我觉得模拟的成分比较少,主要考察编程能力.独立写完这个题特别兴奋...所以我必须好好说一说,独家哦. ...
- 三部曲二(基本算法、动态规划、搜索)-1004-Instant Complexity
Instant Complexity Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
- poj1472[模拟题]
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2017 Accepted: 698 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- 概率论 --- Uva 11181 Probability|Given
Uva 11181 Probability|Given Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.acti ...
随机推荐
- JavaScript 开发规范要求详解
作为一名开发人员(We前端JavaScript开发),不规范的开发不仅使日后代码维护变的困难,同时也不利于团队的合作,通常还会带来代码安全以及执行效率上的问题.本人在开发工作中就曾与不按规范来开发的同 ...
- 【Android】Intent的使用-返回数据给上一个活动
第一个Activity A启动另外一个Activity B,B返回数据给A ============================================================= ...
- Windows Server 2008 R2 安装及配置指南
一.安装需求: 1. 硬件需求条件 硬件 需求 处理器 最低:1.4 GHz(x64处理器) 注意:Windows Server 2008 for Itanium-Based Systems 版本需要 ...
- tar命令解压、压缩gz/bz2/xz文件
1.处理.tar.gz 压缩:tar zcf FILE.tar.gz FILEDIR 解压:tar zxf FILE.tar.gz 2.处理.tar.bz2 压缩:tar jcf FILE.tar.b ...
- CSS样式表其它知识点
1.cursor:pointer鼠标放到上面变形状,pointer为手 2.margin:auto 页面居中 3.显示方式:dispaly:none不显示/block 块换行/inline在一行上,宽 ...
- Angular2 - Starter - Component and Component Lifecircle Hooks
我们通过一个NgModule来启动一个ng app,NgModule通过bootstrap配置来指定应用的入口组件. @NgModule({ bootstrap: [ AppComponent ], ...
- C语言基础知识小总结(1)
这几天在学习C语言,零零散散的学了十来天,这两天由于家里来了朋友,也没有顾得上写个总结,今天刚把朋友送走,下面就把这十来天的学习情况总结一下,一边在以后好复习与查看. 一.流程控制包括:顺序语句.判断 ...
- 如何打包成jar包自己看呢?
第一步:选择你要导出的部分 第二步:
- J2EE入门必备
1,J2EE是什么 J2EE(Java 2 platform Enterprise Edition)是软件平台,适于创建服务器端的大型应用软件和服务系统. J2EE适合开发大规模的业务系统,这种级别的 ...
- Unix时间戳 和 NSDate 的转换
一个时间戳字符串:NSString *timeStampStr = @"1441602721"; 转换成时间 double unixTimeStamp ...