POJ 1472:Instant Complexity 模拟时间复杂度
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 1908 | Accepted: 658 |
Description
should be preferred.
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
END, LOOP and OP or in an integer value. The nesting depth of the LOOP-operators will be at most 10.
Output
terms, and print it in the form "Runtime = a*n^10+b*n^9+ . . . +i*n^2+ j*n+k", where terms with zero coefficients are left out, and factors of 1 are not written. If the runtime is zero, just print "Runtime = 0".
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
又是一道很麻烦的模拟。。。输出结果的时候有很多种情况要考虑。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#pragma warning(disable:4996)
using namespace std; int oper[15];
int coe[15];
string op, op2; int change(string x)
{
int res = 0;
int i, len = x.length();
for (i = 0; i < len; i++)
{
res = res * 10 + x[i] - '0';
}
return res;
}
int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int i, test, num, flag, another_flag;
int j, coe_one, n_num;
cin >> test; for (i = 1; i <= test; i++)
{
cout << "Program #" << i << endl;
cin >> op; memset(coe, 0, sizeof(coe));
num = 0; while (num != -1)
{
cin >> op;
if (op == "LOOP")
{
cin >> op2;
if (op2 == "n")
{
oper[num++] = -1;
}
else
{
oper[num++] = change(op2);
}
}
else if (op == "END")
{
num--;
}
else if (op == "OP")
{
cin >> op2;
coe_one = 1;
n_num = 0;
for (j = num - 1; j >= 0; j--)
{
if (oper[j] == -1)
{
n_num++;
}
else
{
coe_one = coe_one*oper[j];
}
}
coe[n_num] = coe[n_num] + coe_one * change(op2);
}
} cout << "Runtime = "; flag = 0;
another_flag = 0;
for (j = 14; j >= 0; j--)
{
if (coe[j] == 0)
{
continue;
}
else
{
another_flag = 1;
if (j == 0)
{
if (flag == 0)
{
flag = 1;
cout << coe[j];
}
else
{
cout << "+" << coe[j];
}
}
else if (j == 1)
{
if (flag == 0)
{
flag = 1;
if (coe[j] != 1)
cout << coe[j] << "*n";
else
cout << "n";
}
else
{
if (coe[j] != 1)
cout << "+" << coe[j] << "*n";
else
cout << "+n";
}
}
else
{
if (flag == 0)
{
flag = 1;
if (coe[j] != 1)
cout << coe[j] << "*n^" << j;
else
cout << "n^" << j;
}
else
{
if (coe[j] != 1)
cout << "+" << coe[j] << "*n^" << j;
else
cout << "+n^" << j;
}
} }
}
if (another_flag == 0)
cout << 0;
cout << endl << endl;
} //system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1472:Instant Complexity 模拟时间复杂度的更多相关文章
- POJ 1472 Instant Complexity 应该叫它编程题。。
题目:http://poj.org/problem?id=1472 这个题目是分到“模拟题”一类的,我觉得模拟的成分比较少,主要考察编程能力.独立写完这个题特别兴奋...所以我必须好好说一说,独家哦. ...
- Instant Complexity(模拟,递归)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1535 Accepted: 529 Description Analyz ...
- Instant Complexity - POJ1472
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Description Analyzing the run-time comple ...
- poj 1472(递归模拟)
题意:就是让你求出时间复杂度. 分析:由于指数最多为10次方,所以可以想到用一个数组保存各个指数的系数,具体看代码实现吧! 代码实现: #include<cstdio> #include& ...
- HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)
Description Too worrying about the house price bubble, poor Mike sold his house and rent an apartmen ...
- poj 2632 Crashing Robots 模拟
题目链接: http://poj.org/problem?id=2632 题目描述: 有一个B*A的厂库,分布了n个机器人,机器人编号1~n.我们知道刚开始时全部机器人的位置和朝向,我们可以按顺序操控 ...
- POJ 2014 Flow Layout 模拟
http://poj.org/problem?id=2014 嘻嘻2014要到啦,于是去做Prob.ID 为2014的题~~~~祝大家新年快乐~~ 题目大意: 给你一个最大宽度的矩形,要求把小矩形排放 ...
- POJ 2632 Crashing Robots (模拟 坐标调整)(fflush导致RE)
题目链接:http://poj.org/problem?id=2632 先话说昨天顺利1Y之后,直到今天下午才再出题 TAT,真是刷题计划深似海,从此AC是路人- - 本来2632是道略微恶心点的模拟 ...
- poj 1888 Crossword Answers 模拟题
Crossword Answers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 869 Accepted: 405 D ...
随机推荐
- JS 循环赋值
var x_world_map_tiles = 100; var y_world_map_tiles = 100; var world_map_array = []; for (i=0; i<= ...
- java实现经典排序算法
前言 博主最近在恶补基础知识,算法必然是必不可少的,那么就从经典排序算法开始吧!(图源网络,侵删),没有列出全部,因为在期末考试囧 代码太多,就只显示了最关键的部分 1. 冒泡排序 实现思路: (1) ...
- IntelliJ IDEA Debug模式启动项目
导入项目,没有看到debug模式 点击 next next next next
- 《React后台管理系统实战 :三》header组件:页面排版、天气请求接口及页面调用、时间格式化及使用定时器、退出函数
一.布局及排版 1.布局src/pages/admin/header/index.jsx import React,{Component} from 'react' import './header. ...
- C# Show()与ShowDialog()的区别-----转载
A.WinForm中窗体显示 显示窗体可以有以下2种方法: Form.ShowDialog方法 (窗体显示为模式窗体) Form.Show方法 (窗体显示为无模式窗体) 两者具体区别如下: 1 ...
- 微信小程序中,如何实现显示,隐藏密码的功能
最近在搞小程序的开发,遇到隐藏,显示密码的功能的时候,电脑上调试没问题,但是手机上面点击却没有效果,必须要跳转到其他页面再跳回来,才能正常显示. 一时间搞得我很头疼,查找资料后,终于知道了是什么原因. ...
- JavaScript引用类型与对象
1.引用类型 引用类型的值(对象)是引用类型的一个实例.引用类型有时候也被称为对象定义,因为它们描述的是一类对象所具有的属性和方法. 对象是某个特定引用类型的实例.新对象是使用new操作符后跟一个构造 ...
- 105、Java中String类之利用indexOf()方法判断子字符串是否存在
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- FTP大量数据(百万级数据)传输尝试
接上篇<Java FTPClient 大量数据传输的问题(未解决)> 因为数据量过大 FTPClient获取文件列表信息会出现不稳定的问题,找了很多方法都没有解决. 网上找的这一篇文章 h ...
- 使用自己定义的DIV的滚动条
基本思路: 让DIV浮动起来,利用postion:fixed/absolute,设定height:100% var $card=$("#cardDetail"); $ca ...