心动C++ 情牵基础算法 I

/*-----------------------------------------------------------------------------
 * Project: Max.cpp
 * Name: zwp
 * Date: 2013.11
 *------------------------------------------------------------------------------*/



#include <iostream>


#define		MAX		10

int numbers[MAX] = 
{
	10, 12, 13, 14, 9, 16, 17, 18, 19, 20 
};


int max(int* num, int size)
{
	int	max = num[0];

	for(int index = 1; index < size; ++ index)
	{
		if(max < num[index])
			max = num[index];
	}

	return max;
}


int min(int* num, int size)
{
	int min = num[0];

	for(int index = 1; index < size; ++ index)
	{
		if(min > num[index])
			min = num[index];
	}

	return min;
}


bool search(int* num, int size, int special)
{
	for(int index = 0; index < size; ++ index)
	{
		if(num[index] == special)
			return true;
	}

	return false;
}

bool binary_search(int* num, int size, int special)
{
	int index = 0;				// left
	int indeu = size - 1;		// right
	int mid = 0;				// middle
	int location = 0;			// flag
	
	while(index < indeu)
	{
		mid = (index + indeu)/2;
		if(special > num[mid])
			index = mid + 1;
		else
			indeu = mid;
	}
	if(special == num[index])
		location = index;
	else
		location = 0;

	return location;
}


void bubble_sort(int* num, int size)
{
	int temp = 0;

	for(int index = 0; index < size - 1; ++ index)
		for(int indeu = 0; indeu < size - (index+1); ++ indeu)
			if(num[indeu] > num[indeu+1])
			{
				temp = num[indeu];
				num[indeu] = num[indeu+1];
				num[indeu+1] = temp;
			}

	for(int index = 0; index < size; ++ index)
	{
		printf("%d  ", num[index]);
	}
}


void insert_sort(int* num, int size)
{
	int index = 0;
	int special = 0;

	for(int indeu = 1; indeu < size; ++ indeu)
	{
		index = 0;
		while(num[indeu] > num[index])
			index++;

		special = num[indeu];
		for(int indem = 0; indem < indeu - index - 1; ++ indem)
			num[indeu-indem] = num[indeu - indem - 1];
		num[index] = special;
	}

	for(int index = 0; index < size; ++ index)
		printf("%d  ", num[index]);

}


int scon[] =
{
	25, 10, 5, 1
};
void change(int* sco, int n)
{
	for(int index = 0; index < sizeof(sco)/sizeof(sco[0]); ++ index)
	{
		while(n > sco[index] && n == sco[index])
		{
			n -= sco[index];		// 把面值为sco[index] 的硬加入币所找的零钱中
			printf("%d  ", sco[index]);
		}
	}
}
	

	
int main(int argc, char* argv[])
{
	if(binary_search(numbers, MAX, 0))
		printf("Search it ....");
	else
		printf("not found!....");

	insert_sort(numbers, MAX);

	change(scon, 74);

	system("pause");
	return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值