UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#212584#3839. 小t爱算数(mul)zyt042500ms0kbC++2.3kb2024-10-19 11:58:582024-10-19 12:36:56

answer

#include<bits/stdc++.h>
using namespace std;
struct P{
	int m,n;
	P*next;
};
P*scanf(){
	char a,b,c,d,e;
	P*head=NULL;
	P*start=new P;
	if(cin.peek()=='-'){
		char f;
		cin>>f>>start->m>>a>>b>>start->n;
		start->m=-start->m;
	}
	else{
		cin>>start->m>>a>>b>>start->n;
	}
	start->next=head;
	head=start;
	P*tail=start;
	if(cin.peek()!='-1'){
		while(cin>>c){
			P*p=new P;
			cin>>p->m>>d>>e>>p->n;
			if(c=='-')p->m=-p->m;
			p->next=tail->next;
			tail->next=p;
			tail=p;
			if(cin.peek()=='-1')break;
		}
	}
	return head;
}
void printf(P*head){
	if(head==NULL)cout<<0<<endl;
	else{
		P*cur=head;
		cout<<cur->m<<"x^"<<cur->n;
		cur=cur->next;
		while(cur!=NULL){
			if(cur->m<0)cout<<cur->m<<"x^"<<cur->n;
			else cout<<"+"<<cur->m<<"x^"<<cur->n;
			cur=cur->next;
		}
		cout<<endl;
	}
}
P*add(P*head1,P*head2){
	P*head=NULL;
	P*p1=head1;
	P*p2=head2;
	P*tail=head;
	while(p1!=NULL||p2!=NULL){
		if(p1->n>p2->n){
			P*p=new P;
			p->n=p1->n;
			p->m=p1->m;
			if(tail==NULL){
				p->next=NULL;
				head=p;
				tail=p;
			}
			else{
				p->next=tail->next;
				tail->next=p;
				tail=p;
			}
			p1=p1->next;
		}
		if(p2->n>p1->n){
			P*p=new P;
			p->n=p2->n;
			p->m=p2->m;
			if(tail==NULL){
				p->next=NULL;
				head=p;
				tail=p;
			}
			else{
				p->next=tail->next;
				tail->next=p;
				tail=p;
			}
			p2=p2->next;
		}
		if(p1->n==p2->n){
			P*p=new P;
			p->n=p1->n;
			p->m=p1->m+p2->m;
			if(p->m!=0){
				if(tail==NULL){
					p->next=NULL;
					head=p;
					tail=p; 
				}
				else{
					p->next=tail->next;
					tail->next=p;
					tail=p;
				}
			} 
			p1=p1->next;
			p2=p2->next;
		}
		if(p1=NULL){
			while(p2!=NULL){
				P*p=new P;
				p->n=p2->n;
				p->m=p2->m;
				if(tail=NULL){
					p->next=NULL;
					head=p;
					tail=p;
				}
				else{
					p->next=tail->next;
					tail->next=p;
					tail=p;
				}
				p2=p2->next;
			}
		}
		if(p2==NULL){
			while(p1!=NULL){
				P*p=new P;
				p->n=p1->n;
				p->m=p1->m;
				if(tail==NULL){
					p->next=NULL;
					head=p;
					tail=p;
				}
				else{
					p->next=tail->next;
					tail->next=p;
					tail=p;
				}
				p1=p1->next;
			}
		}
	}
	return head;
}
int main(){
	P*head1=scanf();
	P*head2=scanf();
	printf(add(head1,head2));
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 0
Runtime Error

input:

10 -7 9 3 7 4 6 -3 4 2 3 -7 2 -7 0 -3 -1
10 -9 8 9 7 -6 6 7 5 -2 3 -9 2 -9 1 7 0 3 -1

output:


result:


Test #2:

score: 0
Runtime Error

input:

4 3 2 -2 -1
4 3 2 2 -1

output:


result:


Test #3:

score: 0
Runtime Error

input:

100 -7 99 73 98 30 97 44 96 -23 95 -40 94 92 93 -87 92 -27 91 40 90 -3 89 -9 88 -60 87 99 86 -16 85 ...

output:


result:


Test #4:

score: 0
Runtime Error

input:

1000 -807 999 73 998 930 997 544 996 -923 995 -440 994 492 993 -987 992 -327 991 840 990 -303 989 -7...

output:


result:


Test #5:

score: 0
Runtime Error

input:

1000 -807 999 73 998 930 997 544 996 -923 995 -440 994 492 993 -987 992 -327 991 840 990 -303 989 -7...

output:


result:


Test #6:

score: 0
Runtime Error

input:

1000 -807 999 73 998 930 997 544 996 -923 995 -440 994 492 993 -987 992 -327 991 840 990 -303 989 -7...

output:


result:


Test #7:

score: 0
Runtime Error

input:

3 1 2 -1 0 1 -1
1 1 -1

output:


result:


Test #8:

score: 0
Runtime Error

input:

999 -807 998 73 997 930 996 544 995 -923 994 -440 993 492 992 -987 991 -327 990 840 989 -303 988 -70...

output:


result:


Test #9:

score: 0
Runtime Error

input:

0 -807 1 73 2 930 3 544 4 -923 5 -440 6 492 7 -987 8 -327 9 840 10 -303 11 -709 12 -560 13 99 14 -81...

output:


result:


Test #10:

score: 0
Runtime Error

input:

0 -807 1 73 2 930 3 544 4 -923 5 -440 6 492 7 -987 8 -327 9 840 10 -303 11 -709 12 -560 13 99 14 -81...

output:


result: