int数组中最不频繁的公共数
least frequent common number from a int array
我必须从int数组中找到最小公共数,我已经写了代码,但它不能正常工作,
这是我的逻辑,1.对数组进行排序2.更新最小公共计数器3.获取所有是否都是唯一的
以及下面的代码,
static int min_loc ; //minimum value location
static int min_cnt ;
int all_uniqFlag = true;
void leastCommon(int data[],int n)
{
int rcount = 0; //Repeated number counter
int mcount = n; // minimum repetetion counter;
// The array is already sorted we need to only find the least common value.
for(int i = 0 ; i < n-1 ; i++)
{
//Case A : 1 1 2 2 2 3 3 3 3 4 5 5 5 5 : result should be 4
//Case B : 1 2 3 4 5 6 7 (All unique number and common values so all values should be printed
// and )
//Case C : 1 1 2 2 3 3 4 4 (all numbers have same frequency so need to display all )
cout << "data[i] : " << data[i] << " data[i+1] : " << data[i+1] << "i = " << i << endl;
if(data[i] != data[i+1])
{
//mcount = 0;
//min_loc = i;
//return;
}
if(data[i] == data[i+1])
{
all_uniqFlag = false;
rcount++;
}
else if(rcount < mcount)
{
mcount = rcount;
min_loc = i ;//data[i];
}
}
min_cnt = mcount;
}
正如评论中提到的,只有案例B有效,案例A和C无效,你能帮我解决这个问题吗?
- 浏览列表
- 将列表中的每个元素与
out
数组中的最后一个元素进行比较 - 如果元素匹配,则将其计数递增1
- 如果元素不匹配,则将新元素添加到
out
中数组并将index
递增1
一旦扫描完成,out
阵列将具有所有不同的元件out[][0]
及其频率out[][1]
- 扫描频率列表(
out[][1]
)以查找最低频率 - 最后对元素列表
out[][0]
进行另一次扫描,并打印频率与最低频率匹配的元素
#include<stdio.h>
#include<stdlib.h>
#define N 8
int main()
{
//int data[N]={1,2,3,4,5,6,7};
int data[N]={1,1,2,2,3,3,4,4};
//int data[N]={1,1,2,2,2,3,3,3,3,4,5,5,5,5};
int out[N][2];
int i=0,index=0;
for(i=0;i<N;i++)
{
out[i][0]=0;
out[i][1]=0;
}
out[0][0] = data[0];
out[0][1]=1;
for(i=1;i<N;i++)
{
if(data[i] != out[index][0])
{
index++;
out[index][0] = data[i];
out[index][1] = 1;
}
else
{
out[index][1]++;
}
}
int min=65536;
for(i=0;i<N;i++)
{
if(out[i][1] == 0)
{
break;
}
if(out[i][1] < min)
{
min = out[i][1];
}
}
for(i=0;i<N;i++)
{
if(out[i][1] == min)
{
printf("%dt",out[i][0]);
}
}
printf("n");
}
您可以使用map
进行以下操作:
#include <string>
#include <map>
#include <iostream>
typedef std::map<int, int> Counter;
void leastCommon(int data[],int n) {
Counter counter;
int min = n;
for (int i = 0; i < n; i++)
counter[data[i]]++;
for (Counter::iterator it = counter.begin(); it != counter.end(); it++) {
if (min > it->second) min = it->second;
}
for (int i = 0; i < n; i++) {
if (counter[data[i]] == min) {
std::cout << data[i] << std::endl;
counter[data[i]]++;
}
}
}
int main() {
int data[] = {1, 1,3,4,4,2,4,3,2};
leastCommon(data, 9);
return 0;
}
方法是-
- 从排序后的数组中选择第一个元素,当它的连续元素相同时,将它们存储在输出[]中,直到循环中断
- 将元素的频率存储在最小频率中
- 选择下一个元素,检查其连续的元素,并将它们存储在相同的输出[]中,直到循环中断
- 用最小频率检查频率
- 如果相同,则不执行任何操作(将它们添加到输出[]中)
- 如果小于,则清除output[]并存储元素相同次数
- 如果更多,则在迭代该元素之前,将有效输出[]长度更改为以前的长度
-
类似地对所有不同的元素进行迭代,并最终从输出[]获得从0到有效长度的结果
void leastCommon(int data[], int len) { if ( len > 0) { int output[] = new int[len]; int outlen = 0; // stores the size of useful-output array int leastFrequency = len; // stores the lowest frequency of elements int i=0; int now = data[i]; while (i < len) { int num = now; int count = 0; do { output[outlen] = now; outlen++; count++; if((++i == len)){ break; } now = data[i]; } while (num == now); // while now and next are same it adds them to output[] if (i - count == 0) { // avoids copy of same values to output[] for 1st iteration leastFrequency = count; } else if (count < leastFrequency) { // if count for the element is less than the current minimum then re-creates the output[] leastFrequency = count; output = new int[len]; outlen = 0; for (; outlen < leastFrequency; outlen++) { output[outlen] = num; // populates the output[] with lower frequent element, to its count } } else if (count > leastFrequency) { outlen -= count; // marks outlen to its same frequent numbers, i.e., discarding higher frequency values from output[] } } //for(int j = 0; j < outlen; j++) { // print output[] to console //} } }
Plz建议改进。
相关文章:
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 按字符值访问int数组
- 将 int 数组转换为 std::vector<int*>
- 比较if语句中的数组值和int值
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 在c++中获取两个大int,并将它们存储在数组中
- 声明数组>> int arr[] 时出错;在C++(虽然在 Java 中有效)?
- 有没有办法用 2D 数组 (int) 重载构造函数?
- 当大小未知时,如何迭代指向数组 (int*) 的指针?
- 接受来自函数的二维数组(int 类型)并将其返回到主函数
- 数组 + int 作为一个参数做什么
- 如何制作'bit'数组而不是数组'int'?
- 动态2d数组:int(*ptr)[4]=new int[7][4];
- 为[z][y][x]int定义数组int[x][y][z]有什么好处?
- 用Visual c++编写二维数组int[n][m]到HDF5文件
- 数组int未正确返回
- 将元素推送到动态分配的指针数组int C++
- 如何在另一个数组中插入多维数组(int而非char)
- 将数组(int*)设置为NULL是否释放其占用的内存
- C++如何将数组 int[10] push_back std::vector<int[10]>?