生产领料、退料频繁_领料号码
生產領料、退料頻繁
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, 3Output: 4Example 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 arraym[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 caseIF(it->second>max)max=it->second;END IFELSE IF(it->first+1==(it+1)->first){ //absolute difference is 1IF((it->second +(it+1)->second)>max)max=it->second +(it+1)->second;END IFELSEIF(it->second>max) //absolute difference 0 casemax=it->second;END IFEND IF-ELSEEND FOR6. return max; END FUNCTIONAlgorithm 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, 3Map m:Key Frequency3 14 15 16 28 19 310 1So 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 93+1 //three 9 and one 10Now lets think that we append six 12 to the arrayThus input is now: 6, 4, 6, 8, 5, 9, 9, 9, 10, 3, 12, 12, 12, 12, 12, 12Map m:Key Frequency3 14 15 16 28 19 310 112 6Now the max will be 6 //absolute difference 0 caseSince 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+1if((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: 4Second 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
生產領料、退料頻繁
總結
以上是生活随笔為你收集整理的生产领料、退料频繁_领料号码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ntce服务器不稳定,当心!你的教师资格
- 下一篇: python递归查找_Python程序使