1010 一元多项式求导 (25 分)
输入数据:
3 4 -5 2 6 1 -2 0
输出数据:
12 3 -10 1 6 0
此题坑点:
1、输入中可能会有多个空格,此时我们切割字符串的时候需要用到正则表达式去处理中间的多个空格
2、指数为零的情况下,不会有输出
3、零多项式要单独考虑输出,输出为:0 0
public class Main {
public int[] solution(int[] nums) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nums.length; i += 2) {
if (nums[i] * nums[i + 1] != 0) {
list.add(nums[i] * nums[i + 1]);
list.add(nums[i + 1] - 1);
}
}
if (list.isEmpty()) {
list.add(0);
list.add(0);
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
public static void main(String[] args) {
Main yiyuandaoshu = new Main();
Scanner in = new Scanner(System.in);
List<Integer> list = new ArrayList<>();
String s = in.nextLine();
//处理多个空格的情况
String[] ss = s.split("\\s+");
for (String s1 : ss) {
list.add(Integer.parseInt(s1));
}
int[] nums = list.stream().mapToInt(Integer::intValue).toArray();
int[] solution = yiyuandaoshu.solution(nums);
System.out.printf("%d", solution[0]);
for (int i = 1; i < solution.length; i++) {
System.out.printf(" %d", solution[i]);
}
}
}