第二次编程作业

02-线性结构1 一元多项式的乘法与加法运算   (20分)


设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0


//有bug 未完成版本 
#include <stdio.h>
#include <stdlib.h>

typedef struct PolyNode *Polynomial;
struct PolyNode{
	int coef;
	int expon;
	Polynomial link;
};

void Attach(int c, int e, Polynomial *pRear)
{
	Polynomial P;
	
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->coef = c;
	P->expon = e;
	P->link = NULL;
	(*pRear)->link = P;		//将当前项插入尾部 
	*pRear = P;				//当前项作为最后一项 
}

Polynomial ReadPoly()
{
	Polynomial P, Rear, t;
	int c, e, N;
	
	scanf("%d",&N);
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->link = NULL;
	Rear = P;
	while (N--){
		scanf("%d %d",&c, &e);
		Attach(c, e, &Rear);	//将当前项插入多项式尾部 
	}
	//删除临时生成的空头结点 
	t = P;
	P = P->link;
	free(t);
}

int Compare(int p1, int p2)
{
	if(p1 > p2)
		return 1;
	else if(p1 < p2)
		return -1;
	else
		return 0;		
}

Polynomial Add(Polynomial P1, Polynomial P2)
{
	Polynomial front, rear, temp;
	int sum;
	
	rear = (Polynomial)malloc(sizeof(struct PolyNode));
	front = rear;
	while (P1 && P2){
		switch(Compare(P1->expon, P2->expon)){
			case 1:
					Attach(P1->coef, P1->expon, &rear);
					P1 =  P1->link;
					break;
			case -1:
					Attach(P2->coef, P2->expon, &rear);
					P2 = P2->link;
					break;
			case 0:
					//如果系数不为0 
					sum = P1->coef + P2->coef;
					if(sum)
						Attach(sum, P1->expon, &rear);
						P1 = P1->link;
						P2 = P2->link;
						break;				
		}
	}
	//将未处理完的另外一个多项式的所有节点依次复制到结果多项式中 
	while(P1){
		Attach(P1->coef, P1->expon, &rear);
		P1 = P1->link;
	}
	while(P2){
		Attach(P1->coef, P2->expon, &rear);
		P2 = P2->link;
	}
	
	rear->link = NULL;
	temp = front;
	front = front->link;	//令front指向结果多项式的第一个非0项 
	free(temp);
	
	return front;
}

Polynomial Mult(Polynomial P1, Polynomial P2)
{
	Polynomial P, Rear, t1, t2, t;
	int c, e;
	
	if(!P1 || !P2)
		return NULL;
	t1 = P1;
	t2 = P2;
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	Rear = P;
	//先用P1的第一项,乘以P2的所有项,得到 P 
	while(t2){
		Attach(t1->coef * t2->coef, t1->expon + t2->expon, &Rear);
		t2 = t2->link;
	}
	t1 = t1->link;
	while (t1){
		t2 = P2;
		Rear = P;
		while (t2){
			c = t1->coef * t1->coef;
			e = t1->expon + t1->expon;
			while (Rear->link && Rear->link->expon > e)
				Rear = Rear->link;
			if(Rear->link && Rear->link->expon == e){
				//判断系数是否为0 
				if(Rear->link->coef + c)
					Rear->link->coef += c;
				else{ 
					t = Rear->link;
					Rear->link = t->link;
					free(t);
				}			
			}
			else{ 
				t = (Polynomial)malloc(sizeof(struct PolyNode));
				t->coef = c;
				t->expon = e;
				t->link = Rear->link;
				Rear->link = t;
				Rear = t;
			}	
			t2 = t2->link;
		}
		t1 = t1->link;
	}
	t2 = P;
	P = P->link;
	free(t2);
	
	return P;
}

void PrintPoly(Polynomial P)
{
	int flag = 0;
	
	if(!P){
		printf("0 0\n");
		return ;
	}
	while(P){
		if(!flag)
			flag = 1;
		else
			printf(" ");
		printf("%d %d", P->coef, P->expon);		
		P = P->link;
	}
	printf("\n");
}

int main()
{
	Polynomial P1, P2, PMult, PAdd;
	
	P1 = ReadPoly();
	P2 = ReadPoly();
	PMult = Mult(P1, P2);
	PrintPoly(PMult);
	PAdd = Add(P1, P2);
	PrintPoly(PAdd);
	
	return 0;
}


02-线性结构2 Reversing Linked List   (25分)


Given a constant KKK and a singly linked list LLL, you are supposed to reverse the links of every KKK elements on LLL. For example, given LLL being 1→2→3→4→5→6, if K=3K = 3K=3, then you must output 3→2→1→6→5→4; if K=4K = 4K=4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive NNN (≤105\le 10^5105) which is the total number of nodes, and a positive KKK (≤N\le NN) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then NNN lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1


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

typedef struct Node *pNode;
struct Node{
	int Address;
	int Data;
	int Next;
	pNode PNext;
};

pNode Match(int a, int d, int n)
{
	pNode P;
	 
	P = (pNode)malloc(sizeof(struct Node));
	P->Address = a;
	P->Data = d;
	P->Next = n;
	
	return P;
}

pNode CreateList(pNode Array[], int N)
{
	pNode front, rear;
	int i;
	
	front = (pNode)malloc(sizeof(struct Node));		//创建一个空的起始结点 
	rear = front;
	for(i = 1; i <= N; i++){
		rear->PNext = Array[i];
		rear = rear->PNext;
	}
	
	return front;
}

int choice(int K, int N)
{
	if(2 * K > N)
		return 1;	
	else 
		return 2;	
}

//情况1:前部分逆序,后部分正序 
void ReverseList_1(pNode List, int K, int N)
{
	pNode  old, fresh, temp;
	int cnt = 1;
	
	//fresh old temp三者关系画图理解,重新进行链表的排序 
	fresh = List->PNext;
	old = fresh->PNext;
	
	while (cnt < K){
		temp = old->PNext;
		old->PNext = fresh;
		fresh = old;
		old = temp;
		cnt++;			
	}
	List->PNext->PNext = old;	//将前部分和后部分进行粘合 
	
	for(cnt = 1; cnt < N; cnt++){
		printf("%05d %d %05d\n", fresh->Address, fresh->Data, fresh->PNext->Address);
		fresh = fresh->PNext;
	}
	printf("%05d %d -1\n", fresh->Address, fresh->Data);	//最后一项进行特殊处理 
	
}

//情况2:前部分逆序,后部分也逆序,注意cntd的次数 
void ReverseList_2(pNode List, int K, int N)
{
	pNode fresh_1, old, temp, fresh_2;
	int cnt = 1;
	
	fresh_1 = List->PNext;
	old = fresh_1->PNext;
	while (cnt < K){
		temp = old->PNext;
		old->PNext = fresh_1;
		fresh_1 = old;
		old = temp;
		cnt++;
	}
	//fresh_1是新的头结点 
	//分割成两部分 
	fresh_2 = old;
	old = fresh_2->PNext;
	cnt++;
	while (cnt < N){
		temp = old->PNext;
		old->PNext = fresh_2;
		fresh_2 = old;
		old = temp;
		cnt++;
	}
	List->PNext->PNext = fresh_2;	//粘合前后部分 
	
	for(cnt = 1; cnt < N; cnt++){
		printf("%05d %d %05d\n", fresh_1->Address, fresh_1->Data, fresh_1->PNext->Address);
		fresh_1 = fresh_1->PNext;
	}
	printf("%05d %d -1",fresh_1->Address, fresh_1->Data);
}

int main()
{
	int i, FirstAddress, N, K, Tag;
	int a, d, n;
	pNode P, List;
	
	//读入第一行数据 
	scanf("%d %d %d", &FirstAddress, &N, &K);
	pNode Array[N+1];	//指针数组, 每个元素指向结构 
	
	//将将每组数据按1 2 3 ...有序的放入数组中 
	for(i = 0; i < N; i++){
		scanf("%d %d %d", &a, &d, &n);
		Array[d] = Match(a, d, n);
	}
	 
	List = CreateList(Array, N);	//创建有序单链表 
	Tag = choice(K, N);
	switch(Tag){				//进行链表的逆序 
		case 1: ReverseList_1(List, K, N);
				break;
		case 2:	ReverseList_2(List, K, N);
				break;
	}
	
	return 0;	
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值