老师的作业题目,昨晚上肝一晚上了,老是MLE,求大佬指点一下,是哪容易爆内存呀
  • 板块题目总版
  • 楼主李金隆
  • 当前回复22
  • 已保存回复22
  • 发布时间2020/5/14 08:52
  • 上次更新2023/11/7 02:30:21
查看原帖
老师的作业题目,昨晚上肝一晚上了,老是MLE,求大佬指点一下,是哪容易爆内存呀
343956
李金隆楼主2020/5/14 08:52
#include<stdio.h>
#include<stdlib.h>
#include<iostream>

//链表1
typedef struct Node {
	int num1;	//系数
	int num2;	//指数 
	Node* next;	//下一个结点 
}Node;
Node* head1 = NULL;
Node* head2 = NULL;
Node* head3 = NULL;
//释放内存 
void link_free(Node** p_head)
{
	Node* p_mov;
	while (*p_head != NULL)
	{
		p_mov = *p_head;
		*p_head = p_mov->next;
		free(p_mov);
	}

}

//主函数 
int main() {
	//输入系数和指数后,判断输入的字符是否是空格
	//直到不为空格,就开始读取第二个多项式  
	struct Node* temp = new Node[sizeof(Node)];	//临时结点 
	temp->next = NULL;
	head1 = temp;
	struct Node* temp3 = new Node[sizeof(Node)];//临时结点 
	temp3->next = NULL;
	head2 = temp3;
	char yichu;
	//读取第一个多项式 
	do {
		scanf("%d%d", &temp->num1, &temp->num2);	//读取系数和指数
		//新建一个结点来存储下一个 
		struct Node* temp2 = new Node[sizeof(Node)];
		temp2->next = NULL;
		//替换为下一个结点,然后继续读取 
		temp->next = temp2;
		temp = temp2;
		scanf("%c", &yichu);
	} while (yichu != '\n');	//读到回车为止 

	//读取第二个多项式 
	do {
		scanf("%d%d", &temp3->num1, &temp3->num2);	//读取系数和指数
		//新建一个结点来存储下一个 
		struct Node* temp4 = new Node[sizeof(Node)];
		temp4->next = NULL;
		//替换为下一个结点,然后继续读取 
		temp3->next = temp4;
		temp3 = temp4;
		scanf("%c", &yichu);
	} while (yichu != '\n');	//读到回车为止 

	temp = head1;
	/*	测试读取的数据
		while(temp->next!=NULL){
			printf("%d %d ",temp->num1,temp->num2);
			temp=temp->next;
		}printf("\n");
		temp=head2;
		while(temp->next!=NULL){
			printf("%d %d ",temp->num1,temp->num2);
			temp=temp->next;
		}printf("\n");*/
		//相乘得到第三个多项式
	bool flag;	//判断是否有同类项 
	struct Node* temp2 = new Node[sizeof(Node)];//临时结点
	struct Node* temp5 = new Node[sizeof(Node)];//临时结点 
	temp5->next = NULL;
	head3 = temp5;
	temp = head1;
	temp2 = head2;
	struct Node* temp6 = new Node[sizeof(Node)];	//临时结点 
	while (temp->next != NULL) {
		while (temp2->next != NULL) {
			flag = false;
			temp5->num1 = temp->num1 * temp2->num1;	//系数相乘得系数 
			temp5->num2 = temp->num2 + temp2->num2;	//指数相加得指数
			//判断当前结果指数是否已有,有就合并同类项

			temp6 = head3;
			while (temp6 != NULL) {
				if (temp6 != temp5 && temp6->num2 == temp5->num2) {	//不是第一个且指数相同 ,则不新建 
					temp6->num1 += temp5->num1;
					flag = true;	//存在同类项 
					break;
				}
				temp6 = temp6->next;
			}
			//不存在同类型才新建
			if (!flag) {
				struct Node* temp7 = new Node[sizeof(Node)];
				temp7->next = NULL;
				//替换为下一个结点,然后继续计算 
				temp5->next = temp7;
				temp5 = temp7;
			}
			temp2 = temp2->next;//下一项 
		}
		temp2 = head2;	//第二个多项式复原
		temp = temp->next;	//下一项 
	}
	//从高次到低次排序
	temp = head3;
	int num1, num2, num3, num4;
	//如果有后面比前面指数大的就交换 
	while (temp->next != NULL) {
		temp2 = temp->next;
		while (temp2 != NULL) {
			if (temp2->num2 > temp->num2) {
				num1 = temp->num1;
				num2 = temp->num2;
				temp->num1 = temp2->num1;
				temp->num2 = temp2->num2;
				temp2->num1 = num1;
				temp2->num2 = num2;
			}
			temp2 = temp2->next;
		}
		temp = temp->next;
	}
	//输出结果
	temp = head3;
	while (temp->next != NULL) {
		printf("%d %d ", temp->num1, temp->num2);
		temp = temp->next;

	}
	//释放内存 
	return 0;
}

之前有用getchar来进行空格和回车的分辨,然后老是爆内存,然后改成scanf("%c",&myc)也是爆内存,实在是不知道为什么了(要@管理员么

2020/5/14 08:52
加载中...