pku 1472 Instant Complexity
来源:互联网 发布:2017淘宝捉猫猫捉不到 编辑:程序博客网 时间:2024/05/16 12:03
纠结的一道模拟题 ,快被它弄崩溃了,不做评论,栈模拟做的,
需要注意的地方有:注意LOOP的参数为0的情况,然后输出的格式相当复杂,但我的错误不是这两个。
我是错在这种情况:
BEGIN LOOP n OP 1 END LOOP n OP 1 END END
正确的答案应该是 2*n 我之前的结果好象是 n^2+n
也就是没有考虑到LOOP 并行的情况,当成总是嵌套的来处理了。 这个错误弄了n久才发现,太纠结了。
上代码了:
#include <iostream>
#include <stack>
#include <cstring>
#include <cstdio>
using namespace std;
int x[15];
struct op
{
string cmd;
string ope;
int s[15];
};
int main()
{
int ca(1);
int T;
cin>>T;
while (T--)
{
stack<op> a;
string cmd;
string ope;
op tmp;
memset(x,0,sizeof(x));
cin>>cmd;
tmp.cmd = cmd;
a.push(tmp);
while (!a.empty())
{
cin>>cmd;
if (cmd!="END")
{
cin>>ope;
tmp.cmd = cmd;
tmp.ope = ope;
a.push(tmp);
}
else
{
int value;
while (true)
{
op top = a.top();
a.pop();
if (top.cmd=="OP")
{
sscanf(top.ope.c_str(),"%d",&value);
x[0]+=value;
}
else if (top.cmd=="LOOP")
{
int k;
if (top.ope!="n")
{
sscanf(top.ope.c_str(),"%d",&value);
for (k=0;k<=10;++k)
x[k]*=value;
}
else
{
for (k=10;k>0;--k)
x[k]=x[k-1];
x[0]=0;
}
tmp.cmd = "temp";
for (k=0;k<=10;++k)
{
tmp.s[k] = x[k];
x[k] = 0;
}
a.push(tmp);
break;
}
else if (top.cmd=="temp")
{
for (int k = 0;k<=10;++k)
x[k] += top.s[k];
}
else break;
}
}
}
cout<<"Program #"<<ca++<<endl<<"Runtime = ";
int zero(0);
bool first(1);
int j=10;
while (j>=0)
zero+=x[j--];
if (!zero)
{
cout<<0<<endl<<endl;
continue;
}
j=10;
while (j>=0)
{
if (x[j])
{
if (!first) cout<<"+";
if (x[j]>1||j==0) cout<<x[j];
if (j>=1&&x[j]>1) cout<<"*";
if (j>=1) cout<<"n";
if (j>1) cout<<"^"<<j;
first = false;
}
--j;
}
cout<<endl<<endl;
}
return 0;
}
- pku 1472 Instant Complexity
- Poj 1472 Instant Complexity
- POJ 1472 Instant Complexity
- POJ 1472 Instant Complexity
- POJ 1472 Instant Complexity
- Poj 1472 Instant Complexity
- poj-1472 Instant Complexity
- poj 1472 Instant Complexity
- poj 1472 Instant Complexity
- Instant Complexity
- POJ 1472:Instant Complexity 模拟时间复杂度
- Instant Complexity poj 1472(未完善)
- POJ 1472Instant Complexity(模拟+dfs)
- POJ1472浅析------Instant Complexity
- uva 586 Instant Complexity
- UVA 586 Instant Complexity
- UVA - 586 Instant Complexity
- ZOJ 1246 Instant Complexity
- 汉字显示乱码(中文OS,英文SQL Server 2005)
- 由一本电子书发现的一位牛人 《linux 内核修炼之道精华版之方法论》--任桥伟 blog.csdn.net/fudan_abc
- 关于js中"window.location.href"、"location.href"、"parent.location.href"、"top.location.href"的用法
- 《交流》
- 常用脚本 – perl登陆
- pku 1472 Instant Complexity
- 数组初始化
- 常用脚本 – perl监控windows
- 单件模式
- 在Hibernate中实现复杂的数据映射
- 打印机无法删除任务
- 走出来
- 借口
- Andriod 如何添加自定义C++库