PAT Basic 1010. Derivation of univariate polynomials (25)

Design function to find the derivative of one variable polynomial.

Input format: input polynomial non-zero coefficient and index (absolute value is not more than 1000 integer) in exponential descending mode. Numbers are separated by spaces.

Output format: output the coefficient and index of non-zero term of derivative polynomial in the same format as the input. Numbers are separated by spaces, but no extra spaces are allowed at the end.

Input example:
3 4 -5 2 6 1 -2 0
Output example:
12 3 -10 1 6 0

Other people's code

#include <stdio.h>  
#include <string.h>  
int main()  
{  
    int n, e, flag = 0;  
    while (scanf("%d%d", &n, &e) != EOF)  
    {  
        if( n*e )  
        {  
            if(flag)  
                printf(" ");  
            else  
                flag = 1;  
            printf("%d %d", n*e, e-1);  
        }  
    }  
    if(!flag) printf("0 0");  

    return 0;  
}  

Because I read the book first, I don't want to write a long, smelly and aesthetic input directly according to the parity of the input. I don't know if ac is not, I guess I can't get full marks... In the black box, you have to enter, crtl+z, and enter. I don't know that you don't need crtl+z if you read two of the above codes at a time? hey

#include<cstdio>
using namespace std;
int main(){
    int a[2018] = { 0 };
    int k = 1; int b, flag = 0;;
    while (scanf("%d", &b) != EOF){ 
        a[k++] = b;
    }
    for (int i = 1; i <= k; i++){
        if (i % 2 == 1){   //coefficient
            if (a[i]  != 0 && a[i + 1] != 0){
                a[i] = a[i] * a[i + 1];
            }
            else if (a[i]  == 0){
                a[i] = 0; a[i + 1] = 0;
            }

        }
        else if (i % 2 == 0){ //index
            if (a[i] != 0){
                a[i] = a[i] - 1;
            }
            else if (a[i] == 0){
                a[i - 1] = 0;
            }

        }
    }

    for (int i = 1; i <= k; i++){
        if (a[i] == 0 && i % 2 == 1){ i++; continue; } //Coefficient is zero, skip this coefficient and corresponding index
        else { printf("%d", a[i]); flag = 1; }
            if (i != k - 1){
                printf(" ");
                }
    }
    if (!flag) printf("0 0");
        return 0;

}

Added by Monshery on Thu, 30 Apr 2020 15:03:48 +0300