[PAT B 1010, 1011] Unary Polynomial Derivation, A+B and C

 

Table of contents

1010 Unary Polynomial Derivation

1011 A+B and C

1010 Unary Polynomial Derivation

Design functions to find the derivative of a polynomial in one variable. (Note: The first derivative of xn (n is an integer) is nxn−1.)

Input format:

Enter the polynomial nonzero term coefficients and exponents (both integers up to 1000 in absolute value) in exponentially descending form. Numbers are separated by spaces.

Output format:

Output the coefficients and exponents of the nonzero terms of the derivative polynomial in the same format as the input. Numbers are separated by spaces, but there can be no extra spaces at the end. Note that both the exponent and the coefficient of the "null polynomial" are 0, but expressed as  0 0.

Input sample:

3 4 -5 2 6 1 -2 0

Sample output:

12 3 -10 1 6 0
Algorithm idea:

We define two variables to store the coefficient and exponent of the item respectively, and output the calculation result directly every time an item is read.

Error-prone points:
  • Terms whose coefficients are zero after derivation are not output.
  • There must be no spaces after the last number that is output.
  • If the polynomial has only constant terms, output 0 0. 
#include<bits/stdc++.h>
using namespace std;
using gg = long long;
int main()
{
	ios::sync_with_stdio;
	cin.tie(0);
	gg ci, ei;
	bool space = false;
	while (cin >> ci >> ei)
	{
		ci *= ei;
		--ei;
		if (ci != 0)
		{
			cout << (space ? " " : "") << ci << " " << ei;
			space = true;
		}
	}
	if (not space)
	{
		cout << "0  0";
	}
	return 0;
}

 

1011 A+B and C

Given 3 integers A, B and C in the interval [−231,231], please determine whether A+B is greater than C.

Input format:

Input line 1 gives a positive integer T (≤10), which is the number of test cases. Then T groups of test cases are given, each group occupies one line, and A, B, and C are given in sequence. Integers are separated by spaces.

Output format:

For each set of test cases, output in one line  Case #X: true if A+B>C, otherwise output  Case #X: false, where  X is the number of the test case (starting from 1).

Input sample:

4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647

Sample output:

Case #1: false
Case #2: true
Case #3: true
Case #4: false

 Algorithm idea: define 3 variables of long long type, and judge the output.

#include<bits/stdc++.h>
using namespace std;
using gg=long long;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    gg a,b,c,i,T;
    cin>>T;
    for(i=1;i<=T;i++)
    {
        cin>>a>>b>>c;
        cout<<"Case #"<<i<<": "<<(a+b>c?"true":"false")<<'\n';
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_73534885/article/details/131578719