首页 > 其他 > 详细

2017.11.2

时间:2017-11-02 23:46:55      阅读:404      评论:0      收藏:0      [点我收藏+]

题目描述

找出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;
}





2017.11.2

原文:http://www.cnblogs.com/panlangen/p/7775189.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!