PAT 1002 A+B for Polynomials
问题:A、B两个多项式求和,结果按要求输出,考虑到小数点后一位。原问题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000
解决方案:
这是一个可以用链表(List)作为数据结构,使用归并排序解决的问题。首先定义链表中的节点(Node),Node中成员变量有exponent和coefficient,以及指向下一个节点的next指针;然后利用定义好的Node构造List,List中的成员变量有Node的数目,以及head,tail指针,除了成员变量之外,还需要定义成员函数,List的成员函数有insert,search,delete,printf等等,这一题只需定义insert和prinft即可;最后根据输入生成两个链表,使用归并排序即可,注意要考虑到两个元素相加和为0的情况。
C++代码:
1 #include<iostream> 2 #include<iomanip> //题目中要求考到的小数点后一位,故调用这个头文件 3 4 using namespace std; 5 6 class Node{ //定义Node,注意成员变量尽量用private,成员函数用public 7 private: 8 int exponent; 9 float coefficient; 10 Node* next; 11 12 public: 13 Node(int exponent = 0, float coefficient = 0.0, Node* next = NULL) { 14 this->exponent = exponent; 15 this->coefficient = coefficient; 16 this->next = next; 17 } 18 int getExpon() { 19 return this->exponent; 20 } 21 float getCoeff() { 22 return this->coefficient; 23 } 24 void setExpon(int exponent) { 25 this->exponent = exponent; 26 } 27 void setCoeff(float coefficient) { 28 this->coefficient = coefficient; 29 } 30 Node* getNext() { 31 return this->next; 32 } 33 void setNext(Node* A) { 34 this->next = A; 35 } 36 ~Node() { 37 delete this->next; 38 } 39 }; 40 41 class List { 42 private: 43 int number; 44 Node* head, * tail; 45 46 public: 47 List() { 48 this->number = 0; 49 this->head = this->tail = new Node(-1, 0); //这里有个链表的trick,前面加一个没有数据的head,便于后续insert等操作 50 } 51 int getNumber() { 52 return this->number; 53 } 54 void addNumber() { 55 this->number++; 56 } 57 void minusNumber() { 58 this->number--; 59 } 60 Node* getHead() { 61 return this->head; 62 } 63 Node* getTail() { 64 return this->tail; 65 } 66 void setHead(Node* A) { 67 this->head = A; 68 } 69 void setTail(Node* A) { 70 this->tail = A; 71 } 72 void insert(int exponent, float coefficient); //声明insert函数 73 void printf(); //声明printf函数 74 ~List() { 75 delete this->head; 76 delete this->tail; 77 } 78 }; 79 80 void List::insert(int exponent, float coefficient) { 81 Node* A = new Node(exponent, coefficient); 82 Node* B = this->head; 83 for (; B->getNext() != NULL && A->getExpon() < B->getNext()->getExpon(); B = B->getNext()); 84 if (B->getNext() == NULL) { //直接插在最后 85 this->getTail()->setNext(A); 86 this->setTail(A); 87 this->addNumber(); 88 } 89 else if(A->getExpon() == B->getNext()->getExpon()){ //这里有两种情况,一种是和不为0,一种是和为0 90 if (B->getNext()->getCoeff() + A->getCoeff() != 0) //和不为0 91 B->getNext()->setCoeff(B->getNext()->getCoeff() + A->getCoeff()); 92 else { //和为0,消掉链表中的一个Node,注意这个Node正好是在最后的情况,注意出理tail指针 93 Node* C = B->getNext(); 94 B->setNext(B->getNext()->getNext()); 95 if (B->getNext() == NULL) //处理tail指针 96 this->setTail(B); 97 this->minusNumber(); 98 delete(A); 99 delete(C); 100 } 101 } 102 else if(A->getExpon() > B->getNext()->getExpon()) { //插在中间,这就体现了链表前面有一个空head的好处了 103 A->setNext(B->getNext()); 104 B->setNext(A); 105 this->addNumber(); 106 } 107 } 108 109 void List::printf() { 110 cout << this->getNumber(); 111 for (Node* A = this->getHead()->getNext(); A != NULL; A = A->getNext()) { 112 cout << " " << A->getExpon(); 113 cout << fixed << setprecision(1) << " " << A->getCoeff(); //考虑到小数点后一位,使用"iomanip"头文件 114 } 115 116 } 117 118 int main() { 119 int number, exponent; 120 float coefficient; 121 List* A, * B, * C; 122 Node* NA, * NB; 123 A = new List(); 124 B = new List(); 125 C = new List(); 126 cin >> number; 127 for (int i = 0; i < number; i++) { 128 cin >> exponent >> coefficient; 129 A->insert(exponent, coefficient); 130 } 131 cin >> number; 132 for (int i = 0; i < number; i++) { 133 cin >> exponent >> coefficient; 134 B->insert(exponent, coefficient); 135 } 136 NA = A->getHead()->getNext(); 137 NB = B->getHead()->getNext(); 138 while (NA != NULL && NB != NULL) { //归并排序,注意和为0的情况 139 if (NA->getExpon() < NB->getExpon()) { 140 C->insert(NB->getExpon(), NB->getCoeff()); 141 NB = NB->getNext(); 142 } 143 else if (NA->getExpon() == NB->getExpon()) { 144 if (NA->getCoeff() + NB->getCoeff() != 0) //和不为0 145 C->insert(NA->getExpon(), NA->getCoeff() + NB->getCoeff()); 146 NA = NA->getNext(); 147 NB = NB->getNext(); 148 } 149 else { 150 C->insert(NA->getExpon(), NA->getCoeff()); 151 NA = NA->getNext(); 152 } 153 } 154 for (; NA != NULL; NA = NA->getNext()) C->insert(NA->getExpon(), NA->getCoeff()); 155 for (; NB != NULL; NB = NB->getNext()) C->insert(NB->getExpon(), NB->getCoeff()); 156 C->printf(); 157 return 0; 158 }

浙公网安备 33010602011771号