求助 #5 WA 用的递归
查看原帖
求助 #5 WA 用的递归
250520
我是超级傻逼楼主2022/7/27 09:51

定义

double v,q;//v容量     q热值 
double maxx;//最大行驶里程 
int n;//加油站数 
double m;//预算 
struct qwe{
	double d,p;
}a[15];//加油站的距离起点"距离"与"油价" 

递归

void start(int stay,double slack){//站数 剩的油 
	if(stay==n+1){
		return;
	}
	int last=stay+1;
	for(int i=stay+1;i<=n+1;i++){
		if(a[i].d-a[stay].d<maxx){
			if(a[i].p<a[stay].p){
				m+=((a[i].d-a[stay].d)/q)*a[stay].p;
				start(i,0);
				return;
			}
			if(a[i].p<a[last].p){
				last=i;
			}
		}
		else{
			break;
		}
	}
	if(last==n+1){
		m+=((a[n+1].d-a[stay].d)/q)*a[stay].p;
		start(n+1,0);
	}
	m+=v*a[stay].p;
	start(last,v-(a[last].d-a[stay].d)/q);
	return;
}

基础

double zhuan;
	cin>>zhuan>>v>>q>>a[0].p>>n;
	a[n+1].d=zhuan;
	m=0;
	a[0].d=0;
	a[n+1].p=-1;
	maxx=v*q;
	for(int i=1;i<=n;i++){
		cin>>a[i].d>>a[i].p;
	}
	for(int i=1;i<=n+1;i++){
		if(maxx<a[i].d-a[i-1].d){
			cout<<"No Solution";
			return 0;
		}
	}
	start(0,0);
	cout<<setprecision(2)<<fixed<<m<<endl;
2022/7/27 09:51
加载中...