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 ...
随机推荐
- Linux Device Driver 3th 中的一些坑
linux设备驱动第三版由于年代比较久远,有很多东西已过时.开一贴记录自己发现的一些问题. 4.3.1.4. seq_file接口 此节最后提到用 struct proc_dir_entry* cre ...
- 汉诺塔-Hanoi
1. 问题来源: 汉诺塔(河内塔)问题是印度的一个古老的传说. 法国数学家爱德华·卢卡斯曾编写过一个印度的古老传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度教的主神梵 ...
- linux中history命令使用与配置
history中设置显示命令的执行时间 vi /root/.bashrc HISTTIMEFORMAT="%Y-%M-%D %H:%M:%S" export HISTTIMEFOR ...
- css(display,float,position)
display 用来设置元素的显示方式 display : block | none | inline | inline-block inline:指定对象为内联元素 block:指定对象为块元素 i ...
- PHP 解决未定义变量报错
在PHP中 有时候会出现 Notice: Undefined index: sid in D:\Apache Group\Apache2\htdocs\php_mobile\mobile\chao\s ...
- 层叠样式优先级CSS
按照W3School网站(点这里直达)的说法,当同一个 HTML 元素被不止一个样式定义时,它们是有优先级之分的,如下,将优先级从小到大排列出来,其中4的优先级最高: 1.浏览器缺省设置2.外部样式表 ...
- Css3渐变(Gradients)-径向渐变
CSS3径向渐变-radial-gradient()/repeating-radial-gradient() 径向渐变由它的中心定义. 创建径向渐变,至少定义两种颜色节点,呈现平稳过度的颜色.同时,你 ...
- .net 学习路线感想
从上到大学到现在工作,已经有六年多了,发现学习编程到以开发为工作也是一个挺长的过程的. 大学中,从c语言到java.C#到其他各种语言的学习,还有其他知识的学习如:数据库(oracle.sql Ser ...
- Android中Cursor类的概念和用法
http://blog.sina.com.cn/s/blog_618199e60101fskp.html 使用过 SQLite数据库的童鞋对 Cursor 应该不陌生,加深自己和大家对Android ...
- ios进行打包
原文转载:http://blog.csdn.net/azhou_hui/article/details/9058677 公司刚搞了个299美刀的仅提供真机测试的企业账号,这个不需要添加设备ID,而 ...