这个代码为什么有问题啊()
查看原帖
这个代码为什么有问题啊()
1041576
hejingchi0309楼主2024/9/8 20:39
#include <iostream>
using namespace std;
int main()
{
	int i,j,N,tall;
	int head,tail,sum,ans;
	int pre,next;
	sum=0;ans=0;
	cin>>N>>tall;
	int cow[N][3];
	cin>>cow[0][0];
    cow[0][1]=-1;
	cow[0][2]=-1;
	head=0;	
	for(i=1;i<N;i++){
		cin>>cow[i][0];
		if(cow[i][0]>=cow[head][0]){
			cow[head][1]=i;//cow[head][1]指向更大的元素. 
			cow[i][1]=-1;
			cow[i][2]=head;
			head=i;
		}else{
			pre=head;
			next=cow[pre][2];//cow[pre][2]指向更小的元素. 
			while(next!=-1 and cow[i][0]<cow[next][0]){
				pre=next;
				next=cow[pre][2];
			}		//pre表示的是cow[i][2]应该指向的量	
			cow[i][1]=pre;
			cow[i][2]=next;
			cow[pre][2]=i;
			cow[next][1]=i;
			
		}
	}
	cout<<endl;
    while(head!=-1){
    	sum=sum+cow[head][0];
		ans+=1; 
    	head=cow[head][2];
    	if(sum>=tall){
    		cout<<ans<<endl;
    		return 0; 
		}
	}
		return 0;
}







2024/9/8 20:39
加载中...