close

#include <stdio.h>

#define MAX 1000000

int coe[MAX];

int main()
{
    int x, i, var, count, total, deriNum; /* coefficients, variable, derivative number*/

    while(scanf("%d", &x) == 1)
    {
        total = 0, var = 1, deriNum = 1;
        for(count = 0;  scanf("%d", coe+(count++) ) && (getchar() != '\n');  )
            ;
        /* i from count-2 because the value of last element is zero.  */
        for(i = count-2;  i >= 0;  i--, var *= x, deriNum++)
            total += deriNum * coe[i] * var;

        printf("%d\n", total);
    }
    return 0;
}
 

----2017-06-24----

import java.util.*;
public class main{
    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);

            // A case
            while (sc.hasNext()) {
                int power = 0, x, sum = 0;
                ArrayList coefAl = new ArrayList();

                x = sc.nextInt();
                sc.nextLine();

                // Input
                String str = sc.nextLine();
                String[] strArr = str.split(" ");

                for (String theStr : strArr) {
                    coefAl.add(0, Integer.parseInt(theStr));
                }

                // Operation
                for (int i = 1; i < coefAl.size(); i++) { // i == 0 stands for the constant and the constant will disappear after derivation.
                    sum += ((int) coefAl.get(i)) * (power + 1) * Math.pow(x, power); // a * n * x^(n - 1)
                    power++;
                }
                System.out.println("" + sum);
            }
        } catch (Exception e) {
        }
    }
};
 

arrow
arrow
    全站熱搜

    awesq123 發表在 痞客邦 留言(0) 人氣()