【发布时间】:2021-09-18 18:01:04
【问题描述】:
我试图通过链表添加 3 个多项式并设法在 C++ 中创建以下程序,该程序适用于某些情况,但不适用于所有情况。 问题是,我无法弄清楚程序为哪些测试用例提供了错误的输出(因为所有用例在hackerrank 上都不可见)。 任何有关错误来源的更正或帮助将不胜感激。
我基本上将 3 个链表作为输入,并添加了它们对应于从 0 到 100 的每个幂的系数(幂范围的限制为 0 到 100)并将它们放入一个新的链表中(带有头节点 head_node_final)和然后输出。
我得到正确答案的标准案例:
输入:
2 //2 terms in 1st linked list
1 1 // 1*x
2 10 // 10x^2
2 //2 terms in 2nd linked list
1 0 //1
10 2 // 10x^2
2 //2 terms in 3rd linked list
10 2 //10x^2
1 3 // 1x^3
输出:
(1,3)+(30,2)+(1,1)+(1,0) // x^3+30x^2+x+1
代码:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class node
{
public:
int power;
int coefficient;
node *next;
};
int main()
{
int n1;
cin >> n1;
node *head_node1 = new node();
int inp_coefficient1;
cin >> inp_coefficient1;
head_node1->coefficient = inp_coefficient1;
int inp_power1;
cin >> inp_power1;
head_node1->power = inp_power1;
node *value = head_node1;
for (int i1 = 1; i1 <= n1; i1++)
{
if (i1 != n1)
{
cin >> inp_coefficient1;
cin >> inp_power1;
node *value_new = new node();
value_new->coefficient = inp_coefficient1;
value_new->power = inp_power1;
value->next = value_new;
value = value->next;
}
else
{
value->next = NULL;
}
}
int n2;
cin >> n2;
node *head_node2 = new node();
int inp_coefficient2;
cin >> inp_coefficient2;
head_node2->coefficient = inp_coefficient2;
int inp_power2;
cin >> inp_power2;
head_node2->power = inp_power2;
value = head_node2;
for (int i2 = 1; i2 <= n2; i2++)
{
if (i2 != n2)
{
cin >> inp_coefficient2;
cin >> inp_power2;
node *value_new = new node();
value_new->coefficient = inp_coefficient2;
value_new->power = inp_power2;
value->next = value_new;
value = value->next;
}
else
{
value->next = NULL;
}
}
int n3;
cin >> n3;
node *head_node3 = new node();
int inp_coefficient3;
cin >> inp_coefficient3;
head_node3->coefficient = inp_coefficient3;
int inp_power3;
cin >> inp_power3;
head_node3->power = inp_power3;
value = head_node3;
for (int i3 = 1; i3 <= n3; i3++)
{
if (i3 != n3)
{
cin >> inp_coefficient3;
cin >> inp_power3;
node *value_new = new node();
value_new->coefficient = inp_coefficient3;
value_new->power = inp_power3;
value->next = value_new;
value = value->next;
}
else
{
value->next = NULL;
}
}
node *head_node_final = new node();
node *value_final = head_node_final;
for (int power_counter = 100; power_counter >= 0; power_counter--)
{
node *node_value1 = head_node1;
while (node_value1 != NULL && node_value1->power != power_counter)
{
node_value1 = node_value1->next;
}
int node_1_return;
if (node_value1 == NULL)
{
node_1_return = 0;
}
if (node_value1 != NULL)
{
node_1_return = node_value1->coefficient;
}
node *node_value2 = head_node2;
while (node_value2 != NULL && node_value2->power != power_counter)
{
node_value2 = node_value2->next;
}
int node_2_return;
if (node_value2 == NULL)
{
node_2_return = 0;
}
if (node_value2 != NULL)
{
node_2_return = node_value2->coefficient;
}
node *node_value3 = head_node3;
while (node_value3 != NULL && node_value3->power != power_counter)
{
node_value3 = node_value3->next;
}
int node_3_return;
if (node_value3 == NULL)
{
node_3_return = 0;
}
if (node_value3 != NULL)
{
node_3_return = node_value3->coefficient;
}
if ((node_1_return + node_2_return + node_3_return) != 0)
{
value_final->power = power_counter;
value_final->coefficient = (node_1_return + node_2_return + node_3_return);
value_final->next = new node();
value_final = value_final->next;
}
if (power_counter == 0)
{
value_final = NULL;
}
}
node *new_value_final = head_node_final;
while (new_value_final->next != NULL)
{
cout << "(" << new_value_final->coefficient << "," << new_value_final->power << ")";
if (new_value_final->next->next != NULL)
{
cout << "+";
}
new_value_final = new_value_final->next;
}
return 0;
}
【问题讨论】:
-
为什么要自己写链表来添加多项式?
vector的系数不是更容易且更不容易出错吗? -
如果你认为问题需要一个链表,你可以使用
std::list,但对于大多数东西来说std::vector更胜一筹 -
您的代码可以从使用函数中受益匪浅。您可以调用相同的函数 3 次,而不是编写 3 次相同的代码来创建 3 个列表。更少的重复 = 更少的代码 = 更少的错误
-
我无法重现您的示例案例:godbolt.org/z/1zTh8oYfK
-
令人惊讶的是,您的程序有效。只是您的输入是错误的。您在第 3 输入行中交换了 2 和 10。但是,不幸的是,您的程序与体面的设计无关。 . .
标签: c++ object linked-list polynomials