CSDN学院数据结构算法学习示例
//输入一个无序的数组 {3, 7, 8, 2, 6, 7, 5, 9, 9, 1} ,请按照如下的方式输出结果, 要求两个数的相加和为10 .比如 9,1 ; 9; 8,2 ; 7, 3; 7; 6
#include "stdafx.h"
#include <stdlib.h>
void SumTenFunc(int *number, int length)
{
if (number == NULL || length <= 0)
{
return;
}
//顺序排序
int flog = 0;
for (int i = 0; i <= length - 1; ++i)
{
flog = 0;
for (int j = length - 1; j >= i; j--)
{
if(number[j] < number[j - 1])
{
int temp = number[j - 1];
number[j - 1] = number[j];
number[j] = temp;
flog = 1;
}
}
if(1 == flog)
{
continue;
}
}
int low = 0;
int high = length - 1;
while(low < high)
{
//小于等于 10 的 同时相加 当两个数之和小于10的时候 ,如果只是
if (number[low] + number[high] == 10)
{
printf("%d, %d \n", number[low], number[high]);
//先中间移动
++low;
--high;
}
else if (number[low] + number[high] > 10)
{
printf("%d \n", number[high]);
--high;
}
else if (number[low] + number[high] < 10)
{
//printf("%d %d \n", number[high], number[low]);
++low;
}
}
}
void Test()
{
//int array1[] = {3, 7, 8, 2, 6, 7, 5, 9, 9, 1};
//int array1[] = {1, 2, 3, 4, 5, 6, 7, 9};
int array1[] = {1, 2, 3, 3, 4, 5, 6, 7, 9};
SumTenFunc(array1, sizeof(array1)/ sizeof(int));
}
int _tmain(int argc, _TCHAR* argv[])
{
Test();
system("pause");
return 0;
}