排序题。
#include <stdio.h>
#include <stdlib.h>
void HeapSort(int a[], int n);
void HeapAdjust(int a[], int i, int n);
int main()
{
int n, m,i,j;
scanf("%d %d", &n, &m);
int *Coin = (int*)malloc(sizeof(int)*n);
for (i = 0; i < n; i++)
scanf("%d", &Coin[i]);
HeapSort(Coin, n);
for (i = 0, j = n - 1; i != j;) {
if (Coin[i] + Coin[j]>m)
j--;
else if (Coin[i] + Coin[j] < m)
i++;
else break;
}
if (i != j)
printf("%d %d\n", Coin[i], Coin[j]);
else printf("No Solution\n");
return 0;
}
void HeapSort(int a[], int n)
{
int i, temp;
for (i = n / 2 - 1; i >= 0; i--)/* 建立最大堆 */
HeapAdjust(a, i, n - 1);
for (i = n - 1; i>0; i--) {
temp = a[i];
a[i] = a[0];
a[0] = temp;
HeapAdjust(a, 0, i - 1);
}
}
void HeapAdjust(int a[], int i, int n)
{
int X = a[i];
int Parent = i, Child = 2 * Parent + 1;
while (Child <= n) {
if ((Child < n) && (a[Child + 1] > a[Child]))
Child++;
if (a[Child] <= X)
break;
a[Parent] = a[Child];
Parent = Child; Child = 2 * Parent + 1;
}
a[Parent] = X;
}