Arrays 计算一个数字在随机数组中出现的次数
C函数。 这是我的工作代码。它在给定随机数组大小时显示此输出 输入数组的大小:20 阵列中有什么: 361715131561291127101930606616 3发生2次 6发生4次 15发生2次 6发生3次 12发生2次 6发生2次 然而,我想知道,一旦搜索到某个数字,您将如何着手实现,以避免重复循环Arrays 计算一个数字在随机数组中出现的次数,arrays,c,Arrays,C,C函数。 这是我的工作代码。它在给定随机数组大小时显示此输出 输入数组的大小:20 阵列中有什么: 361715131561291127101930606616 3发生2次 6发生4次 15发生2次 6发生3次 12发生2次 6发生2次 然而,我想知道,一旦搜索到某个数字,您将如何着手实现,以避免重复循环 #include <stdio.h> #include
#include <stdio.h>
#include <stdlib.h>
/* shows duplicate numbers in randomly generated array*/
void display_repeats(int *a, int n){
int i, j;
int count = 0;
for(i = 0; i < n; i++){
for(j = i; j < n; j++){
if(a[i] == a[j]){
count++;
}
}
if(count > 1){
printf("%3d occurs %3d times.", a[i], count);
printf("\n");
}
count = 0;
}
}
int main(void){
int array_size = 0;
int *my_array;
int i = 0;
printf("Enter the size of the array: ");
scanf("%d", &array_size);
/*initialises the array to the appropriate size */
my_array = malloc(array_size * sizeof my_array[0]);
if(NULL == my_array){
fprintf(stderr, "memory allocation failed!\n");
return EXIT_FAILURE;
}
for(i = 0; i < array_size; i++){
my_array[i] = rand() % array_size;
}
printf("What's in the array:\n");
for(i = 0; i < array_size; i++){
printf("%d ", my_array[I]);
}
printf("\n");
display_repeats(my_array, array_size);
/* release the memory associated with the array */
free(my_array);
return EXIT_SUCCESS;
}
#包括
#包括
/*在随机生成的数组中显示重复的数字*/
无效显示_重复(int*a,int n){
int i,j;
整数计数=0;
对于(i=0;i1){
printf(“%3d发生%3d次。”,a[i],计数);
printf(“\n”);
}
计数=0;
}
}
int main(void){
int数组_size=0;
int*my_数组;
int i=0;
printf(“输入数组的大小:”);
scanf(“%d”和数组大小);
/*将数组初始化为适当的大小*/
my_array=malloc(数组大小*my_数组大小[0]);
如果(NULL==my_数组){
fprintf(stderr,“内存分配失败!\n”);
返回退出失败;
}
对于(i=0;i
您可以对数组进行排序并计算每个数字的运行次数。时间复杂度为O(nlog(n)),但如果没有干净的哈希解决方案,它应该是合理的,并且是最简单的方法
顺便说一句,把印刷(a)和逻辑分开是个好主意。将结果作为数据结构返回,并让调用者决定如何处理它。保持逻辑和打印紧密耦合会损害可重用性,并阻止您在应用函数后以编程方式对数据进行操作
这里有一个概念的快速证明。基于上面的提示有很大的改进空间,并考虑在排序之前制作一个int数组的拷贝,如果你把这个函数从<代码>主< <代码> > ,就可以保留函数。
#include <stdio.h>
#include <stdlib.h>
int cmp_ints(const void *a, const void *b) {
return *((const int *)a) - *((const int *)b);
}
int main(void) {
int nums[] = {1, 1, 5, 6, 1, 6, 2, 4, 6, 8};
int len = sizeof nums / sizeof nums[0];
qsort(nums, len, sizeof *nums, cmp_ints);
for (int i = 0; i < len;) {
int count = 1;
int num = nums[i++];
for (; i < len && nums[i] == num; i++, count++);
printf("%d => %d\n", num, count);
}
return 0;
}
ggorlen第一个建议的实现,创建一个数组,用于存储每个索引处的数字是否已被看到
void display_repeats(int *a, int n){
int i, j;
int count = 0;
int seen[n]; //stores 1 if the number at this index has already been seen, 0 if not
for (int i = 0; i<n; i++) seen[i] = 0; //initializes values to 0
for(i = 0; i < n; i++){
if (seen[i]) continue; //skips this iteration of the for loop if this number has already been seen
for(j = i; j < n; j++){
if(a[i] == a[j]){
count++;
seen[j] = 1; //notes that we already seen the number at this index
}
}
if(count > 1){
printf("%3d occurs %3d times.", a[i], count);
printf("\n");
}
count = 0;
}
}
void display_重复(int*a,int n){
int i,j;
整数计数=0;
int seen[n];//如果已看到此索引处的数字,则存储1;如果未看到,则存储0
对于(inti=0;i1){
printf(“%3d发生%3d次。”,a[i],计数);
printf(“\n”);
}
计数=0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* shows duplicate numbers in randomly generated array*/
/* note: order of parameters in function
* so can clearly indicate the array sizing
*/
void display_repeats(int arraySize, int Array[ arraySize ] )
{
int minValue = 0;
int maxValue = 0;
for( int i = 0; i < arraySize; i++ )
{
if( Array[i] > maxValue )
{
maxValue = Array[i];
}
}
for( int j = minValue; j <= maxValue; j++ )
{
int count = 0;
for( int i = 0; i < arraySize; i++)
{
if( Array[i] == j )
{
count++;
}
}
if(count > 1 )
{
printf( "%d occurs %d times.\n", j, count );
}
}
}
int main( void )
{
int array_size = 0;
printf( "Enter the size of the array: " );
if( scanf( "%d", &array_size ) != 1 )
{
fprintf( stderr, "scanf for array size failed\n" );
exit( EXIT_FAILURE );
}
/* use VLA feature of C to declare array */
int my_array[ array_size ];
srand( (unsigned)time(NULL) );
for( int i = 0; i < array_size; i++ )
{
my_array[i] = rand() % array_size;
}
printf( "What's in the array:\n" );
for( int i = 0; i < array_size; i++ )
{
printf( "%d ", my_array[i] );
}
printf( "\n" );
display_repeats( array_size, my_array );
}
Enter the size of the array: 100
What's in the array:
19 69 68 90 25 8 44 64 33 3 28 4 4 43 22 6 19 93 70 63 34 96 42 31 74 9 72 49 34 12 12 53 33 80 95 10 40 39 74 26 94 55 82 98 98 56 56 69 49 78 33 35 75 75 19 1 36 91 50 70 55 63 76 40 95 71 51 88 63 25 14 9 80 48 8 30 4 16 0 5 95 33 93 22 60 12 23 96 3 74 19 58 89 95 50 84 18 1 24 33
1 occurs 2 times.
3 occurs 2 times.
4 occurs 3 times.
8 occurs 2 times.
9 occurs 2 times.
12 occurs 3 times.
19 occurs 4 times.
22 occurs 2 times.
25 occurs 2 times.
33 occurs 5 times.
34 occurs 2 times.
40 occurs 2 times.
49 occurs 2 times.
50 occurs 2 times.
55 occurs 2 times.
56 occurs 2 times.
63 occurs 3 times.
69 occurs 2 times.
70 occurs 2 times.
74 occurs 3 times.
75 occurs 2 times.
80 occurs 2 times.
93 occurs 2 times.
95 occurs 4 times.
96 occurs 2 times.
98 occurs 2 times.