310.內存分配

#include <bits/stdc++.h>
using namespace std;
int main()
{
	
	int t;
	scanf("%d", &t);
	while(t--)
	{
		int n;
		scanf("%d", &n);
		int a[110];
		for(int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		sort(a, a+n);
		
		int m;
		scanf("%d", &m);
		int tmp[110];
		memset(tmp, -1, sizeof(tmp));
		int cnt = 0;
		for(int i = 0; i < m; i++)
		{
			int x;
			
			scanf("%d", &x);
			int flag = 0;
			for(int j = 0; j < n; j++)
			{
				if(x <= a[j])
				{
					
				  tmp[cnt++] = a[j];
				  a[j] = -1;//內存塊不能重複分配 
				  sort(a, a+n);
				  flag = 1;break;
				}
			}
			if(!flag)
				cnt++;
			
		} 
		for(int j = 0; j < cnt - 1; j++)
		{
			if(tmp[j]!=-1)
				printf("%d ",tmp[j]);
			else
				printf("NULL ");
		}
		if(tmp[cnt-1]!=-1)
			printf("%d\n",tmp[cnt-1]);
		else
			printf("NULL\n");
		
	}
}
/*
2
4
7 5 10 3
2
4 6
4
3 5 9 10
3
5 12 6
*/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章