1154.cpp

来自「这是一个简单的排序。给你一个有N个值的数组」· C++ 代码 · 共 73 行

CPP
73
字号
#include<iostream.h>
//#include<iomanip>
//using namespace std;
struct t
{
	int data;
	struct t *next;
}*q, *p,*head;


void sort(int array[],int n)
{
	int j,i,t,k;
	for(i=0;i<n-1;i++)
	{
		k=i;
		for(j=i+1;j<n;j++)
			if(array[j]<array[k])
			k=j;
			t=array[k];
			array[k]=array[i];
			array[i]=t;
	}
		
}

int main()
{
	int T, N;
	cin>>T;
	head=NULL;
	p=new t;
	q=p;
	for(int i=0;i<T;i++)
	{
		cin>>N;
		while(N<1||N>1000)
		{
			cout<<"Input error!"<<endl;
			cin>>N;
		}
		int *n=new int[N];
		for(int j=0;j<N;j++)
			cin>>n[j];
         sort(n,N);
		 p=new t;
		 /*if(!p)
		 {
			 cout<<"\nApplition Memory falied\n";
			 exit(-1);
		 }*/
		 for(int s=0;s<N;s++)
		 { 
			 p->data=n[s];
			 if(!head)
				 head=p;
			 else 
				 q->next=p;
			 q=p;
			 p=new t;
		 }
	}
		 q->next=NULL;
		 t *m=head;
	     while(m!=NULL)
		 {
			 cout<<m->data<<endl;
			 m=m->next;
		 }
		 
		 return 0;
	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?