鱼C论坛

 找回密码
 立即注册
查看: 1114|回复: 1

[已解决]输出一个数组中重复次数最少的数

[复制链接]
发表于 2021-11-20 22:31:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
例:
输入: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
  1. #include "list.h"
  2. #include <stdio.h>
  3. #include <stdbool.h>

  4. typedef struct {
  5.     int number;
  6.     size_t count;
  7. } pair_t;

  8. void store(list_t *list, int value) {
  9.     pair_t pair;
  10.     for(size_t i = 0; i < list_size(list); ++i) {
  11.         list_get(list, i, &pair, sizeof(pair));
  12.         if(pair.number == value) {
  13.             ++pair.count;
  14.             list_set(list, i, &pair, sizeof(pair));
  15.             return;
  16.         }
  17.     }
  18.     pair.number = value;
  19.     pair.count = 1;
  20.     list_append(list, &pair, sizeof(pair));
  21. }

  22. int main(void) {
  23.     list_t *list = list_init();
  24.     while(1) {
  25.         int temp; scanf("%d", &temp);
  26.         store(list, temp);
  27.         int ch = getchar();
  28.         if(ch != ',') break;
  29.     }
  30.     pair_t pair;
  31.     list_t *min = list_init();
  32.     size_t count = 1;
  33.     while(1) {
  34.         bool flag = false;
  35.         for(size_t i = 0; i < list_size(list); ++i) {
  36.             list_get(list, i, &pair, sizeof(pair));
  37.             if(pair.count == count) {
  38.                 flag = true;
  39.                 list_append(min, &pair, sizeof(pair));
  40.             }
  41.         }
  42.         ++count;
  43.         if(flag) break;
  44.     }
  45.     for(size_t i = 0; i < list_size(min); ++i) {
  46.         list_get(min, i, &pair, sizeof(pair));
  47.         printf("%d: %lu\n", pair.number, pair.count);
  48.     }
  49.     list_deinit(min);
  50.     list_deinit(list);
  51.     return 0;
  52. }
复制代码


list.h
  1. #ifndef _LIST_H_
  2. #define _LIST_H_

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

  5. struct list_node_tag {
  6.     void *data; size_t size;
  7.     struct list_node_tag *next;
  8. };

  9. typedef struct {
  10.     struct list_node_tag *head;
  11.     size_t size;
  12. } list_t;

  13. list_t *list_init(void);
  14. void list_deinit(list_t *list);
  15. bool list_clean(list_t *list);
  16. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  17. bool list_delete(list_t *list, size_t index);
  18. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  19. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  20. bool list_append(list_t *list, const void *data, size_t size);
  21. size_t list_size(const list_t *list);
  22. bool list_empty(const list_t *list);

  23. #endif
复制代码


list.c
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include <memory.h>

  4. list_t *list_init(void) {
  5.     list_t *list = malloc(sizeof(*list));
  6.     if(!list) return NULL;
  7.     list->head = NULL;
  8.     list->size = 0;
  9.     return list;
  10. }

  11. void list_deinit(list_t *list) {
  12.     if(!list) return;
  13.     list_clean(list);
  14.     free(list);
  15. }

  16. bool list_clean(list_t *list) {
  17.     if(!list) return false;
  18.     while(!list_empty(list)) list_delete(list, 0);
  19.     return true;
  20. }

  21. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  22.     if(!list) return false;
  23.     if(list_size(list) < index) return false;
  24.     if(!data) return false;
  25.     struct list_node_tag **current = &list->head;
  26.     while(index--) current = &(*current)->next;
  27.     struct list_node_tag *node = malloc(sizeof(*node));
  28.     if(!node) return false;
  29.     node->data = malloc(size);
  30.     if(!node->data) {free(node); return false;}
  31.     memcpy(node->data, data, size);
  32.     node->size = size;
  33.     node->next = *current;
  34.     *current = node;
  35.     ++list->size;
  36.     return true;
  37. }

  38. bool list_delete(list_t *list, size_t index) {
  39.     if(!list) return false;
  40.     if(list_size(list) <= index) return false;
  41.     struct list_node_tag **current = &list->head;
  42.     while(index--) current = &(*current)->next;
  43.     struct list_node_tag *temp = *current;
  44.     *current = temp->next;
  45.     free(temp->data); free(temp);
  46.     --list->size;
  47.     return true;
  48. }

  49. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  50.     if(!list) return false;
  51.     if(list_size(list) <= index) return false;
  52.     if(!data) return false;
  53.     struct list_node_tag *const *current = &list->head;
  54.     while(index--) current = &(*current)->next;
  55.     struct list_node_tag *temp = *current;
  56.     if(temp->size > size) return false;
  57.     memcpy(data, temp->data, temp->size);
  58.     return true;
  59. }

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

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

  68. size_t list_size(const list_t *list) {
  69.     if(!list) return 0;
  70.     return list->size;
  71. }

  72. bool list_empty(const list_t *list) {
  73.     if(!list) return true;
  74.     return list_size(list) == 0;
  75. }
复制代码

  1. $ gcc-debug -o main main.c list.c
  2. $ ./main
  3. 4, 1, 2, 1, 2
  4. 4: 1
  5. $ ./main
  6. 12, 12, 34, 56, 56
  7. 34: 1
  8. $ ./main
  9. 1234, 1234, 7654, 234, 234, 987
  10. 7654: 1
  11. 987: 1
  12. $ ./main
  13. 12, 12, 1234, 1234, 1234, 98, 98, 98, 98, 98
  14. 12: 2
  15. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-20 23:13:59 | 显示全部楼层    本楼为最佳答案   
main.c
  1. #include "list.h"
  2. #include <stdio.h>
  3. #include <stdbool.h>

  4. typedef struct {
  5.     int number;
  6.     size_t count;
  7. } pair_t;

  8. void store(list_t *list, int value) {
  9.     pair_t pair;
  10.     for(size_t i = 0; i < list_size(list); ++i) {
  11.         list_get(list, i, &pair, sizeof(pair));
  12.         if(pair.number == value) {
  13.             ++pair.count;
  14.             list_set(list, i, &pair, sizeof(pair));
  15.             return;
  16.         }
  17.     }
  18.     pair.number = value;
  19.     pair.count = 1;
  20.     list_append(list, &pair, sizeof(pair));
  21. }

  22. int main(void) {
  23.     list_t *list = list_init();
  24.     while(1) {
  25.         int temp; scanf("%d", &temp);
  26.         store(list, temp);
  27.         int ch = getchar();
  28.         if(ch != ',') break;
  29.     }
  30.     pair_t pair;
  31.     list_t *min = list_init();
  32.     size_t count = 1;
  33.     while(1) {
  34.         bool flag = false;
  35.         for(size_t i = 0; i < list_size(list); ++i) {
  36.             list_get(list, i, &pair, sizeof(pair));
  37.             if(pair.count == count) {
  38.                 flag = true;
  39.                 list_append(min, &pair, sizeof(pair));
  40.             }
  41.         }
  42.         ++count;
  43.         if(flag) break;
  44.     }
  45.     for(size_t i = 0; i < list_size(min); ++i) {
  46.         list_get(min, i, &pair, sizeof(pair));
  47.         printf("%d: %lu\n", pair.number, pair.count);
  48.     }
  49.     list_deinit(min);
  50.     list_deinit(list);
  51.     return 0;
  52. }
复制代码


list.h
  1. #ifndef _LIST_H_
  2. #define _LIST_H_

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

  5. struct list_node_tag {
  6.     void *data; size_t size;
  7.     struct list_node_tag *next;
  8. };

  9. typedef struct {
  10.     struct list_node_tag *head;
  11.     size_t size;
  12. } list_t;

  13. list_t *list_init(void);
  14. void list_deinit(list_t *list);
  15. bool list_clean(list_t *list);
  16. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  17. bool list_delete(list_t *list, size_t index);
  18. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  19. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  20. bool list_append(list_t *list, const void *data, size_t size);
  21. size_t list_size(const list_t *list);
  22. bool list_empty(const list_t *list);

  23. #endif
复制代码


list.c
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include <memory.h>

  4. list_t *list_init(void) {
  5.     list_t *list = malloc(sizeof(*list));
  6.     if(!list) return NULL;
  7.     list->head = NULL;
  8.     list->size = 0;
  9.     return list;
  10. }

  11. void list_deinit(list_t *list) {
  12.     if(!list) return;
  13.     list_clean(list);
  14.     free(list);
  15. }

  16. bool list_clean(list_t *list) {
  17.     if(!list) return false;
  18.     while(!list_empty(list)) list_delete(list, 0);
  19.     return true;
  20. }

  21. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  22.     if(!list) return false;
  23.     if(list_size(list) < index) return false;
  24.     if(!data) return false;
  25.     struct list_node_tag **current = &list->head;
  26.     while(index--) current = &(*current)->next;
  27.     struct list_node_tag *node = malloc(sizeof(*node));
  28.     if(!node) return false;
  29.     node->data = malloc(size);
  30.     if(!node->data) {free(node); return false;}
  31.     memcpy(node->data, data, size);
  32.     node->size = size;
  33.     node->next = *current;
  34.     *current = node;
  35.     ++list->size;
  36.     return true;
  37. }

  38. bool list_delete(list_t *list, size_t index) {
  39.     if(!list) return false;
  40.     if(list_size(list) <= index) return false;
  41.     struct list_node_tag **current = &list->head;
  42.     while(index--) current = &(*current)->next;
  43.     struct list_node_tag *temp = *current;
  44.     *current = temp->next;
  45.     free(temp->data); free(temp);
  46.     --list->size;
  47.     return true;
  48. }

  49. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  50.     if(!list) return false;
  51.     if(list_size(list) <= index) return false;
  52.     if(!data) return false;
  53.     struct list_node_tag *const *current = &list->head;
  54.     while(index--) current = &(*current)->next;
  55.     struct list_node_tag *temp = *current;
  56.     if(temp->size > size) return false;
  57.     memcpy(data, temp->data, temp->size);
  58.     return true;
  59. }

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

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

  68. size_t list_size(const list_t *list) {
  69.     if(!list) return 0;
  70.     return list->size;
  71. }

  72. bool list_empty(const list_t *list) {
  73.     if(!list) return true;
  74.     return list_size(list) == 0;
  75. }
复制代码

  1. $ gcc-debug -o main main.c list.c
  2. $ ./main
  3. 4, 1, 2, 1, 2
  4. 4: 1
  5. $ ./main
  6. 12, 12, 34, 56, 56
  7. 34: 1
  8. $ ./main
  9. 1234, 1234, 7654, 234, 234, 987
  10. 7654: 1
  11. 987: 1
  12. $ ./main
  13. 12, 12, 1234, 1234, 1234, 98, 98, 98, 98, 98
  14. 12: 2
  15. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 21:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表