2017.11.2

题目描述

找出n个数里最小的k个

输入描述:

每个测试输入包含空格分割的n+1个整数,最后一个整数为k值,n
不超过100。

输出描述:

输出n个整数里最小的k个数。升序输出
示例1

输入

3 9 6 8 -10 7 -11 19 30 12 23 5

输出

-11 -10 3 6 7


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <iomanip> //不能写成#include <iomanip.h>
#include <string.h>


using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main()
{
int temp,temp1,temp2;
int i=0;

int array[101];

while(cin>>temp)
{
if(temp==EOF)break;
array[i]=temp;
i++;
}
temp2=array[i-1];
for(int j=1;j<i-1;j++)
{
temp1=array[j];
int k=j-1;
while(k>=0&&array[k]>temp1)
{
array[k+1]=array[k];
k--;
}
array[k+1]=temp1;
}
for(int j=0;j<temp2;j++)
{
cout<<setw(4)<<array[j];
}
return 0;
}





原文地址:https://www.cnblogs.com/panlangen/p/7775189.html