题目链接:uva 586 - Instant Complexity
题目大意:给出一段程序,求程序的执行语句的次数。
解题思路:DFS,传入两个参数,一个n的指数,一个常数的系数。遇到LOOP n,就递归调用,并且n+1,遇到LOOP num,就常数系数乘上num。输出要按照正常格式,
- 首项不需要+号
- 系数为1不需要输出
- 系数为0整项不显示
- 指数为1不显示
- 次数为0时要输出0
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 105;
int a[maxn];
char order[maxn];
void dfs (int n, int k) {
int op = 0, x;
while (scanf("%s", order) == 1 && strcmp(order, "END") ) {
if (strcmp(order, "LOOP") == 0) {
scanf("%s", order);
if (strcmp(order, "n") == 0)
dfs(n+1, k);
else {
sscanf(order, "%d", &x);
dfs(n, k * x);
}
} else if (strcmp(order, "OP") == 0) {
scanf("%d", &x);
op += x;
}
}
a[n] += op * k;
}
void put (int i, int flag) {
if (a[i] == 0)
return;
if (flag)
printf("+");
if (a[i] > 1 && i)
printf("%d*", a[i]);
else if (i == 0)
printf("%d", a[i]);
if (i)
printf("n");
if (i > 1)
printf("^%d", i);
}
int main () {
int cas;
scanf("%d", &cas);
for (int kcas = 1; kcas <= cas; kcas++) {
memset(a, 0, sizeof(a));
scanf("%s", order);
dfs(0, 1);
int mv = 10;
while (mv >= 0 && a[mv] == 0) mv--;
printf("Program #%d\nRuntime = ", kcas);
if (mv != -1) {
put(mv, 0);
for (int i = mv-1; i >= 0; i--)
put(i, 1);
} else
printf("0");
printf("\n\n");
}
return 0;
}
分享到:
相关推荐
北大POJ1472-Instant Complexity 解题报告+AC代码
POJ1472-Instant Complexity 测试数据。数据来源:Southwestern European Regional Contest 1997
Domain-Driven Design - Tackling Complexity in the Heart of Software
IE598NH-lecture-20-Lower Complexity Bound in Stochastic Optimization.pdfIE598NH-lecture-20-Lower Complexity Bound in Stochastic Optimization.pdf
Programming Scala Tackle Multi-Core Complexity on the Java Virtual Machine
Programming.Scala_Tackle.Multi-Core.Complexity.on.the.Java.Virtual.Machine[2009][EN][PDF] Programming Scala: Tackle Multi-Core Complexity on the Java Virtual Machine by Venkat Subramaniam Scala is ...
【国外SOMEIP学习资料】Ethernet-Development-Ways-to-Keeping-the-complexity
OFDM 系统中一种低复杂度的信道估计方法,胡蝶,周小林,信道估计对于OFDM系统来说非常关键。本文提出一种基于训练序列的低复杂度信道估计方法。通过对训练序列进行仔细设计,接收端在估��
用法添加开发依赖: npm install --save-dev eslint-config-code-complexity 要么yarn add -D eslint-config-code-complexity 在 eslint 配置文件(例如.eslintrc.json )中添加扩展配置: ..."extends": ["eslint-...
Algorithm-.NET-Big-O-Algorithm-Complexity-Cheat-Sheet.zip,net和计算机科学中常用算法的大o复杂性。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
安装通过npm安装软件包npm install -S graphql-query-complexity用法创建具有最大查询复杂度的规则: import queryComplexity , { simpleEstimator} from 'graphql-query-complexity' ;const rule = queryComplexity...
Convex Optimization - Algorithms and Complexity Sébastien Bubeck Theory Group, Microsoft Research
DSSS信号扩频码低复杂度估计算法,唱亮,汪芙平,扩频码的估计是实现直接扩频(DSSS)信号盲解调的先决条件。本文提出两个实际中非常有效的统计量实现扩频码的同步和估计,同时该��
CCSDS 124.0-b-1 Robust Compression of Fixed-Length Housekeeping Data CCSDS 122.1-b-1 Spectral...CCSDS 123.0-B-1 Low-Complexity Lossless and Near-Lossless Multispectral and Hyperspectral Image Compression
一种低复杂度的线性离散码发射天线选择技术A low complexity transmit antenna selection for linear discrete code technology
$ [sudo] npm install -g flow-type-complexity 用法 $ flow-type-complexity 默认情况下,仅显示5种复杂度最高的类型。 要查看所有类型的复杂性,请使用选项-a (或--show-all )。 查看help : $ flow-type-...
安装pip install flake8-cognitive-complexity例子def f ( a , b ): if a : for i in range ( b ): if b : return 1 用法: $ flake8 --max-cognitive-complexity=3 test.pytext.py:1:5: CCR001 Cog
这是关于算法设计与复杂性分析的电子书,高清,最新版本,经典著作,英文版
Multi-scale Complexity Analysis of Muscle Coactivation during Gait in Children with Cerebral Palsy