C language test practice questions

Input format:

The input is divided into 2 lines, and each line first gives the number of non-zero terms of the polynomial, and then enters a polynomial non-zero term coefficient and exponent in an exponentially descending manner (the absolute value is an integer not exceeding 1000). Numbers are separated by spaces.

Output format:

The output is divided into 2 lines, and the coefficients and exponents of the product polynomial and the non-zero term of the sum polynomial are output in an exponentially descending manner, respectively. Numbers are separated by spaces, but there can be no extra spaces at the end. A zero polynomial should output 0 0.

Input sample:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

Sample output:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
代码长度限制16 KB
时间限制200 ms
内存限制64 MB

Answer:

#include<stdio.h>

#define N 10000

int main() {
    
    
    int a[N]= {
    
    0};
    int b[N]= {
    
    0};
    int c[N]= {
    
    0};
    int d[N]= {
    
    0};
    int i,m,f;
    scanf("%d",&i);
    while(i--) {
    
    
        scanf("%d %d",&m,&f);
        a[f]+=m;
    }
    scanf("%d",&i);
    while(i--) {
    
    
        scanf("%d %d",&m,&f);
        b[f]+=m;
    }
    for(int i=N-1; i>=0; i--) {
    
    
        if(a[i]) {
    
    
            for(int j=0; j<N; j++) {
    
    
                if(b[j]) {
    
    
                    c[i+j]+=a[i]*b[j];
                }
            }
        }
    }
    int cnt=0;
    for(int i=N-1; i>=0; i--) {
    
    

        if(c[i]) {
    
    
            if(cnt)printf(" ");
            printf("%d %d",c[i],i);
            cnt++;
        }

    }
    if(!cnt)printf("0 0");

    for(int i=N-1; i>=0; i--)
        if(a[i])
            d[i]+=a[i];
    for(int j=0; j<N; j++)
        if(b[j])
            d[j]+=b[j];

printf("\n");

     cnt=0;
    for(int i=N-1; i>=0; i--) {
    
    

        if(d[i]) {
    
    
            if(cnt)printf(" ");
            printf("%d %d",d[i],i);
            cnt++;
        }

    }
    if(!cnt)printf("0 0");
    return 0;
}

test:

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41438423/article/details/125413011