算法刷题记录:[NOIP2000]计算器的改良(未AC)
题目链接
https://ac.nowcoder.com/acm/contest/19306/1043
题目分析
模拟就完事,代码写的很屎山,估计哪里死循环了,要不就是TLE,明天改。
未AC代码
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
const int N = 100010;
string equation;
// a:字母,b:数字
// flag:true -> 字母
int main()
{
cin >> equation;
// equation = "6a-5+1=2-2a";
int equal = 0;
char c;
for (int i = 0; i < equation.size(); ++ i)
{
if (equation[i] == '=') equal = i;
if (equation[i] >= 97 && equation[i] <= 122) c = equation[i];
}
float a = 0, b = 0;
for (int i = 0; i < equal; ++ i)
{
if (!i && equation[i] != '-')
{
int j = i, flag = false, tmp = 0, bit = 1;
int stk[10], tt = -1;
if (equation[j] >= 97 && equation[j] <= 122) tmp = 1;
else
{
while (equation[j] >= '0' && equation[j] <= '9')
stk[ ++ tt] = equation[j ++ ] - '0';
}
if (equation[j] >= 97 && equation[j] <= 122) flag = true;
while (tt != -1) tmp += stk[tt -- ] * bit, bit *= 10;
if (flag) a += tmp;
else b -= tmp;
i = j - 1;
}
if (equation[i] == '-' || equation[i] == '+')
{
bool op = true, flag = false;
if (equation[i] == '-') op = false;
int j = i + 1, tmp = 0, bit = 1;
int stk[10], tt = -1;
// 开头就是字母的情况
if (equation[j] >= 97 && equation[j] <= 122) tmp = 1;
else
{
while (equation[j] >= '0' && equation[j] <= '9')
stk[ ++ tt] = equation[j ++ ] - '0';
}
if (equation[j] >= 97 && equation[j] <= 122) flag = true;
while (tt != -1)
tmp += stk[tt -- ] * bit, bit *= 10;
if (flag)
{
if (op) a += tmp;
else a -= tmp;
}
else
{
if (op) b -= tmp;
else b += tmp;
}
i = j - 1;
}
}
for (int i = equal + 1; i < equation.size(); ++ i)
{
if (i == equal + 1 && equation[i] != '-')
{
int j = i, flag = false, tmp = 0, bit = 1;
int stk[10], tt = -1;
if (equation[j] >= 97 && equation[j] <= 122) tmp = 1;
else
{
while (equation[j] >= '0' && equation[j] <= '9')
stk[ ++ tt] = equation[j ++ ] - '0';
}
if (equation[j] >= 97 && equation[j] <= 122) flag = true;
while (tt != -1) tmp += stk[tt -- ] * bit, bit *= 10;
if (flag) a -= tmp;
else b += tmp;
i = j - 1;
}
if (equation[i] == '-' || equation[i] == '+')
{
bool op = true, flag = false;
if (equation[i] == '-') op = false;
int j = i + 1, tmp = 0, bit = 1;
int stk[10], tt = -1;
// 开头就是字母的情况
if (equation[j] >= 97 && equation[j] <= 122) tmp = 1;
else
{
while (equation[j] >= '0' && equation[j] <= '9')
stk[ ++ tt] = equation[j ++ ] - '0';
}
if (equation[j] >= 97 && equation[j] <= 122) flag = true;
while (tt != -1)
tmp += stk[tt -- ] * bit, bit *= 10;
if (flag)
{
if (op) a -= tmp;
else a += tmp;
}
else
{
if (op) b += tmp;
else b -= tmp;
}
i = j - 1;
}
}
cout << c << '=' << fixed << setprecision(3) << b /a << endl;
}
本文来自博客园,作者:想个昵称好难ABCD,转载请注明原文链接:https://www.cnblogs.com/ClockParadox43/p/17444644.html