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

【华为机试题 HJ58】HJ58 输入n个整数,输出其中最小的k个 输入n个整数,找出其中最小的k个整数并按升序输出本题有多组输入样例 阅读详情

述

 

输入n个整数,找出其中最小的k个整数并按升序输出 

本题有多组输入样例

数据范围:1≤n≤1000 1≤n≤1000  ,输入的整数满足 1≤val≤10000 1≤val≤10000 

输入描述:

第一行输入两个整数n和k
第二行输入一个整数数组

输出描述:

从小到大输出最小的k个整数,用空格分开。

示例1

输入:5 2
1 3 5 7 2

输出:1 2
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;

int sort(vector<int> vec) {
    int len = vec.size();
    for (int i = 0; i < len; i++) {
        for (int k = i + 1; k < len; k++) {
            if (vec[i] > vec[k]) {
                int temp = vec[i];
                vec[i] = vec[k];
                vec[k] = temp;
            }
        }
    }
    return true;
}

int main() {
    int n = 0, k = 0;
    int val = 0;
    vector<int> vec;
    while (cin >> n >> k) {
        // cout<<n<<" "<<k<<endl;
        if (n >= 1 && n <= 1000 ) {
            for (int i = 0; i < n; i++) {
                cin >> val;
                if (val >= 1 && val <= 10000) {
                    vec.push_back(val);
                    //cout<<val<<" ";
                }

            }
            //cout<<"ok!"<<endl;
            sort(vec.begin(), vec.end());
            // cout<<vec.size()<<endl;
            //for(int i=0;i<n;i++)cout<<"vec:"<<vec[i]<<" ";cout<<endl;
            for (int l = 0; l < k; l++)cout << vec[l] << " ";
            cout << endl;
        }

    }

}
// 64 位输出请用 printf("%lld")g++ -o

【牛客网华为机试】HJ58 输入n个整数,输出其中最小的k个 题目 描述 输入一个字符串和一个整数k,截取字符串的前k个字符并输出 本题输入含有多组数据 输入描述: 1.输入待截取的字符串 2.输入一个正整数k,代表截取的长度 输出描述: 截取后的字符串 示例1 输入: abABCcDEF 6 输出: abABCc 示例2 输入: ffIKEHauv 1 bdxPKBhih 6 输出: f bdxPKB 解题思路 根据输入的k直接截取字符串 代码 while True: try: in_s. 阅读详情

相关推荐

PSCAD学习笔记(1)Project Settings(重点snapshot设置)

若不勾选,则横坐标为实际时间2-7s,故障显示在第3s发生。图片中时间坐标轴显示为0-5s,故障时刻显示为第1s,即0-1s展示系统稳态,1-5s展示系统发生故障后的暂态响应,如何通过快照文件实现?勾选该项后,使用快照文件启动仿真会将仿真起始时间强制设置为0s,反之不勾选则依据快照文件保存的时间轴继续(即坐标为实际时间)。如果已有的快照文件保存的是0s至5s的仿真内容,则仿真运行时间从5s开始,到10s结束。如果已有的快照文件保存的是0s至2s的仿真内容,则仿真运行时间从2s开始,到7s结束;

gwenh的博客 7348

华为机试牛客刷题之HJ58 输入n个整数,输出其中最小的k个

第二行输入 n 个整数 a1 ,a2 ,…,an (1≦ai ≦10^4 ) 代表给定的数字。第一行输入两个整数 n,k(1≦n≦1000;对于输入的 n 个整数,升序输出其中最小的 k 个。在一行中升序输出最小的 k 个整数。升序排序后取前k个是对的。

seabirdssss的博客 361

Abaqus CEL分析必备:Hypermesh网格导出与inp文件合并技巧

本文详细介绍了在Abaqus CEL分析中使用Hypermesh生成六面体网格并合并多组件inp文件的完整流程。从单元类型设置(EC3D8R)、网格质量优化到inp文件合并技巧,提供了一套解决流固耦合仿真前处理痛点的实战方案,帮助工程师高效完成复杂CEL分析任务。

weixin_29279101的博客 218

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

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

d3y1`5 81_09 409

汽车LED驱动芯片TPS92682-Q1 SPI接口配置与调试实战指南

SPI(串行外设接口)作为一种高速、全双工的同步串行通信总线,广泛应用于微控制器与外围设备之间的数据交换。其工作原理基于主从架构,通过时钟、数据线和片选信号实现设备间的精准时序控制。在汽车电子和工业控制领域,SPI因其简单高效、易于扩展的特性,成为配置复杂电源管理芯片、传感器和存储器的关键技术。通过SPI接口,工程师能够动态调整设备参数、实时读取状态信息,并实现复杂的故障诊断与保护策略,极大地提升了系统的灵活性与可靠性。本文以汽车级双通道LED恒流驱动器TPS92682-Q1为例,深入解析如何通过SPI接口

weixin_29185493的博客 290

TeraTerm 使用技巧

⏹TeraTerm 使用技巧

菜鸟记录 1万+

华为机试HJ58:输入n个整数,输出其中最小的k个

C++实现——输入n个整数,输出其中最小的k个

翟天保的博客 1万+

手把手教你用VCS+Verdi在Linux下仿真蜂鸟E203 RISC-V核(附波形调试技巧)

本文详细介绍了在Linux环境下使用VCS和Verdi工具链对蜂鸟E203 RISC-V核进行仿真验证的完整流程。从环境配置、工程结构解析到VCS编译优化和Verdi波形分析技巧,提供了实战性强的调试方法和性能优化策略,帮助工程师高效完成数字IC验证工作。

weixin_42531163的博客 418

华为机试HJ58 输入n个整数,输出其中最小的k个

首先看一下题。

bingw0114的博客 511

51单片机 步进电机正反转控制+Proteus仿真

51单片机 步进电机正反转控制+Proteus仿真 Proteus仿真 实例代码 /*************** ******************/ #include <reg52.h> #define uint unsigned int #define uchar unsigned char uchar code FFW[]= { 0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09 }; uchar code REV[]= { 0x09,0x

perseverance51博客 2788

Dify使用教程3:Dify创建工作流

Dify使用教程3:Dify创建工作流 接着,我们连接下一个节点:“LLM”填入上下文变量为:result。这时候知识库就添加成功了。添加新节点:结束节点。

拥抱具身智能,昇腾Ascend C 5129

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

输入n个整数,输出其中最小的k个整数并按升序输出

Caso_卡索的博客 2159

SSD |(二)SSD主控

控制器作为一个片上系统,处理来自用户端的命令并负责管理闪存颗粒。

知识库搭建ing 1966

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

数据范围:1≤n≤1000 1≤n≤1000 ,输入的整数满足 1≤val≤10000 1≤val≤10000。输入 n 个整数,找出其中最小的 k 个整数并按升序输出。从小到大输出最小的 k 个整数,用空格分开。第一行输入两个整数 n 和 k。第二行输入一个整数数组。

???的博客 207

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【牛客网 - 华为机试 - HJ58 输入n个整数,输出其中最小的k个】

思路: step1:首先输入n,k这两个参数(因为input()后是字符串类型,所以需要将n,k转换为int类型,这时我们需要使用list(map())函数); step2:其次,输入一个整数数组num(原理同上,也需要转换为int型,同样需要list(map())函数;) step3:使用sorted()函数,对num整数数组进行排序; step4:使用for循环,遍历“排序后的、且使用切片切割好的”整数数组num; step5:逐个打印,并使用空格分开 代码如下: n,k = lis..

爱摸鱼的经济er的博客 2589

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

【JavaScript——牛客网算法No.HJ58】输入n个整数,输出其中最小的k个(输入格式很坑爹)

@No.HJ9 提取不重复的整数 @problem description: 输入n个整数,输出其中最小的k个 @input description: 输入说明 1 输入两个整数 2 输入一个整数数组 @output description: 输出一个整数数组 示例 @input: 5 2 1 3 5 7 2 @output: 1 2 while(line = readline()){ let arr = line.split(' ') let n = Number(a...

JZevin的博客 684

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

华为机试HJ58输入n个整数,输出其中最小的k个

输出输入中最小的k个元素。

qq_29787929的博客 259

HJ58 输入n个整数,输出其中最小的k个(c++)

#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ int n; vector<int>nums; int a; int k = 0; while(cin >> n){ cin >> k; for(int i = 0; i < n;.

qq_36421001的博客 611
上一篇: HJ55 挑7
下一篇: HJ73 计算日期到天数转换
sherryyang1234
博客等级 码龄10年 13粉丝 118原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值