java实现:
import java.util.Scanner;
import java.io.IOException;
import java.io.FileReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Formatter;
class Main
{
public static final boolean DEBUG = false;
public static void main(String[] args) throws IOException
{
Scanner cin;
Pattern p = Pattern.compile("(\\+|-|=)");
String s;
double a, b, c;
boolean bEqual, bMinus;
int loc1;
if (DEBUG) {
cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
} else {
cin = new Scanner(System.in);
}
while (cin.hasNext()) {
s = cin.next();
a = b = c = 0;
bEqual = bMinus = false;
loc1 = 0;
String sub;
Matcher m = p.matcher(s);
while (m.find()) {
sub = s.substring(loc1, m.start());
if (sub.indexOf('x') == -1) {
if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
if (sub.compareTo("") != 0) c += Integer.parseInt(sub);
} else {
if (sub.compareTo("") != 0)c -= Integer.parseInt(sub);
}
} else if (sub.indexOf('^') == -1) {
if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
if (sub.length() == 1) b += 1;
else b += Integer.parseInt(sub.substring(0, sub.length() - 1));
} else {
if (sub.length() == 1) b -= 1;
else b -= Integer.parseInt(sub.substring(0, sub.length() - 1));
}
} else {
if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
if (sub.length() == 3) a += 1;
else a += Integer.parseInt(sub.substring(0, sub.length() - 3));
} else {
if (sub.length() == 3) a -= 1;
else a -= Integer.parseInt(sub.substring(0, sub.length() - 3));
}
}
if (m.group().compareTo("=") == 0) bEqual = true;
else if (m.group().compareTo("-") == 0) bMinus = true;
else bMinus = false;
loc1 = m.end();
}
sub = s.substring(loc1, s.length());
if (sub.indexOf('x') == -1) {
if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
if (sub.compareTo("") != 0) c += Integer.parseInt(sub);
} else {
if (sub.compareTo("") != 0) c -= Integer.parseInt(sub);
}
} else if (sub.indexOf('^') == -1) {
if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
if (sub.length() == 1) b += 1;
else b += Integer.parseInt(sub.substring(0, sub.length() - 1));
} else {
if (sub.length() == 1) b -= 1;
else b -= Integer.parseInt(sub.substring(0, sub.length() - 1));
}
} else {
if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
if (sub.length() == 3) a += 1;
else a += Integer.parseInt(sub.substring(0, sub.length() - 3));
} else {
if (sub.length() == 3) a -= 1;
else a -= Integer.parseInt(sub.substring(0, sub.length() - 3));
}
}
if (b * b - 4 * a * c < 0) {
System.out.println("No Solution");
} else {
double x1 = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
double x2 = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
if (x1 > x2) {
double tmp = x1;
x1 = x2;
x2 = tmp;
}
Formatter fmt= new Formatter();
fmt.format("%.2f %.2f", x1, x2);
System.out.println(fmt);
}
}
}
}
C++实现:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
string s;
string sop = "+-=";
size_t loc1, loc2;
float a, b, c;
bool bEqual, bMinus;
#ifndef ONLINE_JUDGE
ifstream cin("d:\\OJ\\uva_in.txt");
#endif // ONLINE_JUDGE
while (cin >> s) {
loc1 = -1;
loc2 = 0;
bEqual = false;
bMinus = false;
a = b = c = 0;
while (loc2 < s.length()) {
loc2 = s.find_first_of(sop, loc1 + 1);
if (loc2 == string::npos) {
loc2 = s.length();
}
string sub = s.substr(loc1 + 1, loc2 - loc1 - 1);
//cout << "sub:" << sub << endl;
if (sub.find('x') == string::npos) {
if ((!bEqual && !bMinus) || (bEqual && bMinus))
c += atoi(sub.c_str());
else
c -= atoi(sub.c_str());
} else if (sub.find('^') == string::npos) {
if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
if (sub.length() == 1) {
b += 1;
} else {
b += atoi(sub.c_str());
}
} else {
if (sub.length() == 1) {
b -= 1;
} else {
b -= atoi(sub.c_str());
}
}
} else {
if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
if (sub.length() == 3) {
a += 1;
} else {
a += atoi(sub.c_str());
}
} else {
if (sub.length() == 3) {
a -= 1;
} else {
a -= atoi(sub.c_str());
}
}
}
loc1 = loc2;
if (loc2 < s.length() && s.at(loc2) == '-') bMinus = true;
else if (loc2 < s.length() && s.at(loc2) == '=') bEqual = true;
else bMinus = false;
//cout << "a:" << a << " b:" << b << " c:" << c << endl;
}
if (b * b - 4 * a * c < 0) {
cout << "No Solution" << endl;
} else {
float result1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
float result2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
if (result1 > result2) swap(result1, result2);
cout.precision(2);
cout.setf(ios::fixed);
cout << result1 << " " << result2 << endl;
}
}
return 0;
}