10分求调!
  • 板块学术版
  • 楼主jinhaoxuan001
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/5 11:50
  • 上次更新2025/1/5 17:26:01
查看原帖
10分求调!
1351940
jinhaoxuan001楼主2025/1/5 11:50

P2141 高精度减法

#include<stdio.h>
#include<stdlib.h>
struct node{
	int data;
	struct node *left,*right;
}*head_1,*tail_1,*head_2,*tail_2,*head_3,*tail_3;
void Input(){
	for(char temp;scanf("%c",&temp)&&temp!='\n';){
		struct node *p=(struct node*)malloc(sizeof(struct node));
		p->data=temp-'0';
		p->right=NULL;
		p->left=tail_1;
		tail_1->right=p;
		tail_1=p;
		head_1->data++;
	}
	for(char temp;scanf("%c",&temp)&&temp!='\n';){
		struct node *p=(struct node*)malloc(sizeof(struct node));
		p->data=temp-'0';
		p->right=NULL;
		p->left=tail_2;
		tail_2->right=p;
		tail_2=p;
		head_2->data++;
	}	
}
void Completion(){
	if(head_1->data>head_2->data){
		for(int i=1;i<=head_1->data-head_2->data;i++){
			struct node *p=(struct node*)malloc(sizeof(struct node));
			p->data=0;
			struct node *t=head_2->right;
			t->left=p;			
			head_2->right=p;			
			p->right=t;
			p->left=head_2;
		}
	}else if(head_2->data>head_1->data){
		for(int i=1;i<=head_2->data-head_1->data;i++){
			struct node *p=(struct node*)malloc(sizeof(struct node));
			p->data=0;
			struct node *t=head_1->right;
			t->left=p;			
			head_1->right=p;			
			p->right=t;
			p->left=head_1;
		}		
	}
}
void Zero_Suppression(){
	for(struct node *p=tail_3,*t=NULL;p!=head_3&&p->data==0;t=p,p=p->left,tail_3=p,p->right=NULL,free(t),t=NULL);
}
void Output(){
	for(struct node *p=tail_3;p!=head_3;p=p->left){
		printf("%d",p->data);
	}
}
int ifLIST(){
	for(struct node *p=head_1->right,*t=head_2->right;p!=NULL&&t!=NULL;p=p->right,t=t->right){
		if(p->data<t->data){
			printf("-");
			return 0;
		}else if(p->data>t->data){
			return 1;
		}
	}
	printf("0");
	exit(0);		
}
void Subtraction(){
	int x=0;
	if(ifLIST()){
		for(struct node *p=tail_1,*t=tail_2;p!=head_1&&t!=head_2;p=p->left,t=t->left){
			struct node *k=(struct node*)malloc(sizeof(struct node));
			if(p->data-t->data-x<0){
				k->data=10+p->data-t->data-x;
				x=1;
			}else{
				k->data=p->data-t->data-x;
				x=0;
			}
			tail_3->right=k;
			k->left=tail_3;
			k->right=NULL;
			tail_3=k;
		}
	}else{
		for(struct node *p=tail_1,*t=tail_2;p!=head_1&&t!=head_2;p=p->left,t=t->left){
			struct node *k=(struct node*)malloc(sizeof(struct node));
			if(t->data-p->data-x<0){
				k->data=10+t->data-p->data-x;
				x=1;
			}else{
				k->data=t->data-p->data-x;
				x=0;
			}
			tail_3->right=k;
			k->left=tail_3;
			k->right=NULL;
			tail_3=k;
		}		
	}
}
void Free(){
	for(struct node *p=head_1,*t=NULL;p!=NULL;t=p,p=p->right,free(t),t=NULL);
	for(struct node *p=head_2,*t=NULL;p!=NULL;t=p,p=p->right,free(t),t=NULL);
	for(struct node *p=head_3,*t=NULL;p!=NULL;t=p,p=p->right,free(t),t=NULL);
	head_1=tail_1=head_2=tail_2=head_3=tail_3=NULL;
}
int main(){
	head_1=tail_1=(struct node*)malloc(sizeof(struct node));
	tail_1->data=0;
	tail_1->right=tail_1->left=NULL;
	head_2=tail_2=(struct node*)malloc(sizeof(struct node));
	tail_2->data=0;
	tail_2->right=tail_2->left=NULL;
	head_3=tail_3=(struct node*)malloc(sizeof(struct node));
	tail_3->data=0;
	tail_3->right=tail_3->left=NULL;
	Input();
	Completion();
	Subtraction();
	Zero_Suppression();
	Output();
	Free();
	return 0;
}
2025/1/5 11:50
加载中...