1002 A+B for Polynom

1002 A+B for Polynomials

这题不成功,VS上可以成功输出但PAT平台输出不正确

Input Specification:

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 Specification:

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.

#include<stdio.h>
#include<stdlib.h>

int main()
{
  int num_of_nonzero1=0, num_of_nonzero2=0;
  
  //输入多项式1
  scanf_s("%d", &num_of_nonzero1);
  int* aexponents_1 = NULL;
  float* coefficients_1 = NULL;
  aexponents_1 = (int*)calloc(num_of_nonzero1, sizeof(int) );
  coefficients_1 = (float*)calloc(num_of_nonzero1, sizeof(float));
  for (int i = 0; i < num_of_nonzero1; i++)
  {
    scanf_s("%d", &aexponents_1[i]);
    scanf_s("%f", &coefficients_1[i]);
  }

  //输入多项式2
  scanf_s("%d", &num_of_nonzero2);
  int* aexponents_2 = NULL;
  float* coefficients_2 = NULL;
  aexponents_2 = (int*)calloc(num_of_nonzero2, sizeof(int));
  coefficients_2 = (float*)calloc(num_of_nonzero2, sizeof(float));
  for (int i = 0; i < num_of_nonzero2; i++)
  {
    scanf_s("%d", &aexponents_2[i]);
    scanf_s("%f", &coefficients_2[i]);
  }

  int num_of_nonzero=0;
  for (int i = 0; i < num_of_nonzero1; i++)
  {
    int flag = 0;
    for (int j = 0; j < num_of_nonzero2; j++)
    {
      if (aexponents_1[i] == aexponents_2[j])
      {
        flag = 1;
      }
    }
    if (flag == 0)
    {
      num_of_nonzero++;
    }
  }
  num_of_nonzero = num_of_nonzero + num_of_nonzero2;

  int* aexponents_result = NULL;
  float* coefficients_result = NULL;
  aexponents_result = (int*)calloc(num_of_nonzero, sizeof(int));
  coefficients_result = (float*)calloc(num_of_nonzero, sizeof(float));
  int count = 0;
  int i = 0;
  int j = 0;
  while (i<num_of_nonzero1 && j<num_of_nonzero2)
  {
    if (aexponents_1[i] == aexponents_2[j])
    {
      aexponents_result[count] = aexponents_1[i];
      coefficients_result[count] = coefficients_1[i] + coefficients_2[j];
      count++;
      i++;
      j++;
    }
    if (aexponents_1[i] > aexponents_2[j])
    {
      aexponents_result[count] = aexponents_1[i];
      coefficients_result[count] = coefficients_1[i];
      count++;
      i++;
    }
    if (aexponents_1[i] < aexponents_2[j])
    {
      aexponents_result[count] = aexponents_2[j];
      coefficients_result[count] = coefficients_2[j];
      count++;
      j++;
    }    
  }
  if (i < num_of_nonzero1-1)
  {
    while (i < num_of_nonzero1)
    {
      aexponents_result[count] = aexponents_1[i];
      coefficients_result[count] = coefficients_1[i];
      count++;
      i++;
    }
  }
  if (j < num_of_nonzero2 - 1)
  {
    while (j < num_of_nonzero2)
    {
      aexponents_result[count] = aexponents_2[i];
      coefficients_result[count] = coefficients_2[i];
      count++;
      j++;
    }
  }

  printf("%d ", num_of_nonzero);
  for (int k = 0; k < num_of_nonzero; k++)
  {
    printf("%d ", aexponents_result[k]);
    printf("%.1f ", coefficients_result[k]);
  }
  printf("\n");
}

这做法太尼玛蠢了,看了 @王森ouc 同学的做法才发现自己有多蠢

#include <bits/stdc++.h>
using namespace std;
int main()
{
    float poly[1005] = {};
    for (int i = 0; i < 2; i++) {
        int k, exp;
        float coe;
        cin >> k;
        while (k--) {
            cin >> exp >> coe;
            poly[exp] += coe;
        }
    }
    int size = 0;   //统计不为0的个数
    for(int i = 0;i<1005;i++)
        if (poly[i] != 0) 
            size++;
    cout << size;
    for(int i = 1001;i >= 0;i--)
        if (poly[i] != 0) {
            printf(" %d %.1f", i, poly[i]);
        }
}

以上来自@王森ouc同学

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值