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 ...
随机推荐
- Android(java)学习笔记178:BroadcastReceiver之 自定义广播
广播使用: 电台:对外发送信号.---------电台发送广播(可以自定义) 收音机:接收电台的信号.-----广播接收者 这里,我们就说明自定 ...
- JavaScript 应用开发 #5:为完成的任务添加样式
判断一下任务的状态,如果是完成的任务,可以在任务项目的上面,添加一个额外的 css 类,在这个 css 类里,可以去定义完成的任务的样式.比如,把文字的颜色变成浅友色,并且在文字上面添加一条删除线.这 ...
- BrainTree信用卡包
BrainTree是一个国外集成信用卡支付的卡包. 沙盒登陆地址: https://sandbox.braintreegateway.com/login 登陆沙盒得到商户ID.公钥.私钥. 1.配置w ...
- ASP.NET MVC——Controller的激活
Controller的激活是根据在路由过程得到的Controller名称来创建对应的Controller对象.相关类如图: Controller激活的过程可通过如下序列图表示: 代码示例如下: str ...
- 自动化构建jenkins配置
1.安装jdk7+tomcat7 2.下载msi安装文件(我是win7上安装,下载地址 http://Jenkins-ci.org/),文件安装路径选择\tomcat\webapps. 3.安装成功之 ...
- 对于百川SDK签名验证的问题
SDK是要在wantu.taobao.com生成的.而生成这个SDK其实是要上传一个apk,而这个上传其实就是取他的签名而已.验证就是那张yw222那张图片.重点是你上传的apk的签名是不是跟你的生成 ...
- 建立一个方法的attribute,可以放在任意方法上,可以自动记录方法出错时的信息,就不用写try 。。cacth. 【注意】 不是在asp.net MVC下,是在普通三层结构下写的的特性。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- ios 添加通用断点定位到异常点
今天下午项目突然异常崩溃,因为代码没有多少结构改动,恢复旧版本还是不行,判定为网络获取信息异常,无奈从网络获取的信息太多,搞了很久后发现有个通用异常断点很好用,新手就是新手,浪费时间了.
- hdu Train Problem I(栈的简单应用)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- 复选框字段数组拆分后循环选项值,if判断根据选项值,前端输出html
{php $specials = explode(',',$r[special]);} <div class="special"> {loop $specials $s ...