Acwing 1474. 多项式 A + B

题解:

#include <iostream>

using namespace std;

const int N=1010;

int main()
{
    double x[N];
    double y[N];
    double z[N];
    int k;
    cin>>k;
    while(k--)
    {
        int n;
        double v;
        cin>>n>>v;
        x[n]=v;
    }
    cin>>k;
    while(k--)
    {
        int n;
        double v;
        cin>>n>>v;
        y[n]=v;
    }
    //sim
    for(int i=0;i<N;i++) {z[i]=x[i]+y[i];}
    //count
    if(y[0]<0.000001&&y[0]>0)
    {
        y[0]=0;
    }
    if(z[0]<0.000001&&z[0]>0)
    {
        z[0]=0;
    }

    k=0;
    for(int i=0;i<N;i++)
    {
        if(z[i])
        {
            k++;
        }
    }
    cout<<k;
    for(int i=N-1;i>=0;i--)//N-1要记住
    {
        if(z[i])
        {
           printf(" %d %.1lf",i,z[i]);
        }
    }
    
    
}
 

//是有疑问的!!为何啊!

好的题解:

#include <iostream>
using namespace std;
const int N = 1010;
double n[N];
int main()
{
    for (int i = 0; i < 2; i ++)
    {
        int k;
        cin >> k;
        for (int j = 0; j < k; j ++)
        {
            int a;
            double b;
            cin >> a >> b;
            n[a] += b;
        }
    }
    int k = 0;
    for (int i = 0; i < N; i ++)
        if (n[i] != 0) k ++;
    cout << k << " ";
    for (int i = N - 1; i >= 0; i --)
        if (n[i] != 0) printf("%d %.1lf ", i, n[i]);
}

作者:P云
链接:https://www.acwing.com/solution/content/11082/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

以下是实现一元多项式计算器所需功能的核心代码: ```c #include <stdio.h> #include <stdlib.h> // 定义多项式节点结构 typedef struct PolyNode { int coef; // 系数 int exp; // 指数 struct PolyNode *next; } PolyNode; // 创建新节点 PolyNode* createNode(int coef, int exp) { PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode)); newNode->coef = coef; newNode->exp = exp; newNode->next = NULL; return newNode; } // 插入节点,按指数降序排列 void insertNode(PolyNode** head, int coef, int exp) { PolyNode* newNode = createNode(coef, exp); if (*head == NULL || exp > (*head)->exp) { newNode->next = *head; *head = newNode; } else { PolyNode* current = *head; while (current->next != NULL && current->next->exp > exp) { current = current->next; } if (current->next != NULL && current->next->exp == exp) { current->next->coef += coef; if (current->next->coef == 0) { PolyNode* temp = current->next; current->next = temp->next; free(temp); } free(newNode); } else { newNode->next = current->next; current->next = newNode; } } } // 输入并建立多项式 PolyNode* inputPoly() { PolyNode* head = NULL; int n, coef, exp; printf("请输入多项式数: "); scanf("%d", &n); for (int i = 0; i < n; i++) { printf("请输入第 %d 的系数指数: ", i + 1); scanf("%d %d", &coef, &exp); insertNode(&head, coef, exp); } return head; } // 按特定格式输出多项式 void outputPoly(PolyNode* head) { int n = 0; PolyNode* current = head; while (current != NULL) { n++; current = current->next; } printf("%d", n); current = head; while (current != NULL) { printf(",%d,%d", current->coef, current->exp); current = current->next; } printf("\n"); } // 多项式相加 PolyNode* addPoly(PolyNode* a, PolyNode* b) { PolyNode* result = NULL; while (a != NULL || b != NULL) { if (a == NULL) { insertNode(&result, b->coef, b->exp); b = b->next; } else if (b == NULL) { insertNode(&result, a->coef, a->exp); a = a->next; } else if (a->exp > b->exp) { insertNode(&result, a->coef, a->exp); a = a->next; } else if (a->exp < b->exp) { insertNode(&result, b->coef, b->exp); b = b->next; } else { insertNode(&result, a->coef + b->coef, a->exp); a = a->next; b = b->next; } } return result; } // 多项式相减 PolyNode* subPoly(PolyNode* a, PolyNode* b) { PolyNode* result = NULL; while (a != NULL || b != NULL) { if (a == NULL) { insertNode(&result, -b->coef, b->exp); b = b->next; } else if (b == NULL) { insertNode(&result, a->coef, a->exp); a = a->next; } else if (a->exp > b->exp) { insertNode(&result, a->coef, a->exp); a = a->next; } else if (a->exp < b->exp) { insertNode(&result, -b->coef, b->exp); b = b->next; } else { insertNode(&result, a->coef - b->coef, a->exp); a = a->next; b = b->next; } } return result; } ``` ### 主函数调用示例 ```c int main() { PolyNode* polyA = inputPoly(); PolyNode* polyB = inputPoly(); printf("多项式 A: "); outputPoly(polyA); printf("多项式 B: "); outputPoly(polyB); PolyNode* sum = addPoly(polyA, polyB); printf("多项式 A + B: "); outputPoly(sum); PolyNode* diff = subPoly(polyA, polyB); printf("多项式 A - B: "); outputPoly(diff); return 0; } ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值