1010 一元多项式求导 (25分)
原题链接:传送门
一、题目
二、解析
思路
题目读懂:输入案例中3 4 = 对应的输出为12 3。12是3*4的结果,3是4-1。
注意空格,注意“零多项式”的指数和系数都是 0,但是表示为 0 0。
ac代码
你可以用字符串保存,最后再把首位空格去掉。
import java.util.Scanner;
/**
* 1010 一元多项式求导 (25分)
*
* @author ChangSheng
* @date 2020-03-03
*/
public class P1010_一元多项式求导 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ans = "";
while (sc.hasNext()) {
int a = sc.nextInt(), b = sc.nextInt();
if (b != 0) ans += (a*b)+" "+(b-1) + " ";
}
if ("".equals(ans)) ans += "0 0";
System.out.println(ans.trim());
}
}
你也可以边输入边打印。思路同上一样
import java.util.Scanner;
/**
* 1010 一元多项式求导 (25分)
*
* @author ChangSheng
* @date 2020-03-03
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag = false;
while (sc.hasNext()) {
int a = sc.nextInt(), b = sc.nextInt();
if (b != 0) {
if (!flag) flag = true;
else System.out.print(" ");
System.out.print((a*b)+" "+(b-1));
}
}
if (!flag) System.out.print("0 0");
}