Problem F: 498'
Problem F: 498' |
Problem
Looking throw the Online Judge's Problem Set Archive I found a very interesting problem number 498, titled ``Polly the Polynomial''.Frankly speaking, I did not solve it, but I derived from it this problem.Everything in this problem is a derivative of something from 498. In particular, 498 was "... designed to help you remember ... basic algebra skills, make the world a better place, etc., etc.". This problem is designed to help you remember basic derivation algebra skills, increase the speed in which world becomes a better place, etc., etc.
In 498 you had to evaluate the values of polynomial
Input
Your program should accept an even number of lines of text. Each pair of lines will represent one problem. The first line will contain one integer - a value for x. The second line will contain a list of integers a0, a1, ..., an-1, an, which represent a set of polynomial coefficients.Input is terminated by <EOF>.
Output
For each pair of lines, your program should evaluate the derivative of polynomial for the given value x and output it in a single line.Sample Input
7 1 -1 2 1 1 1
Sample Output
1 5
大意:給多項式公式 求微分的結果
解法:
horner's rule 開到long
EX: x^3 + x^2 + x + 1
f(x)= ((x+1)x)x+1
f'(x)= (3x+2)x+1
import java.util.Scanner;
public class UVA10268 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
long X = Long.parseLong(sc.nextLine());
long sum = 0;
long last = 0;
String s[] = sc.nextLine().split(" ");
long number[] = new long[s.length];
boolean number_box[] = new boolean[s.length];
for (int i = 0; i < s.length; i++) {
boolean check = false;
for (int j = 0; j < s[i].length(); j++) {
if (s[i].charAt(j) == '-' && '9' - s[i].charAt(j + 1) >= 0
&& s[i].charAt(j + 1) - '0' <= 9) {
check = true;
}
else if ('9' - s[i].charAt(j) >= 0
&& s[i].charAt(j) - '0' <= 9
&& s[i].charAt(j) != '-') {
check = true;
} else {
check = false;
break;
}
}
if (check) {
number_box[i] = true;
}
else
continue;
}
int k = 0;
for (int i = 0; i < number_box.length; i++) {
if (number_box[i] == true) {
last++;
number[k] = Long.parseLong(s[i]);
k++;
}
}
long j = last;
int i;
for (i = 0; i < j - 2; i++) {
last--;
sum = (sum + (last * number[i])) * X;
}
if (last < 2)
sum = 0;
else
sum += number[i];
System.out.println(sum);
}
sc.close();
}
}
留言
張貼留言