完了做成屎山了,这代码是救还是重写啊。。。
查看原帖
完了做成屎山了,这代码是救还是重写啊。。。
1075989
BlauAnthony楼主2024/12/8 16:23

直接看屎山(全是MLE):

#include<iostream>
#include<math.h>
using namespace std;
typedef long long ll;
ll n;
struct power{
	int exponent;
	bool yes;
	power *fnext;
	power *next;
	power(int e){
		exponent=e;
		next=nullptr;
		fnext=nullptr;
		yes=0;
	}
	power(){
		next=nullptr;
		fnext=nullptr;
		yes=0;
	}
};
void fd(ll now,power *head){//fd意为发癫 
	power *glast,*last;
	int pie=0;
	while(now){
		last=new power();
		if(pie==0){
			head=last;
		}else{
			glast->next=last;
		}
		last->exponent=int(log2(n));
		n-=pow(2,last->exponent);
		glast=last;
		last=last->next;
		pie++;
	}
	power *tmp=head;
	for(int i=0;i<pie;i++){
		if(tmp->exponent>2){
			tmp->yes=1;
			fd(tmp->exponent,tmp->fnext);
		}
		tmp=tmp->next;
	}
}
void out(power *head){
	power *tmp=head;
	while(tmp!=nullptr){
		cout<<'2';
		if(tmp->exponent==1){
			tmp=tmp->next;
			continue;
		}
		cout<<'('
		if(tmp->yes==1){
			out(tmp->fnext);
		}
		cout<<')';
		if(tmp->next!=nullptr){
			cout<<'+';
		}
		tmp=tmp->next;
	}
}
int main(){
	power *head;
	cin>>n;
	fd(n,head);
	out(head);
	return 0;
}
2024/12/8 16:23
加载中...