微光拼图 发表于 2021-11-20 22:31:44

输出一个数组中重复次数最少的数

例:
输入:4,1,2,1,2
输出:4

输入:12,12,34,56,56
输出:34

输入:1234,1234,7654,234,234,987
输出:7654,987

人造人 发表于 2021-11-20 23:13:59

main.c
#include "list.h"
#include <stdio.h>
#include <stdbool.h>

typedef struct {
    int number;
    size_t count;
} pair_t;

void store(list_t *list, int value) {
    pair_t pair;
    for(size_t i = 0; i < list_size(list); ++i) {
      list_get(list, i, &pair, sizeof(pair));
      if(pair.number == value) {
            ++pair.count;
            list_set(list, i, &pair, sizeof(pair));
            return;
      }
    }
    pair.number = value;
    pair.count = 1;
    list_append(list, &pair, sizeof(pair));
}

int main(void) {
    list_t *list = list_init();
    while(1) {
      int temp; scanf("%d", &temp);
      store(list, temp);
      int ch = getchar();
      if(ch != ',') break;
    }
    pair_t pair;
    list_t *min = list_init();
    size_t count = 1;
    while(1) {
      bool flag = false;
      for(size_t i = 0; i < list_size(list); ++i) {
            list_get(list, i, &pair, sizeof(pair));
            if(pair.count == count) {
                flag = true;
                list_append(min, &pair, sizeof(pair));
            }
      }
      ++count;
      if(flag) break;
    }
    for(size_t i = 0; i < list_size(min); ++i) {
      list_get(min, i, &pair, sizeof(pair));
      printf("%d: %lu\n", pair.number, pair.count);
    }
    list_deinit(min);
    list_deinit(list);
    return 0;
}


list.h
#ifndef _LIST_H_
#define _LIST_H_

#include <stddef.h>
#include <stdbool.h>

struct list_node_tag {
    void *data; size_t size;
    struct list_node_tag *next;
};

typedef struct {
    struct list_node_tag *head;
    size_t size;
} list_t;

list_t *list_init(void);
void list_deinit(list_t *list);
bool list_clean(list_t *list);
bool list_insert(list_t *list, size_t index, const void *data, size_t size);
bool list_delete(list_t *list, size_t index);
bool list_get(const list_t *list, size_t index, void *data, size_t size);
bool list_set(list_t *list, size_t index, const void *data, size_t size);
bool list_append(list_t *list, const void *data, size_t size);
size_t list_size(const list_t *list);
bool list_empty(const list_t *list);

#endif


list.c
#include "list.h"
#include <stdlib.h>
#include <memory.h>

list_t *list_init(void) {
    list_t *list = malloc(sizeof(*list));
    if(!list) return NULL;
    list->head = NULL;
    list->size = 0;
    return list;
}

void list_deinit(list_t *list) {
    if(!list) return;
    list_clean(list);
    free(list);
}

bool list_clean(list_t *list) {
    if(!list) return false;
    while(!list_empty(list)) list_delete(list, 0);
    return true;
}

bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
    if(!list) return false;
    if(list_size(list) < index) return false;
    if(!data) return false;
    struct list_node_tag **current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *node = malloc(sizeof(*node));
    if(!node) return false;
    node->data = malloc(size);
    if(!node->data) {free(node); return false;}
    memcpy(node->data, data, size);
    node->size = size;
    node->next = *current;
    *current = node;
    ++list->size;
    return true;
}

bool list_delete(list_t *list, size_t index) {
    if(!list) return false;
    if(list_size(list) <= index) return false;
    struct list_node_tag **current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *temp = *current;
    *current = temp->next;
    free(temp->data); free(temp);
    --list->size;
    return true;
}

bool list_get(const list_t *list, size_t index, void *data, size_t size) {
    if(!list) return false;
    if(list_size(list) <= index) return false;
    if(!data) return false;
    struct list_node_tag *const *current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *temp = *current;
    if(temp->size > size) return false;
    memcpy(data, temp->data, temp->size);
    return true;
}

bool list_set(list_t *list, size_t index, const void *data, size_t size) {
    bool res = list_delete(list, index);
    return res ? list_insert(list, index, data, size) : res;
}

bool list_append(list_t *list, const void *data, size_t size) {
    if(!list) return false;
    return list_insert(list, list_size(list), data, size);
}

size_t list_size(const list_t *list) {
    if(!list) return 0;
    return list->size;
}

bool list_empty(const list_t *list) {
    if(!list) return true;
    return list_size(list) == 0;
}


$ gcc-debug -o main main.c list.c
$ ./main
4, 1, 2, 1, 2
4: 1
$ ./main
12, 12, 34, 56, 56
34: 1
$ ./main
1234, 1234, 7654, 234, 234, 987
7654: 1
987: 1
$ ./main
12, 12, 1234, 1234, 1234, 98, 98, 98, 98, 98
12: 2
$
页: [1]
查看完整版本: 输出一个数组中重复次数最少的数