2013
12-12

# 九度-1103-二次方程计算器[解题代码]

x^2+x=3x+4

-1.24 3.24

java 代码如下：
import java.io.BufferedInputStream;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
static int arr[];
static String regx[] = { "([+-]?)(\\d*)x\\^2", "([+-]?)(\\d*)x",
"([+-]?)(\\d+)" };
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
while (sc.hasNextLine()) {
String str = sc.nextLine().replaceAll("\\s", "");
StringBuffer sb = new StringBuffer(str);
arr = new int[3];
String s1, s2;
for (int i = 0; i < 3; i++) {
Pattern p = Pattern.compile(regx[i]);
Matcher m = p.matcher(sb);
int temp = 0;
int index;
while (m.find()) {
index = sb.indexOf("=");
s1 = m.group(1);
s2 = m.group(2);
if (s1.equals("-"))
temp = -1 * Integer.parseInt(s2);
else {
if (s2.equals(""))
temp = 1;
else
temp = Integer.parseInt(s2);
}
if(m.start()<index)
arr[i] += temp;
else
arr[i] -= temp;
sb.replace(m.start(), m.end(), "");
m.reset();
}
}
int k = arr[1] * arr[1] - 4 * arr[0] * arr[2];
if (k < 0)
System.out.println("No Solution");
else {
double d1 = (-arr[1] + Math.sqrt(k)) / (2 * arr[0]);
double d2 = (-arr[1] - Math.sqrt(k)) / (2 * arr[0]);
System.out.printf("%.2f %.2f\n",d2,d1);
}
}
}
}
/**************************************************************
Problem: 1103
User: coder
Language: Java
Result: Accepted
Time:170 ms
Memory:16144 kb
****************************************************************/

1. #include <stdio.h>
int main(void)
{
int arr[] = {10,20,30,40,50,60};
int *p=arr;
printf("%d,%d,",*p++,*++p);
printf("%d,%d,%d",*p,*p++,*++p);
return 0;
}

为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下？

2. 有两个重复的话结果是正确的，但解法不够严谨，后面重复的覆盖掉前面的，由于题目数据限制也比较严，所以能提交通过。已更新算法