1054: 黄黄的计算器II
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 13 Solved: 2
Submit Status Discuss
Description
我们知道黄黄是个喜欢怀旧的人,他总喜欢换把小学数学题拿出做,这样他就可以彰显自己智商多么高(很显然他在欺骗自己),但是黄黄的智商不是很高,对于解小学一元一次方程有点头疼,所以他希望写一个程序来实现解一元一次方程.
Input
包含多组样例,第一行一个数字T表示测试样例的个数,接下来T行,每行一个字符串表示一个一元一次方程.
方程的描述规则:由一种变量,整数以及+,-,=符号组成,数字和符号之间一定有空格,整数和变量之间没有空格,符号和变量之间不一定有空格,等号两边有空格.(如果觉得无法理解方程描述请认真参考输入样例)
保证构成方程的字符个数不会超过30.
Output
如果有解,只要输出一个保留两位小数的浮点数表示方程的解.
如果没有解,输出“no solution”(没有带引号)
提示:例如 x = x、1 = 1、x - 1 = x - 1这样的式子是被判定成无解的,也就是说如果最后x被消掉方程就会无解!
Sample Input
3
x + 2x = 3
-x - 3 = 0
1 = 1
Sample Output
1.00
-3.00
no solution
HINT
Source
2015年低年级程序设计大赛热身赛
就是各种模拟各种烦 数据范围还改小了 不然还得用大数
#include "cstring"
#include "string"
#include "iostream"
#include "cstdio"
#include "cmath"
#include "stack"
#define MIN -1
#define PLUS 1
using namespace std;
int main()
{
int ncase;
while (scanf("%d", &ncase) != EOF)
{
getchar();
while (ncase--)
{
char a[1000];
gets(a);
int i = 0;
int left_num = 0, left_num_x = 0;
int right_num = 0, right_num_x = 0;
int flag = 0;//前面没有数字
stack <char> list;
int symbol = PLUS;
// int symbol_num_x = PLUS;
int marklr = 0;
for (int j = 0;; j++)
{
if (a[j] == '\0')
break;
if (!((a[j] >= '0'&&a[j] <= '9') || a[j] == ' ' || a[j] == '+' || a[j] == '-' || a[j] == '='))
{
a[j] = 'x';
}
}
while (a[i] != '\0')
{
if (flag == 0&&a[i]=='x')
{
if (marklr == 0)
{
if (symbol == PLUS)
left_num_x++;
else
left_num_x--;
}
else
{
if (symbol == PLUS)
right_num_x++;
else
right_num_x--;
}
}
if (flag == 1 && a[i] == 'x')
{
int sum = 0;
int v = 1;
while (!list.empty())
{
sum += (list.top()-'0')*v;
list.pop();
v = v * 10;
}
if (marklr == 0)
{
if (symbol == PLUS)
left_num_x += sum;
else
left_num_x -= sum;
}
else
{
if (symbol == PLUS)
right_num_x += sum;
else
right_num_x -= sum;
}
}
if (a[i] >= '0'&&a[i] <= '9')
{
flag = 1;
list.push(a[i]);
}
if (a[i] == '+')
{
int sum = 0;
int v = 1;
while (!list.empty())
{
sum += (list.top()-'0')*v;
list.pop();
v = v * 10;
}
if (marklr == 0)
{
if (symbol == PLUS)
left_num += sum;
else
left_num -= sum;
}
else
{
if (symbol == PLUS)
right_num += sum;
else
right_num -= sum;
}
symbol = PLUS;
flag = 0;
}
if (a[i] == '-')
{
int sum = 0;
int v = 1;
while (!list.empty())
{
sum += (list.top()-'0')*v;
list.pop();
v = v * 10;
}
if (marklr == 0)
{
if (symbol == PLUS)
left_num += sum;
else
left_num -= sum;
}
else
{
if (symbol == PLUS)
right_num += sum;
else
right_num -= sum;
}
symbol = MIN;
flag = 0;
}
if (a[i] == '=')
{
int sum = 0;
int v = 1;
while (!list.empty())
{
sum += (list.top() - '0')*v;
list.pop();
v = v * 10;
}
if (marklr == 0)
{
if (symbol == PLUS)
left_num += sum;
else
left_num -= sum;
}
else
{
if (symbol == PLUS)
right_num += sum;
else
right_num -= sum;
}
marklr = 1;
symbol = PLUS;
flag = 0;
}
i++;
}
int sum = 0;
int v = 1;
while (!list.empty())
{
sum += (list.top() - '0')*v;
list.pop();
v = v * 10;
}
if (marklr == 0)
{
if (symbol == PLUS)
left_num += sum;
else
left_num -= sum;
}
else
{
if (symbol == PLUS)
right_num += sum;
else
right_num -= sum;
}
if (left_num == right_num&&left_num_x == right_num_x)
printf("no solution\n");
else
{
int totalx = left_num_x - right_num_x;
int totalnum = right_num - left_num;
if (totalx == 0)
{
printf("no solution\n");
continue;
}
double ans = totalnum*1.0 / totalx*1.0;
printf("%.2lf\n", ans);
}
}
}
}
/************************************************************** Problem: 1054 User: raphl Language: C++ Result: Accepted Time:4 ms Memory:1272 kb ****************************************************************/