输入n个整数,输出其中最小的k个



题目描述

输入n个整数,输出其中最小的k个。

详细描述:

接口说明

原型:

bool GetMinK(unsignedint uiInputNum, int * pInputArray, unsignedint uiK, int * pOutputArray);

输入参数:

     unsignedint uiInputNum //输入整数个数

int * pInputArray  //输入整数数组

unsignedint uiK   //需输出uiK个整数

输出参数(指针指向的内存区域保证有效):

    int * pOutputArray //最小的uiK个整数

返回值:

        false 异常失败

          true  输出成功

 

 


输入描述:

输入说明  1 输入两个整数  2 输入一个整数数组



输出描述:

输出一个整数数组


输入例子:
5 2
1 3 5 7 2

输出例子:
1 2
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
bool GetMinK(unsigned int uiInputNum, int *pInputArray, unsigned int uiK, int *pOutputArray)
{
 unsigned int innum = uiInputNum;
 unsigned int outnum = uiK;
 unsigned int i = 0;
  int *p1 = pInputArray;
  int *p2 = pOutputArray;
 if (innum == 0 || p1 == NULL || outnum == 0 || p2 == NULL)
  return false;
 sort(p1, p1 + innum);
 for (i = 0; i<outnum; i++)
 {
  p2[i] = p1[i];
 }
 for (i = 0; i<outnum-1; i++)
  cout << p2[i] << " ";
 cout << p2[i];
 //cout << endl;     //多了此行通不过
 return true;
}
int main()
{
 unsigned int n, k, i;
  int inarray[2000] = { 0 }, outarray[2000] = { 0 };
 while (cin >> n >> k)
 {
  for (i = 0; i<n; i++)
   cin >> inarray[i];
  GetMinK(n, inarray, k, outarray);
 }
 return 0;
}
#include<iostream>
#include<stdlib.h>
#include<algorithm>

using namespace std;
bool GetMinK(unsigned int uiInputNum, int *pInputArray, unsigned int uiK, int *pOutputArray)
{
	unsigned int innum = uiInputNum;
	unsigned int outnum = uiK;
	unsigned int i = 0;
	 int *p1 = pInputArray;
	 int *p2 = pOutputArray;
	if (innum == 0 || p1 == NULL || outnum == 0 || p2 == NULL)
		return false;


	sort(p1, p1 + innum);

	for (i = 0; i<outnum; i++)
	{
		p2[i] = p1[i];
	}

	for (i = 0; i<outnum-1; i++)
		cout << p2[i] << " ";
	cout << p2[i];
	//cout << endl;					//多了此行通不过
	return true;
}
int main()
{
	unsigned int n, k, i;
	 int inarray[2000] = { 0 }, outarray[2000] = { 0 };

	while (cin >> n >> k)
	{
		for (i = 0; i<n; i++)
			cin >> inarray[i];

		GetMinK(n, inarray, k, outarray);

	}

	return 0;
}
#include<iostream>
#include<stdlib.h>
#include<algorithm>

using namespace std;
bool GetMinK(unsigned int uiInputNum, int *pInputArray, unsigned int uiK, int *pOutputArray)
{
	unsigned int innum = uiInputNum;
	unsigned int outnum = uiK;
	unsigned int i = 0;
	 int *p1 = pInputArray;
	 int *p2 = pOutputArray;
	if (innum == 0 || p1 == NULL || outnum == 0 || p2 == NULL)
		return false;


	sort(p1, p1 + innum);

	for (i = 0; i<outnum; i++)
	{
		p2[i] = p1[i];
	}

	for (i = 0; i<outnum-1; i++)
		cout << p2[i] << " ";
	cout << p2[i];
	//cout << endl;					//多了此行通不过
	return true;
}
int main()
{
	unsigned int n, k, i;
	 int inarray[2000] = { 0 }, outarray[2000] = { 0 };

	while (cin >> n >> k)
	{
		for (i = 0; i<n; i++)
			cin >> inarray[i];

		GetMinK(n, inarray, k, outarray);

	}

	return 0;
}

可以使用堆排序来解决这个问题。首先,我们可以定义一个大小为k的最大堆,然后每输入一个整数,就将它与堆顶元素进行比较,如果小于堆顶元素,则将堆顶元素替换为该整数,并重新调整堆,以保证堆中仍然只有k个元素且堆顶元素为最大值。最终,堆中剩余的k个元素就是输入最小的k个整数。 以下是C语言实现的代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_HEAP_SIZE 100 void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } void max_heapify(int arr[], int heap_size, int i) { int largest = i; int left = 2 * i + 1; int right = 2 * i + 2; if (left < heap_size && arr[left] > arr[largest]) { largest = left; } if (right < heap_size && arr[right] > arr[largest]) { largest = right; } if (largest != i) { swap(&arr[i], &arr[largest]); max_heapify(arr, heap_size, largest); } } void build_max_heap(int arr[], int heap_size) { for (int i = heap_size / 2 - 1; i >= 0; i--) { max_heapify(arr, heap_size, i); } } void heap_sort(int arr[], int heap_size) { build_max_heap(arr, heap_size); for (int i = heap_size - 1; i > 0; i--) { swap(&arr[0], &arr[i]); max_heapify(arr, i, 0); } } void print_min_k(int arr[], int n, int k) { int heap[MAX_HEAP_SIZE]; int heap_size = 0; for (int i = 0; i < n; i++) { if (heap_size < k) { heap[heap_size++] = arr[i]; if (heap_size == k) { build_max_heap(heap, heap_size); } } else { if (arr[i] < heap[0]) { heap[0] = arr[i]; max_heapify(heap, heap_size, 0); } } } heap_sort(heap, heap_size); printf("The smallest %d numbers are: ", k); for (int i = 0; i < k; i++) { printf("%d ", heap[i]); } printf("\n"); } int main() { int arr[MAX_HEAP_SIZE]; int n, k; printf("Please enter the number of integers: "); scanf("%d", &n); printf("Please enter the integers: "); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Please enter k: "); scanf("%d", &k); print_min_k(arr, n, k); return 0; } ``` 这个代码的时间复杂度为O(nlogk),其中n为输入整数的个数,k为最小的k个整数的个数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值