PAT Class B Java Implementation_1010. Unary Polynomial Derivation_With Detailed Problem Solving Notes_10

1010. Unary Polynomial Derivation(25)

time limit
400 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard

Design a function to take the derivative of a polynomial in one variable. (Note: the first derivative of x n (n is an integer) is n*x n-1 .)

Input format: Input the non-zero coefficients and exponents of the polynomial in exponential descending mode (the absolute values ​​are integers not exceeding 1000). The numbers are separated by spaces.

Output Format: Output the coefficients and exponents of the non-zero terms of the derivative polynomial in the same format as the input. Numbers are separated by spaces, but no extra spaces at the end. Note that the "zero polynomial" has both exponents and coefficients 0, but is represented as "0 0".

Input sample:
3 4 -5 2 6 1 -2 0
Sample output:
12 3 -10 1 6 0

----------------------------------------------------------------------------------------------------

/*Idea: Use the trim() function to remove the leading and trailing spaces from the input string,
 * Then use split(" ") to divide the input string into character substrings with spaces as the boundary, and save them to the string array.
 * The question is not difficult, but there is a pit. That is, there is more than one space in the middle of the input string, you have to use split("\\s+")
 * "\\s+" is a regular expression form, \\ is escaped into \, \ and s form \s, which means a space, and + means at least one space.
 * */
import java.util.Scanner;
public class PAT_B_1010
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);//Receive input
		String[] num = in.nextLine().trim().split("\\s+");
		//Remove the leading and trailing spaces from the input number and save it to a string array with spaces as the boundary
		String out = "";//The last string to output
		for(int i = 0; i < num.length; i+=2 )
		{
			int xiShu = Integer.parseInt(num[i]);//The coefficient of the input item
			int zhiShu = Integer.parseInt(num[i+1]);//The index of the input item
			int xiShuD = xiShu * zhiShu;//The coefficient of the derivative term
			int zhiShuD = zhiShu - 1;//The index of the derivative term
			
			if(i == 0 && zhiShu == 0)//The exponent and coefficient of "zero polynomial" are both 0, but they are represented as "0 0".
			{
				out = "0 0";
				break;
			}
			
			if(xiShuD == 0)//If the derivative item is 0, no output
				continue;
			
			out = out + xiShuD + " " + zhiShuD + " ";//Save the string to be output
		}
		System.out.print(out.trim());//Remove the space at the end of the string and output.
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326424402&siteId=291194637