/*-----------------------------------------------------------------------------
* 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;
}
心动C++ 情牵基础算法 I
最新推荐文章于 2022-11-24 19:11:28 发布