Instant Complexity(模拟,递归)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 1535 | Accepted: 529 |
Description
Generally, one determines the run-time of an algorithm in relation to the `size' n of the input, which could be the number of objects to be sorted, the number of points in a given polygon, and so on. Since determining a formula dependent on n for the run-time of an algorithm is no easy task, it would be great if this could be automated. Unfortunately, this is not possible in general, but in this problem we will consider programs of a very simple nature, for which it is possible. Our programs are built according to the following rules (given in BNF), where < number > can be any non-negative integer:
< Program > ::= "BEGIN" < Statementlist > "END"
< Statementlist > ::= < Statement > | < Statement > < Statementlist >
< Statement > ::= < LOOP-Statement > | < OP-Statement >
< LOOP-Statement > ::= < LOOP-Header > < Statementlist > "END"
< LOOP-Header > ::= "LOOP" < number > | "LOOP n"
< OP-Statement > ::= "OP" < number >
The run-time of such a program can be computed as follows: the execution of an OP-statement costs as many time-units as its parameter specifies. The statement list enclosed by a LOOP-statement is executed as many times as the parameter of the statement indicates, i.e., the given constant number of times, if a number is given, and n times, if n is given. The run-time of a statement list is the sum of the times of its constituent parts. The total run-time therefore generally depends on n.
Input
Output
Output a blank line after each test case.
Sample Input
2
BEGIN
LOOP n
OP 4
LOOP 3
LOOP n
OP 1
END
OP 2
END
OP 1
END
OP 17
END BEGIN
OP 1997 LOOP n LOOP n OP 1 END END
END
Sample Output
Program #1
Runtime = 3*n^2+11*n+17 Program #2
Runtime = n^2+1997 简直是想半天也无从下手的一道题,不过个人感觉通过这道题对递归了解的更深刻了点,不过自己写递归,还是不会写。。。sad.加油吧
大致题意:
给出一段Pascial程序,计算其时间复杂度(能计算的项则计算,不能计算则化到最简的关于n的表达式O(n),并把各项根据n的指数从高到低排列),输出时,系数为0的项不输出,系数为1的项不输出系数,指数为1的项不输出指数。
一段程序只有唯一一个BEGIN,代表程序的开始。与其对应的为最后的END,代表程序的结束。
一段程序最多只有10层循环嵌套,循环的入口为LOOP,一个LOOP对应一个END,代表该层循环的结束。
一段程序中OP的个数不限。
LOOP是循环的入口,其后面的数据可能是常量(非负整数),也可能是变量n,代表循环体执行的次数。
OP是语句,其后面的数据只能为常量(非负整数),代表该语句执行的次数。
还要注意输出,指数为1的不输出指数,系数为1的不输出系数,系数为0的不输出,指数为0的只输出常数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void Loop(int *ans, char *LpNum)//LpNum代表该循环的次数
{
char s[],s1[] = {};
while(scanf("%s",s) && s[] != 'E')
{
if(s[] == 'L')//相当于一个大循环嵌套的小循环,是一个子结构,所以,递归调用
{
int *tmp = new int [];
memset(tmp,,*sizeof(int));//暂存数组tmp
scanf("%s",s1);
Loop(tmp,s1); for(int i = ; i <= ; i++)
ans[i] += tmp[i];
}
else if(s[] == 'O')
{
int x;
scanf("%d",&x);
ans[] += x;
}
} if(LpNum[] == 'n')
{
for(int i = ; i > ; i--)
ans[i] = ans[i-];
ans[] = ;
}
else
{
int x = atoi(LpNum);
for(int i = ; i <= ; i++)
ans[i] *= x;
}
} int main()
{
int test;
int ans[];
scanf("%d",&test);
for(int item = ; item <= test; item++)
{
char s[];
scanf("%s",s);
memset(ans,,sizeof(ans)); Loop(ans,"");//把最外面的大循环次数当做1 printf("Program #%d\n",item);
printf("Runtime = "); bool tag = ;
for(int i = ; i >= ; i--)
{
if(ans[i] == )
continue;
if(i == )
printf("%s%d",tag?"":"+",ans[]);
else if(ans[i] == )
printf("%sn",tag?"":"+");
else if(ans[i] > )
printf("%s%d*n",tag?"":"+",ans[i]);
if(i > )
printf("^%d",i);
tag = ;
}
if(tag)
printf("");
printf("\n\n"); }
return ;
}
Instant Complexity(模拟,递归)的更多相关文章
- POJ 1472:Instant Complexity 模拟时间复杂度
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1908 Accepted: 658 ...
- Instant Complexity - POJ1472
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Description Analyzing the run-time comple ...
- python--递归(附利用栈和队列模拟递归)
博客地址:http://www.cnblogs.com/yudanqu/ 一.递归 递归调用:一个函数,调用的自身,称为递归调用 递归函数:一个可以调用自身的函数称为递归函数 凡是循环能干的事,递归都 ...
- 深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)
将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...
- 【ACwing 93】【模版】非递归实现组合型枚举——模拟递归
(题面来自ACwing) 从 1~n 这 n 个整数中随机选出 m 个,输出所有可能的选择方案. 输入格式 两个整数 n,m ,在同一行用空格隔开. 输出格式 按照从小到大的顺序输出所有方案,每行1个 ...
- POJ 1472 Instant Complexity 应该叫它编程题。。
题目:http://poj.org/problem?id=1472 这个题目是分到“模拟题”一类的,我觉得模拟的成分比较少,主要考察编程能力.独立写完这个题特别兴奋...所以我必须好好说一说,独家哦. ...
- 【模拟+递归+位运算】POJ1753-Flip Game
由于数据规模不大,利用爆搜即可.第一次用位运算写的,但是转念一想应该用递归更加快,因为位运算没有剪枝啊(qДq ) [思路] 位运算:时间效率较低(172MS),有些辜负了位运算的初衷.首先将二维数组 ...
- UVA 586 Instant Complexity
给出一段程序,求运行时间. 现在只考虑一层LOOP,不妨用数组a[i]来表示n的i次方的系数.如果输入OP m,那么就在a[0]上加m,遇到END,就说明循环结束了,需要在系数上乘以循环次数.如果次数 ...
- Atcoder Beginner Contest 115 D Christmas 模拟,递归 B
D - Christmas Time limit : 2sec / Memory limit : 1024MB Score : 400 points Problem Statement In some ...
随机推荐
- mac 下svn降级
mac 手欠 homebrew 安装完成后 brew install svn svn版本更新至1.8.11 公司svn 不支持1.8 需要降级 搜索很多资料 写的比较麻烦 总结出来是先卸载再 ...
- 一次优化web项目的经历记录(三)
一次优化web项目的经历记录 这段时间以来的总结与反思 前言:最近很长一段时间没有更新博客了,忙于一堆子项目的开发,严重拖慢了学习与思考的进程. 开水倒满了需要提早放下杯子,晚了就会烫手,这段时间以来 ...
- unity介绍
- css3购物网站商品文字提示实例
css3购物网站商品文字提示实例先来看效果图:<ignore_js_op> 当鼠标划过图片时,有着泰迪熊黑色长方形的背景就会出现.来看HTML5+CSS3代码: <!DOCTYPE ...
- .NET 菜单如何链接到指定的框架
这2天我看了网络上很多关于这方面的资料,很多都是抄人家的,要不就是没图说个jiba,要不就是没有说到重点,浪费大家的时间,今天我把我的心得给大家分享下,希望对大家有所帮助. 一.首先,你需要简历一个框 ...
- postgre sql 字符串转为integer类型
select cast(setting_value as integer) from ud_organization_setting. select cast('123123' as integer) ...
- CentOS 7重装mysql编译过程报错解决方法
错误记录: [ 82%] Building C object libmysql/CMakeFiles/clientlib.dir/__/sql-common/client.c.o/usr/local/ ...
- javascript基础学习(四)
javascript之流程控制语句 学习要点: 表达式语句含义 选择语句:if.if...else.switch 循环语句:while.do...while.for.for...in 跳转语句:bre ...
- 数据挖掘经典书籍[ZZ]
数据挖掘就是在数据库中查找所需数据的过程,它是随着数据库产生的一门学科.近几年,数据库的发展还是非常迅速的,数据挖掘也成为热门技术,学习的人络绎不绝.下面给大家介绍的就是数据挖掘经典书籍及数据挖掘书籍 ...
- .NET Framework(二)
在上一篇的随笔中,我们在理论层面上大致说明了.NET Framework的工作机制,内容的确比较晦涩难懂,但是还是希望大家有时候可以看看.我个人觉得,编程不是一味的敲代码,当自己遇到瓶颈的时候,可以多 ...