PTA 多项式相加 Java代码

题目简介

某天,dh 得到两个多项式,他现在想知道两个多项式相加之后每一项的系数,但是 dh 的数学不好,所以他打算求助聪明的你。

输入格式

输入一共有两行,第一行表示第一个多项式,第二行表示第二个多项式。
每一行第一个数字 n,表示该多项式一共有 n 项,然后后面有 n 对数字 a i a_i ai, b i b_i bi ,第一个数字 a i a_i ai 表示系数,第二个数字 b i b_i bi 表示指数。
每一行的指数可能出现多次,并且不保证指数有序。
(0 <= a i a_i ai, b i b_i bi <=9) , (1<=n<=10)

输出格式

输出一共一行,按照指数从大到小,输出系数,每个系数之间用空格隔开(如果系数为 0 则不输出)。

注意行末无空格。

输入样例

在这里给出一组输入。例如:

4 2 3 3 2 1 1 0 0
3 2 3 3 2 1 1

输出样例

在这里给出相应的输出。例如:

4 6 2

tips

第一个多项式是 2 ∗ x 3 + 3 ∗ x 2 + 1 ∗ x 1 + 0 ∗ x 0 2*x^3+3*x^2+1*x^1+0*x^0 2x3+3x2+1x1+0x0
第二个多项式是 2 ∗ x 3 + 3 ∗ x 2 + 1 ∗ x 1 2*x^3+3*x^2+1*x^1 2x3+3x2+1x1
两个多项式相加后是
4 ∗ x 3 + 6 ∗ x 2 + 2 ∗ x 1 + 0 ∗ x 0 4*x^3+6*x^2+2*x^1+0*x^0 4x3+6x2+2x1+0x0
题目要求输出系数不为 0 的项,所以只输出 4 6 2,0 不输出

代码如下

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		int n,m,a,b;
		Scanner scan=new Scanner(System.in);
		n=scan.nextInt();
		int []num=new int[10];
		while(n!=0) {
			a=scan.nextInt();
			b=scan.nextInt();
			if(a<0&&b%2==0) {
				a=-a;
			}
			num[b]+=a;
			n--;
		}
		m=scan.nextInt();
		while(m!=0) {
			a=scan.nextInt();
			b=scan.nextInt();
			if(a<0&&b%2==0) {
				a=-a;
			}
			num[b]+=a;
			m--;
		}
		int flag=0;
		int i;
		for(i=9;i>=0;i--) {
			if(num[i]!=0) {
				if(flag==0) {
					System.out.print(num[i]);
					flag++;
				}
				else {
					System.out.print(" "+num[i]);
				}
			}
		}
        System.out.println("");
	}
}

还请大佬多多指教!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,我可以给你介绍一下C语言链表多项式相加代码。下面是一个简单的示例: ```c #include <stdio.h> #include <stdlib.h> // 定义多项式的节点结构 typedef struct Node { int coefficient; // 系数 int exponent; // 指数 struct Node* next; } Node; // 创建一个新节点 Node* createNode(int coefficient, int exponent) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->coefficient = coefficient; newNode->exponent = exponent; newNode->next = NULL; return newNode; } // 向多项式中插入一个节点 void insertNode(Node** poly, int coefficient, int exponent) { Node* newNode = createNode(coefficient, exponent); if (*poly == NULL) { *poly = newNode; } else { Node* temp = *poly; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } } // 打印多项式 void printPolynomial(Node* poly) { Node* temp = poly; while (temp != NULL) { printf("%dx^%d ", temp->coefficient, temp->exponent); if (temp->next != NULL) { printf("+ "); } temp = temp->next; } printf("\n"); } // 相加两个多项式 Node* addPolynomials(Node* poly1, Node* poly2) { Node* result = NULL; Node* temp1 = poly1; Node* temp2 = poly2; while (temp1 != NULL && temp2 != NULL) { if (temp1->exponent > temp2->exponent) { insertNode(&result, temp1->coefficient, temp1->exponent); temp1 = temp1->next; } else if (temp1->exponent < temp2->exponent) { insertNode(&result, temp2->coefficient, temp2->exponent); temp2 = temp2->next; } else { int sum = temp1->coefficient + temp2->coefficient; if (sum != 0) { insertNode(&result, sum, temp1->exponent); } temp1 = temp1->next; temp2 = temp2->next; } } // 将剩余的节点添加到结果中 while (temp1 != NULL) { insertNode(&result, temp1->coefficient, temp1->exponent); temp1 = temp1->next; } while (temp2 != NULL) { insertNode(&result, temp2->coefficient, temp2->exponent); temp2 = temp2->next; } return result; } int main() { Node* poly1 = NULL; Node* poly2 = NULL; // 创建第一个多项式 insertNode(&poly1, 3, 4); insertNode(&poly1, 2, 3); insertNode(&poly1, 5, 1); // 创建第二个多项式 insertNode(&poly2, 4, 3); insertNode(&poly2, 1, 2); insertNode(&poly2, 2, 0); printf("第一个多项式:"); printPolynomial(poly1); printf("第二个多项式:"); printPolynomial(poly2); Node* result = addPolynomials(poly1, poly2); printf("相加后的多项式:"); printPolynomial(result); return 0; } ``` 这段代码实现了链表表示的多项式相加。首先定义了一个多项式的节点结构,包含系数和指数,并且有一个指向下一个节点的指针。然后通过`createNode`函数创建一个新节点,`insertNode`函数向多项式中插入一个节点,`printPolynomial`函数打印多项式。 最后,`addPolynomials`函数实现了两个多项式相加的功能。它遍历两个多项式的节点,根据指数的大小进行比较,将节点插入到结果多项式中。如果指数相同,则将系数相加,如果和不为0,则插入到结果多项式中。 在`main`函数中,创建了两个多项式,并调用`addPolynomials`函数进行相加操作,最后打印出相加后的多项式
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值