生产领料、退料频繁_领料号码

生产领料、退料频繁

Problem statement:

问题陈述:

Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to 1.

给定一个整数数组,查找并打印可以从数组中选择的最大整数数,以使任意两个选定整数之间的绝对差小于或等于1。

Examples:

例子:

    Input array:
6, 4, 6, 8, 5, 9, 9, 9, 10, 3
Output: 
4

Example with explanation:

带有说明的示例:

The input array is: 6, 4, 6, 8, 5, 9, 9, 9, 10, 3

输入数组为:6,4,6,8,5,9,9,9,10,3

No we can pick up different sets for which the absolute difference between any two numbers in the set is 1.

不,我们不能选择集合中任意两个数字的绝对差为1的不同集合。

Possible sets are:

可能的设置有:

    {6, 6, 5}
{4, 5}
{8, 9, 9, 9}
{9, 9, 9, 10}
{4, 3}

Thus maximum no of picked up elements is: 4

因此,拾取元素的最大数量为: 4

Algorithm:

算法:

This problem can be implemented with help of map data structure. We have used STL for map implementation. (For details regarding STL map, C++ STL Map)

可以借助地图数据结构来实现此问题。 我们已经使用STL来实现地图。 (有关STL映射, C ++ STL映射的详细信息)

FUNCTION pickingNumbers(input array)
1.  Declare   map<int,int>m to store key with their frequencies;
2.  Build the map.
For  i=0:length of array
m[array[i]]++;
3.  Declare max as INT_MIN;
4.  Declare map<int,int>::iteratorit;
5.  For(it=m.begin();it!=m.end();it++)
IF (it+1==m.end()) //last case
IF(it->second>max)
max=it->second;
END IF
ELSE IF(it->first+1==(it+1)->first){ //absolute difference is 1
IF((it->second +(it+1)->second)>max)
max=it->second +(it+1)->second;
END IF
ELSE
IF(it->second>max) //absolute difference 0 case
max=it->second;
END IF
END IF-ELSE
END FOR
6.  return max;
END FUNCTION

Algorithm is pretty simple.

算法非常简单。

We first extract the unique numbers and store their frequencies. Then we simply check for two unique number's additive frequency or any one unique number's frequency itself and return the greater one.

我们首先提取唯一数字并存储其频率。 然后,我们只需检查两个唯一数字的加法频率或任何一个唯一数字的频率本身,然后返回较大的一个。

Let's solve the above example.

让我们解决以上示例。

    The input array is: 6, 4, 6, 8, 5, 9, 9, 9, 10, 3
Map m:
Key 			Frequency
3				1
4				1
5				1
6				2
8				1
9				3
10				1
So if we do all the iterations then each iteration, 
maxgets to be updated(or not, keeps last value)
From this map, we can see max is 4 
1+3 //one 8 and three 9
3+1 //three 9 and one 10
Now lets think that we append six 12 to the array
Thus input is now: 6, 4, 6, 8, 5, 9, 9, 9, 10, 3, 12, 12, 12, 12, 12, 12
Map m:
Key 			Frequency
3				1
4				1
5				1
6				2
8				1
9				3
10				1
12				6
Now the max will be 6 //absolute difference 0 case
Since the subset will be {12, 12, 12, 12, 12, 12}

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int pickingNumbers(vector<int> a) 
{
map<int,int> m;
for(int i=0;i<a.size();i++)
m[a[i]]++;
int max=INT_MIN;
map<int,int>::iterator it;
for(it=m.begin();it!=m.end();it++){
//std::next(it) points to it+1
if((std::next(it))==m.end()){
if(it->second>max)
max=it->second;
}
else if(it->first+1==(std::next(it))->first){
if((it->second +(std::next(it))->second)>max)
max=it->second +(std::next(it))->second;
}
else{
if(it->second>max)
max=it->second;
}
}
return max;
}
int main(){
int n,item;
cout<<"Enter number of elements in the array\n";
cin>>n;
vector<int> a;
cout<<"enter numbers\n";
for(int i=0;i<n;i++){
cin>>item;
a.push_back(item);
}
cout<<"Maximum no of such numbers can be picked: "<<pickingNumbers(a)<<endl;
return 0;
}

Output

输出量

First run:
Enter number of elements in the array
10
enter numbers
6 4 6 8 5 9 9 9 10 3
Maximum no of such numbers can be picked: 4
Second run:
Enter number of elements in the array
16
enter numbers
6 4 6 8 5 9 9 9 10 3 12 12 12 12 12 12
Maximum no of such numbers can be picked: 6

翻译自: https://www.includehelp.com/icp/picking-numbers.aspx

生产领料、退料频繁

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542933.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

iOS设备、Icon、LaunchImage、图片分辨率

iOS设备 iOS设备的屏幕的大小、分辨率以及比例因数&#xff08;Scale Factor&#xff09;[1]。 iPhone 设备宽(inch)高(inch)对角线(inch)逻辑分辨率(point)Scale Factor设备分辨率(pixel)PPI3GS2.44.53.5320X4801x320X4801634/4s2.314.53.5320X4802x640X9603265c2.334.904320X…

计算机应用基础2010版知识点,2010计算机应用基础选择题(含答案版)重点.doc

2010计算机应用基础选择题(含答案版)重点第1部分1、C根据计算机使用的电信号来分类&#xff0c;电子计算机分为数字计算机和模拟计算机&#xff0c;其中&#xff0c;数字计算机是以( )为处理对象。&#xff21;&#xff0e;字符数字量 &#xff22;&#xff0e;物理量 &#…

mysql如何植入到oracle_分享MSSQL、MySql、Oracle的大数据批量导入方法及编程手法细节...

1&#xff1a;MSSQLSQL语法篇&#xff1a;BULK INSERT[ database_name . [ schema_name ] . | schema_name . ] [ table_name | view_name ]FROM data_file[ WITH([ [ , ] BATCHSIZE batch_size ][ [ , ] CHECK_CONSTRAINTS ][ [ , ] CODEPAGE { ACP | OEM | RAW | code_page…

Java文件类String [] list(FilenameFilter fnf)方法,带示例

File Class String []列表(FilenameFilter fnf) (File Class String[] list(FilenameFilter fnf)) This method is available in package java.io.File.list(FilenameFilter fnf). 软件包java.io.File.list(FilenameFilter fnf)中提供了此方法。 This method is used to return…

求最大公因数

while 1:s input(请输入一个数&#xff1a;)e input(请输入一个数&#xff1a;)s int(s)e int(e)if s 0 or e 0:print(错误)continueif s > e:f eelse:f swhile f:if s % f 0 and e % f 0:print(f)breakelse:f f - 1 转载于:https://www.cnblogs.com/wumac/p/567…

窦学计算机基础期末考试,关于新生开学考计算机基础

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼单选题练习1&#xff0e;完整的计算机系统由( c )组成。A&#xff0e;运算器、控制器、存储器、输入设备和输出设备B&#xff0e;主机和外部设备C&#xff0e;硬件系统和软件系统D&#xff0e;主机箱、显示器、键盘、鼠标、打印机…

AIX配置Volumn

我们知道&#xff0c;现在操作系统都具有默认的卷管理系统来管理磁盘。详见存储技术之卷管理和文件系统。总体来说&#xff0c;从下向上分为物理磁盘(PV)、逻辑卷组(VG)、逻辑卷(LV)&#xff0c;用户可以直接mount的是逻辑卷。本文记录一些AIX下的卷管理和配置方法。 AIX下的Vo…

高并发内存占用持续下降_师兄,为什么删除数据后,Redis内存占用依然很高?...

前言上周刚来了个应届小师弟&#xff0c;组长说让我带着&#xff0c;周二问了我这样一个问题&#xff1a;师兄啊&#xff0c;我用top命令看了下服务器的内存占用情况&#xff0c;发现Redis内存占用严重&#xff0c;于是我就删除了大部分不用的keys&#xff0c;为什么内存占用还…

如何打印出给定尺寸的方格_打印给定号码的表格| 8085微处理器

如何打印出给定尺寸的方格Problem statement: 问题陈述&#xff1a; Write an assembly language program in 8085 to print the table of input integer. 在8085中编写汇编语言程序以打印输入整数表。 Assumptions: Suppose the inputted number is at memory location 2050…

maka如何看html文件,自己在MAKA上做得H5,别人如何能看到收集的信息

1。登陆1。 ? ?登陆入口&#xff1a;点击首页右上角“登录”按钮进入登录界面&#xff1b;2。 ? ?登陆界面&#xff1a;输入有效注册的个人账号信息&#xff1a;邮箱、密码&#xff1b;您也可以选择QQ等第三方登录。3。 ? ?密码找回&#xff1a;进入账户登录界面&#xf…

发现保存GIF格式后相素发生变化咋办

数学公式编辑器MathType主要的作用就是编辑公式用的&#xff0c;一些用户朋友编辑完公式希望把公式保存为“高分辨率”的GIF格式&#xff0c;但是在图片查看器中进行浏览查看时发现GIF的分辨率发生了变化&#xff0c;对于这种情况该如何处理呢&#xff1f;下面我们就针对这个问…

3个阶段 项目征名_2020年即将上线的3个爆款,或许它们能改变现有的手游格局...

在近几年国内的手游市场中&#xff0c;基本都被《王者荣耀》和吃鸡类型的给垄断了&#xff0c;偶尔有个别爆款出现&#xff0c;也只是昙花一现&#xff0c;连半年时间都坚持不到&#xff0c;就比如去年的自走棋。不过在2020年&#xff0c;以王者和吃鸡为主的这种格局或许会被打…

python判断素数程序_使用面向对象方法检查素数的Python程序

python判断素数程序This program will check whether a given number is Prime or Not, in this program we will divide the number from 2 to square root of that number, if the number is divided by any number in b/w then the number will not be a prime number. 该程…

湖北计算机技能高考专科学校排名,湖北2021年技能高考专科录取分数线

https://forms.ebdan.net/ls/wg2YPHOQ点击查看全部院校武汉船舶职业技术学院&#xff1a;技能高考(机械类)507技能高考(电气电子类)437技能高考(计算机类)532技能高考(财经类)530技能高考(建筑设计类)319技能高考(旅游类)489技能高考(汽车维修类)466湖北科技职业学院&#xff1…

定位样式

Web页面中的特殊效果&#xff0c;如菜单效果&#xff0c;对话框效果都需要通过定位属性来实现。定位样式position属性可以控制元素的定位类型position属性值可以为sataic、fixed、absolute、relativeposition属性的语法结构- position:value;定位属性static默认值。没有定位&am…

c#异常处理_C#异常处理能力问题和解答 套装2

c#异常处理1) There are the following statements that are given below, which is correct about an exception in C#.NET? The exception occurs at the time of compilationThe exception occurs during program loadingThe exception occurs during JIT compilationThe …

考虑题4所示的日志记录_[南开大学]18秋学期(1703)《数据库基础与应用》在线作业...

18秋学期(1703)《数据库基础与应用》在线作业一、单选题&#xff1a;1.[单选题]在SQL语言中&#xff0c;模式对应于() (满分:)A. 视图和部分基本表B. 基本表C. 存储文件D. 物理磁盘正确答案:——B——2.[单选题]在数据库系统中&#xff0c;读脏数据是指一个事务读了另…

数字图像处理图像反转的实现_反转8位数字| 8085微处理器

数字图像处理图像反转的实现Problem statement: 问题陈述&#xff1a; To reverse 8 bits number using 8085 microprocessors. 使用8085微处理器反转8位数字。 Algorithm: 算法&#xff1a; Load the accumulator with the first data. 向累加器加载第一个数据。 Use RLC i…

计算机控制z反变换公式,第三章 计算机控制系统的数学描述(修正Z变换).ppt

第三章 计算机控制系统的数学描述(修正Z变换)* 3.4 修改Z变换 1&#xff0e;具有多采样频率系统 在某些控制系统中&#xff0c;存在着不同采样频率的采样开关&#xff0c;如图3.10所示。 图3.10 具有不同采样频率系统结构图 图3.10表示&#xff0c;该系统反馈回路的采样频率高一…

7月19日实习日志

今天是实习第十二天&#xff0c;时间过得很快一转眼实习一般都已经过去了&#xff0c;今天早上下了大雨&#xff0c;到单位的时候差一点迟到。 今天难道单位公司的同事就带领着我给公司的防火请升级&#xff0c;防火墙可以是一套硬件或软件&#xff0c;它在网络和互联网之间形成…