PAT——1010. 一元多项式求导

设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为n*xn-1。)

输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是0,但是表示为“0 0”。

输入样例:

3 4 -5 2 6 1 -2 0

输出样例:

12 3 -10 1 6 0

 1 package com.hone.basical;
 2 
 3 import java.util.Scanner;
 4 /**
 5  * 
 6  * @author Xia
 7  * 合理的利用标记flag,判断是否有输出
 8  */
 9 public class basicalLevel1010Derivative2 {
10     public static void main(String[] args) {
11         Scanner in = new Scanner(System.in);
12         boolean isHaveOutput = false;
13         while (in.hasNext()) {
14             int expon = in.nextInt();
15             int coef = in.nextInt();
16             
17             if (expon * coef != 0) {
18                 if (isHaveOutput) {
19                     System.out.print(" ");
20                 } else {
21                     isHaveOutput = true;
22                 }
23                 System.out.print(expon * coef + " " + (coef - 1));
24             }
25         }
26         in.close();
27         
28         if (!isHaveOutput) {
29             System.out.print("0 0");
30         }
31     }
32 }

 

posted @ 2017-12-04 15:44  SnailsCoffee  阅读(266)  评论(0编辑  收藏  举报