数组的问题= =大神求指教

xiaoxiaohaihaizizi 2012-11-16 12:16:25

Description

输入一个二维数组A[5][5](无序),将其所有元素排序后从小到大按照A[0][0],A[0][1],…,A[0][4],A[1][0],…,A[4][4]顺序存放,请写出一个比较高效率的程序搜索任一输入值x在经过上述处理后的数组中的位置,如果不在数组中,则输出-1,如果x不止出现1次,则输出在处理后二维数组中靠左或者靠上的一个。
Input

第一行为测试用例的数量,从第二行开始,每6行为一组测试数据,每组测试数据的第一行为输入值x,接下来的每一行测试数据中有5个整数,用空格分隔
Output

每组测试用例的结果输出到一行,每组结果包含两个整数,分别是输入值x所在的行号和列号,如果x不在数组中,则行和列均输出-1
Sample Input
Copy sample input to clipboard

6
10
8 1 2 7 2
4 6 1 3 5
10 9 6 20 3
8 7 12 4 5
22 21 16 23 4
-2
8 6 7 1 2
10 9 12 3 6
1000 2 9 6 9
4 20 28 32 37
0 1 7 -1 2
1000
-10 -1000 2 4 5
1000 2000 3000 2768 3125
2000 1514 1357 1000 0
5 43 3 2 1
3 8 6 7 1
11
1 111 11 1111 111
1010 1001 1100 1110 1101
1011 101 11 1 -1
10 100 110 120 119
1212 213 1324 10 8
9999
8 1 9 2 0
2 6 5 1 3
6 7 2 1 3
7 5 6 4 0
0 9 3 8 8
14
-2 0 50 70 30
3 6 90 20 25
13 15 24 17 -14
100 98 64 43 40
0 93 88 43 15

Sample Output

3 3
-1 -1
3 1
1 1
-1 -1
-1 -1

Hint

二分的思想
...全文
238 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
JiMoKuangXiangQu 2012-11-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

int intcmp(const void *a, const void *b)
{
	const int ia = *(const int *)a;
	const int ib = *(const int *)b;

	if (ia > ib) return 1;
	else if (ia == ib) return 0;
	else return -1;
}

int binary_search(int arr[], int n, int key)
{
	int high, low, mid;

	low = 0;
	high = n - 1;
	while (low <= high)
	{
		mid = (low + high) / 2;
		if (key > arr[mid])
			low = mid + 1;
		else if (key < arr[mid])
			high = mid - 1;
		else
			return mid;
	}
	return -1;
}

#define N 5
#define M 5

typedef struct coordinate { 
	int x; int y; 
} coordinate_t;

int main(void)
{
	int arr[N][M];
	int tcn, key, pos;
	coordinate_t *cos;
	int i, j, k;

	printf("请输入测试用例数量:");
	scanf("%d", &tcn);

	if ((cos = (coordinate_t *)malloc(sizeof(coordinate_t) * tcn)) == NULL)
	{
		fprintf(stderr, "Out of memory!\n");
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < tcn; i++)
	{
		printf("请输入要查找的数字:");
		scanf("%d", &key);

		printf("请输入被查找的数据:\n");
		for (j = 0; j < N; j++)
			for (k = 0; k < M; k++)
				scanf("%d", &arr[j][k]);

		// 排序输人数据
		qsort((void *)arr, N*M, sizeof(int), intcmp);

		pos = binary_search((int *)arr, N*M, key);
		if (pos != -1)
		{
			cos[i].x = pos / M;
			cos[i].y = pos % M;
		}
		else
		{
			cos[i].x = -1;
			cos[i].y = -1;
		}
		/*{
#define NUM_ELEM(a) sizeof(a) / sizeof((a)[0])
#define CREATE_TC(nm) {#nm, NUM_ELEM(nm), nm}
			typedef struct test_case {
				const char* name;
				int n;
				int *data;
			} tc_t;

			int t1[1] = {1};
			int t2[2] = {1, 3};
			int t3[3] = {1, 3, 5};
			int t4[4] = {1, 3, 5, 7};
			int t5[5] = {1, 3, 5, 7, 9};
			
			tc_t tcs[] = {
				CREATE_TC(t1), CREATE_TC(t2), CREATE_TC(t3), CREATE_TC(t4), CREATE_TC(t5)
			};
			int i;

			binary_search(t1, 0, 100);
			for (i = 0; i < NUM_ELEM(tcs); i++)
			{
				int s = tcs[i].data[0] - 1;
				int e = tcs[i].data[tcs[i].n - 1] + 1;

				printf("searching %s...\n", tcs[i].name);
				for (key = s; key <= e; key++)
				{
					int pos = binary_search(tcs[i].data, tcs[i].n, key);
					if (pos != -1)
						printf("Found %d from %s at postion %d\n", key, tcs[i].name, pos);
					else
						printf("Can't find %d from %s\n", key, tcs[i].name, pos);
				}
				printf("\n\n");
			}
		}*/
	}

	for (i = 0; i < tcn; i++)
		printf("(%d, %d)\n", cos[i].x, cos[i].y);

	return 0;
}
xiaoxiaohaihaizizi 2012-11-16
  • 打赏
  • 举报
回复
唔。。。不一样么~直接给你全分不就行了么= =Orz
dpdp_2012 2012-11-16
  • 打赏
  • 举报
回复
重新开帖,给你答案
xiaoxiaohaihaizizi 2012-11-16
  • 打赏
  • 举报
回复
楼上大神膜拜orz= = 还有一个问题,不知道能不能伸出援手,帮我解决 Description 试从含有n个int型数的数组中删去若干个成分,使剩下的全部成分构成一个不减的子序列(序列中后面的数总大于等于前面的数)。设计算法和编写程序求出数组的最长不减子序列及其长度。若可以同时删去两个数,优先删去较小的数。 Input 第一行为测试用例的数量,从第二行开始,每组测试用例两行,第一行为数组中元素的个数n,第二行为该测试用例的所有数组元素,元素之间用空格分隔。 Output 每组测试数据输出两行,第一行为最长不减子序列的长度,第二行为该最长不减子序列 Sample Input Copy sample input to clipboard 8 8 1 2 4 5 6 6 8 3 9 1 1 2 4 4 3 5 9 8 5 1 3 5 7 9 5 1 3 2 5 4 8 7 6 5 5 4 3 2 1 5 1 2 4 3 6 2 4 2 10 9 1 3 5 7 6 7 8 12 12 Sample Output 7 1 2 4 5 6 6 8 7 1 1 2 4 4 5 9 5 1 3 5 7 9 3 1 3 5 2 5 5 4 1 2 4 6 1 4 8 1 3 5 7 7 8 12 12
转角天边 2012-11-16
  • 打赏
  • 举报
回复
冒似楼主发了两个一模一样的贴子
rocktyt 2012-11-16
  • 打赏
  • 举报
回复
发2遍做什么http://bbs.csdn.net/topics/390283058 只要通过的话根本不需要排序,当然学习一下算法也是好的
dpdp_2012 2012-11-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

int cmp(const void *a,const void *b)
{
	return *(int*)a-*(int*)b;
}

int main()
{
	int t,i,j,x,index,a[5][5];
	int *p=(int*)a;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&x);
		for(i=0;i<5;i++)
			for(j=0;j<5;j++)
				scanf("%d",&a[i][j]);
		qsort(p,25,sizeof(int),cmp);
		i=0,j=24,index=-1;
		while(i<=j)
		{
			int mid=(i+j)/2;
			if(p[mid]==x)
			{
				if(mid>0&&p[mid-1]==x)
					j=mid-1;
				else
				{
					index=mid;
					break;
				}
			}
			else if(p[mid]>x)
				j=mid-1;
			else
				i=mid+1;
		}
		if(index==-1)
			printf("-1 -1\n");
		else
			printf("%d %d\n",index/5,index%5);
	}
}

或者输入的时候不要a[][],直接

{
	for(i=0;i<5;i++)
		for(j=0;j<5;j++)
			scanf("%d",&p[i*5+j]);
}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧