PAT: A + B for Polynomials

英文试题:

1002. A+B for Polynomials (25)

This time, you are supposed to find A+B where A and B are two polynomials.

Input

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.

Output

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place. 

Sample Input

2 1 2.4 0 3.2

2 2 1.5 1 0.5

Sample Output

3 2 1.5 1 2.9 0 3.2

中文翻译:

1002. A+B的多项式操作

这次,你应该发现在A+B中,A和B是2个多项式

输入:

每个输入文件包含一个测试用例。每个测试用例占据2行,每一行包含一个多项式的信息:

K N1 aN1 N2 aN2 ... NK aNK

K:多项式的非0项个数,并且要求1 <= K <= 10

Ni:单项式的指数,并且要求0 <= NK < ... < N2 < N1 <=1000

aNi:单项式的系数

输出:

对于每一个测试用例,你应该在一行输出输出多项式A和B的和,输出格式同输入格式。

注意,每一行的末尾不要有多余的空格。请准确到小数点后一位。

 

样例输入:

2 1 2.4 0 3.2

2 2 1.5 1 0.5

样例输出:

3 2 1.5 1 2.9 0 3.2


数组实现:选取的数据结构是 数组

思路

声明一个大小为1001的double型的数组array,并且初始化为各元素都是0

数组的下标代表输入的指数

数组元素代表输入的系数

定义一个Int型变量k,初始时是0,如果输入的指数为a,系数为b并且array[a]=0,就把k自增1

k最终就表示这个多项式的非0项的个数

但不管怎样,都要把每次输入的系数b以累加的方式填入到array[a]

待填充好数组array,以逆序的方式输出非0项的下标和非0项


代码

#include <stdio.h>

int main()
{
    double array[1001] = {0};

//    printf("%1.1f\n",array[9]);
    int num = 0;
    int k = 0;
    int tmp_exponent;
    double tmp_coefficient;

    // input
    scanf("%d",&num);
    for(int i=0;i<num;i++){
        scanf("%d",&tmp_exponent);
        scanf("%lf",&tmp_coefficient);

        if(array[tmp_exponent]==0){
            k++;
        }
        array[tmp_exponent] = array[tmp_exponent] + tmp_coefficient;
    }

    scanf("%d",&num);
    for(int i=0;i<num;i++){
        scanf("%d",&tmp_exponent);
        scanf("%lf",&tmp_coefficient);

        if(array[tmp_exponent]==0){
            k++;// number of nonzero terms in the polynomial
        }
        array[tmp_exponent] = array[tmp_exponent] + tmp_coefficient;
    }

    //
    printf("%d",k);
    for(int i=1000;i>=0;i--){//reverse output
        if(array[i]!=0){
            printf(" %d %1.1f",i,array[i]);//be accurate to 1 decimal place
        }
    }
    printf("\n");
}


链表实现:选取的数据结构是 单链表,比较麻烦,因为比数组的思路增加了链表排序的实现

代码

#include <stdio.h>
#include <malloc.h>

void test()
{
    int num;
    scanf("%d",&num);

    int a[num];
    for(int i=0;i<num;i++){
        int tmp;
        scanf("%d",&tmp);
        a[i] = tmp;
    }

    for(int i=0;i<num;i++){
        printf("%d\n",a[i]);
    }
}

struct node_ex
{
    int value;
    double coefficient_sum;
    struct node_ex *next;
};

struct node_ex *init_node_ex(struct node_ex *node, int value, double coefficient)
{
//    printf("[init_node_ex] value = %d, coefficient = %lf\n",value,coefficient);
    struct node_ex *r = node;
    if(r){
        r->value = value;
        r->coefficient_sum = coefficient;
        r->next = NULL;
        return r;
    }else{
        printf("arg node_ex is NULL\n");
        return NULL;
    }
};

void print_current_status(struct node_ex *head_ex)
{
    struct node_ex *pre,*tmp = head_ex;
    while(tmp){
        printf(" %d %lf",tmp->value,tmp->coefficient_sum);
        pre = tmp;
        tmp = tmp->next;
    }
    printf("\n");
}

struct node_ex *function(int num,struct node_ex *head_ex,int *exponent_array, double *coefficient_array)
{
    for(int i=0; i<num; i++ ){
        if(head_ex == NULL){ // head_ex is null

            head_ex = (struct node_ex *)malloc(sizeof(struct node_ex));
            head_ex = init_node_ex(head_ex,exponent_array[i],coefficient_array[i]);

        }else{ // head_ex is not null
            if(exponent_array[i] > head_ex->value){
                struct node_ex *new_ex = (struct node_ex *)malloc(sizeof(struct node_ex));
                new_ex = init_node_ex(new_ex,exponent_array[i],coefficient_array[i]);

                new_ex->next = head_ex;
                head_ex = new_ex;
            }else if(exponent_array[i] < head_ex->value){
//                printf("%d < %d\n",exponent_array[i],head_ex->value);
                struct node_ex *tmp = head_ex;
                struct node_ex *pre = head_ex;

                while(tmp && exponent_array[i] < tmp->value){
                    pre = tmp;
                    tmp = tmp->next;
                }

                if(tmp == NULL){
                    struct node_ex *new_ex = (struct node_ex *)malloc(sizeof(struct node_ex));
                    new_ex = init_node_ex(new_ex,exponent_array[i],coefficient_array[i]);

                    pre->next = new_ex;

                }else if(exponent_array[i] > tmp->value){
                    struct node_ex *new_ex = (struct node_ex *)malloc(sizeof(struct node_ex));
                    new_ex = init_node_ex(new_ex,exponent_array[i],coefficient_array[i]);

                    new_ex->next = tmp;
                    pre->next = new_ex;
                }else{
//                    printf("================ L ===============\n");
                    tmp->coefficient_sum = tmp->coefficient_sum + coefficient_array[i];
                }
            }else{
                head_ex->coefficient_sum = head_ex->coefficient_sum + coefficient_array[i];
            }
        }// head_ex id not null
//        print_current_status(head_ex);
    }// for

    return head_ex;
};

int main()
{
    int num_first;
    scanf("%d",&num_first);

    int exponent_first[num_first];
    double coefficient_first[num_first];
    for(int i=0;i<num_first;i++){
        int tmp_exponent;
        double tmp_coefficient;
        scanf("%d",&tmp_exponent);
        scanf("%lf",&tmp_coefficient);

        exponent_first[i] = tmp_exponent;
        coefficient_first[i] = tmp_coefficient;
    }

    int num_second;
    scanf("%d",&num_second);

    int exponent_second[num_second];
    double coefficient_second[num_second];
    for(int i=0;i<num_second;i++){
        int tmp_exponent;
        double tmp_coefficient;
        scanf("%d",&tmp_exponent);
        scanf("%lf",&tmp_coefficient);

        exponent_second[i] = tmp_exponent;
        coefficient_second[i] = tmp_coefficient;
    }

    struct node_ex *head_ex = NULL;

    head_ex = function(num_first,head_ex,exponent_first,coefficient_first);
    head_ex = function(num_second,head_ex,exponent_second,coefficient_second);

    struct node_ex *tmp = head_ex;

    int i=0;
    while(tmp && i<10){
        tmp = tmp->next;
        i++;
    }

    printf("%d",i);
    struct node_ex *pre = head_ex;
    tmp = head_ex;
    while(tmp){
        printf(" %d %1.1f",tmp->value,tmp->coefficient_sum);
        pre = tmp;
        tmp = tmp->next;
        free(pre);
        pre = NULL;
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值