POJ2252解题报告 解一元一次方程

Equation Solver
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 427 Accepted: 266

Description

Write a program that can solve linear equations with one variable.

Input

The input will contain a number of equations, each one on a separate line. All equations are strings of less than 100 characters which strictly adhere to the following grammar (given in EBNF):
Equation   := Expression '=' Expression

Expression := Term { ('+' | '-') Term }
Term := Factor { '*' Factor }
Factor := Number | 'x' | '(' Expression ')'
Number := Digit | Digit Number
Digit := '0' | '1' | ... | '9'

Although the grammar would allow to construct non-linear equations like "x*x=25", we guarantee that all equations occuring in the input file will be linear in x. We further guarantee that all sub-expressions of an equation will be linear in x too. That means, there won't be test cases like x*x-x*x+x=0 which is a linear equation but contains non-linear sub-expressions (x*x).
Note that all numbers occuring in the input are non-negative integers, while the solution for x is a real number.

Output

For each test case, print a line saying "Equation #i (where i is the number of the test case) and a line with one of the following answers:
  • If the equation has no solution, print "No solution.".
  • If the equation has infinitely many solutions, print "Infinitely many solutions.".
  • If the equation has exactly one solution, print "x = solution" where solution is replaced by the appropriate real number (printed to six decimals).

Print a blank line after each test case.

Sample Input

x+x+x=10
4*x+2=19
3*x=3*x+1+2+3
(42-6*7)*x=2*5-10

Sample Output

Equation #1
x = 3.333333

Equation #2
x = 4.250000

Equation #3
No solution.

Equation #4
Infinitely many solutions.
题意:解一元一次方程
思路:设置两个整型,一个保存当前得到的常数,另一个保存X的系数,然后递归求解.....
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值