1. 程式人生 > >uva 10474 Where is the Marble?(簡單題)

uva 10474 Where is the Marble?(簡單題)

content mil stdlib.h std lib [0 數據 main pre

我非常奇怪為什麽要把它歸類到回溯上,明明就是簡單排序,查找就OK了。wa了兩次,我還非常不解的懷疑了為什麽會

wa,原來是我居然把要找的數字也排序了,當時僅僅是想著能快一點查找。所以就給他排序了,沒考慮到要按給的順序輸

出答案,這次真是二了,。,看別人題解實用打表做的,那個應該是正確解法。我的耗時980ms,預計數據再大一些就

要TLE了

貼代碼:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
	return *(int *)a - *(int *)b;
}
int main()
{
	int m,n,j,i;
	int a[10005];
	int b[10005];
	int cnt = 0;
	while(scanf("%d%d",&n,&m),n&&m)
	{
		cnt ++;
		for(i=1; i<=n; i++)
			scanf("%d",&a[i]);
		for(j=1; j<=m; j++)
			scanf("%d",&b[j]);
		qsort(a+1,n,sizeof(a[0]),cmp);
		int k = 1;
		printf("CASE# %d:\n",cnt);
		for(i=1; i<=m; i++)
		{
			int flag = 0;
			for(j=1; j<=n; j++)
			{
				if(b[i] == a[j])
				{
					flag = 1;
					break;
				}
			}
			if(flag)
			{
				printf("%d found at %d\n",b[i],j);
			}
			else
			{
				printf("%d not found\n",b[i]);
			}
		}
	}
	return 0;
}


uva 10474 Where is the Marble?(簡單題)