码迷,mamicode.com
首页 > 编程语言 > 详细

脑洞题目 - 改自从一组无序数组中找不存在的最小正整数

时间:2017-09-22 00:57:29      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:temp   bsp   eof   class   main   []   col   printf   std   

    无聊想的题目,但题目创意改自从一组无序数组中找不存在的最小正整数。并不难,但也有陷阱,比考虑0和负数...

/**
 *    从无序数组中找不存在的最小正整数
 *    我的要求:比如:{3,4,6,9,20}中 最小的不存在的正整数为2
 */
#include <stdio.h>

int main()
{
	int arr[] = {-5, -10, 42, 29, 18, -3, 8, 20, -1};
	int i, j, temp;


	for(i = 1; i < sizeof(arr)/sizeof(int); i++)
	{
		j = i - 1;
		if(arr[j] < arr[i] && arr[j] > 0)
			temp = arr[j];
	}
	temp --;

	printf("%d\n", temp);
	return 0;
}

 

脑洞题目 - 改自从一组无序数组中找不存在的最小正整数

标签:temp   bsp   eof   class   main   []   col   printf   std   

原文地址:http://www.cnblogs.com/darkchii/p/7571976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!