c数组问题
我有一个像这样的数组:
int sayilar[10] = {5,6,2,1,4,2,5,5,5,2};
现在我想得到这样的结果:找到 2 个重复项,5= 4 次,2= 3 次。
不知道该怎么做。这是我的代码,它不能正常工作:
#include <stdio.h>
int main()
{
int sayilar[10]={5,6,2,1,4,2,5,5,5,2};
int i,j;
int matris[5][2];
int ar[5];
int temp=0;
int tempX;
int k=0;
for(i=0; i<10; i++)
{
for(j=i+1; j<10; j++)
{
if(sayilar[j]==sayilar[i])
{
if(temp==0)
{
matris[k][0] = sayilar[j];
matris[k][1] = 1;
temp=1;
} else
{
matris[k][1]++;
}
}
}
if(temp!=0)
k++;
temp=0;
}
printf("%d %d",matris[0][0],matris[0][1]+1);
}
I have an array like this:
int sayilar[10] = {5,6,2,1,4,2,5,5,5,2};
now I want to get a result like: 2 duplicates found, 5= 4 times, 2= 3times.
Couldn't figure how to do that. Here is my code, that's not working right:
#include <stdio.h>
int main()
{
int sayilar[10]={5,6,2,1,4,2,5,5,5,2};
int i,j;
int matris[5][2];
int ar[5];
int temp=0;
int tempX;
int k=0;
for(i=0; i<10; i++)
{
for(j=i+1; j<10; j++)
{
if(sayilar[j]==sayilar[i])
{
if(temp==0)
{
matris[k][0] = sayilar[j];
matris[k][1] = 1;
temp=1;
} else
{
matris[k][1]++;
}
}
}
if(temp!=0)
k++;
temp=0;
}
printf("%d %d",matris[0][0],matris[0][1]+1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你知道数组中的数字吗?
例如,如果您知道它们都在 1 到 10 之间,那么您可以创建一个大小为 10 的数组,其中包含每个数字的计数。
像这样的东西(未经测试)
如果您对 sayilar 中可能包含哪些值没有任何保证,那么首先排序可能是最好的选择,正如其他人提到的那样。查看 qsort,它将就地对数组进行排序。
Do you know anything about the numbers in the array?
If you know, for instance, that they will all be between 1 and 10, then you could just create an array of size 10 which would contain the count for each number.
Something like this (not tested)
If you don't have any guarantees about what values might be in
sayilar
, then sorting first is probably the best option, as others have mentioned. Check out qsort, which will sort your array in-place.我认为您应该在执行嵌套循环之前检查
sayilar[i]
不在matris
中。如果您想要更高级的解决方案,您可以对数组进行排序,其复杂度为 O(nlogn),而不仅仅是简单地迭代排序后的数组...只是为了获得灵感。
I think you should check that
sayilar[i]
is not inmatris
before doing the nested loop.If you want more advanced solution you can sort the array, which has O(nlogn) complexity, and than just simply iterate thru the sorted array... Just for inspiration.
理想情况下,您希望一次性完成此操作,而不是使用嵌套循环。最好的方法是使用某种映射结构,其中映射键是您正在计数的值,映射值是该值出现的次数。
对于这个特定的示例,最简单的方法是创建一个一维数组,其中索引对应于您正在计算的值。示例:
因此,当此循环执行时,
counts
会更新如下:不幸的是,如果您要跟踪非常广泛的值或非整数的值,则此方法的扩展性不太好。
C++ 和 Java 等语言提供了内置的地图数据类型,该数据类型通常构建在某种平衡树结构(例如红黑树)之上,对于这个特定问题来说,这超出了杀伤力。
Ideally, you'd like to do this in one pass, rather than using the nested loops. The best way is to use some sort of mapping structure, where the map key is the value you're counting, and the map value is the number of occurences for that value.
For this specific example, the easiest thing to do would be to create a single-dimensioned array, where the index corresponds to the value you're counting. Example:
So as this loop executes,
counts
gets updated as follows:Unfortunately, this approach doesn't scale very well if you're tracking a very wide range of values, or values that aren't integers.
Languages like C++ and Java provide a built-in map data type which is usually built on top of some sort of balanced tree structure like a Red-Black tree, which is beyond overkill for this particular problem.
如果我来写,我会分两个阶段来写。
首先,我将循环遍历数组,并计算每个数组的实例数:
接下来,我将循环并查找重复项:
这种方法使其更具可读性。
If I were writing it, I'd do it in two stages.
First, I'd loop over the array, and count instances of each:
Next, I would loop over and look for duplicates:
This approach leaves it much more readable.