生產領料、退料頻繁_領料號碼

生產領料、退料頻繁

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.pswp.cn/news/542933.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542933.shtml
英文地址,請注明出處:http://en.pswp.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;它在網絡和互聯網之間形成…