zoukankan      html  css  js  c++  java
  • 一元多项式的乘法与加法运算

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

    输入格式:

    输入分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

    代码实现:
    #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) {
    	if(c==0){return;}
      Polynomial P;
    	P = (Polynomial)malloc(sizeof(struct PolyNode));
    	P->coef = c;
    	P->expon = e;
    	P->link = NULL;
    	(*pRear)->link = P;   //注意一定要加括号()
    	*pRear = P;
    }
    Polynomial ReadPoly() {
    	int N,c,e;
    	Polynomial P, t, Rear;
    	P = (Polynomial)malloc(sizeof(struct PolyNode));
    	P->link = NULL;
    	Rear = P;
    	scanf("%d",&N);
    	while (N--) {
    		scanf("%d %d", &c, &e);
    		if( c != 0) Attach(c,e,&Rear);
    	}
    	t = P;
    	P = P->link;
    	free(t);
    	return P;
    }
    int Compare(int a,int b) {
    	if (a>b) {
    		return 1;
    	}else if (a<b) {
    		return -1;
    	}
    	return 0;
    }
    Polynomial Add(Polynomial P1, Polynomial P2) {
    	Polynomial T1, T2, P,  rear, tempP;
    	int sum;
    	if (!P1 && !P2) { return NULL; }
    	T1 = P1;
    	T2 = P2;
    	P = (Polynomial)malloc(sizeof(struct PolyNode));
    	P->link = NULL;
    	rear = P;
    	while (T1 && T2) {
    		switch (Compare(T1->expon, T2->expon)) {
    		case 1:
    			Attach(T1->coef, T1->expon, &rear);
    			T1 = T1->link;
    			break;
    		case -1:
    			Attach(T2->coef, T2->expon, &rear);
    			T2 = T2->link;
    			break;
    		case 0:
    			sum = T1->coef + T2->coef;
    			if (sum) Attach(sum, T1->expon, &rear);
    			T1 = T1->link;
    			T2 = T2->link;
    			break;
    		}
    	}
    	for (; T1; T1 = T1->link) Attach(T1->coef, T1->expon, &rear);
    	for (; T2; T2 = T2->link) Attach(T2->coef, T2->expon, &rear);
    	rear->link = NULL;
    	tempP = P; 
    	P = P->link; 
    	free(tempP);
    	return P;
    }
    Polynomial Mult(Polynomial P1,Polynomial P2) {
    	Polynomial P, T1, T2, Rear,tempP,t;
    	int c, e;
    	if (!P1 || !P2) { return NULL; }
    	T1 = P1;
    	T2 = P2;
    	P = (Polynomial)malloc(sizeof(struct PolyNode));
    	P->link = NULL;
    	Rear = 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 * T2->coef;
    			e = T1->expon + T2->expon;
    			while (Rear->link && Rear->link->expon > e) {
    				Rear = Rear->link;
    			}
    			if (Rear->link && Rear->link->expon == e) {
    				if (Rear->link->coef + c) { //!=0时
    					Rear->link->coef += c;
    				}
    				else {
    					tempP = Rear->link;
    					Rear->link = tempP->link;
    					free(tempP);
    				}
    			}
    			else {
    				t = (Polynomial)malloc(sizeof(struct PolyNode));
    				t->coef = c;
    				t->expon = e;
    				t->link = Rear->link;
    				Rear->link = t;
    				Rear = Rear->link;
    			}
    			T2 = T2->link;
    		}
    		T1 = T1->link;
    	}
    	tempP = P;
    	P = P -> link;
      free(tempP);
    	return P;
    }
    void PrintPoly(Polynomial P) {
    	int flag = 0;
    	if (!P) {printf("0 0
    "); return;}
    	while (P) {
    		if (!flag) {
    			flag = 1;
    		}
    		else {
    			printf(" ");
    		}
    		printf("%d %d",P->coef,P->expon);
    		P = P->link;
    	}
    	printf("
    ");
    }
    
    int main(){
    	Polynomial P1, P2, PP, PS;
    	P1 = ReadPoly();
    	P2 = ReadPoly();
    	PP = Mult(P1,P2);
    	PrintPoly(PP);
    	PS = Add(P1, P2);
    	PrintPoly(PS);
        return 0;
    }
    
  • 相关阅读:
    pytorch实现BiLSTM+CRF用于NER(命名实体识别)
    pytorch中如何处理RNN输入变长序列padding
    pytorch nn.LSTM()参数详解
    Pytorch的LSTM的理解
    转:pytorch版的bilstm+crf实现sequence label
    【Tensorflow】tf.nn.atrous_conv2d如何实现空洞卷积?膨胀卷积
    iOS iphone5屏幕适配 autosizing
    IOS文件存储小结
    IIS6_IIS7日志文件位置
    xcode中没有autoSizing的设置
  • 原文地址:https://www.cnblogs.com/TangYJHappen/p/12988958.html
Copyright © 2011-2022 走看看