鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: Loser_YiMo

用C语言写出如下条件程序

[复制链接]
发表于 2021-5-12 13:42:32 | 显示全部楼层

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-12 21:35:13 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-12 23:25:28 | 显示全部楼层
本帖最后由 yuxijian2020 于 2021-5-12 23:33 编辑

工具人报到
太晚了,就弄了第一个
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. //从 pos 开始往左 找到第一个 0值 在数组中的位置距离当前位置的距离
  5. int LeftFindZero(int* box, int num, int pos)
  6. {
  7.         int cur = (pos - 1) < 0 ? num - 1 : pos - 1;
  8.         int distance = 1;

  9.         while (box[cur] != 0)
  10.         {
  11.                 distance++;
  12.                 cur = (cur - 1) < 0 ? num - 1 : cur - 1;
  13.         }

  14.         return distance;
  15. }
  16. //从 pos 开始往右 找到第一个 0值 在数组中的位置距离当前位置的距离
  17. int RightFindZero(int* box, int num, int pos)
  18. {
  19.         int cur = (pos + 1) < num ? pos + 1 : 0;
  20.         int distance = 1;

  21.         while (box[cur] != 0)
  22.         {
  23.                 distance++;
  24.                 cur = (cur + 1) < num ? cur + 1 : 0;
  25.         }

  26.         return distance;
  27. }
  28. //优先向左边填充
  29. int LeftFindNearestZeroAndSwap(int* box, int num, int pos)
  30. {
  31.         int leftZero = LeftFindZero(box, num, pos);
  32.         int rightZero = RightFindZero(box, num, pos);
  33.         int cur = 0;

  34.         if (leftZero > rightZero)
  35.         {
  36.                 cur = rightZero + pos >= num ? (rightZero + pos - num) : rightZero + pos;
  37.                 box[cur]++;
  38.                 box[pos]--;
  39.                 return rightZero;
  40.         }
  41.         else
  42.         {
  43.                 cur = pos - leftZero < 0 ? (pos + num - leftZero) : pos - leftZero;
  44.                 box[cur]++;
  45.                 box[pos]--;
  46.                 return leftZero;
  47.         }
  48. }
  49. //优先向右边填充
  50. int RightFindNearestZeroAndSwap(int* box, int num, int pos)
  51. {
  52.         int leftZero = LeftFindZero(box, num, pos);
  53.         int rightZero = RightFindZero(box, num, pos);
  54.         int cur = 0;

  55.         if (rightZero > leftZero)
  56.         {
  57.                 cur = pos - leftZero < 0 ? (pos + num - leftZero) : pos - leftZero;
  58.                 box[cur]++;
  59.                 box[pos]--;
  60.                 return leftZero;
  61.         }
  62.         else
  63.         {
  64.                 cur = rightZero + pos >= num ? (rightZero + pos - num) : rightZero + pos;
  65.                 box[cur]++;
  66.                 box[pos]--;
  67.                 return rightZero;
  68.         }
  69. }
  70. //分发
  71. int HandOut(int* box, int num)
  72. {
  73.         if (num <= 0) return 0;

  74.         int left                = 0;                //左边
  75.         int right                = 0;                //右边
  76.         int leftCount        = 0;                //优先往左移动
  77.         int rightCount        = 0;                //优先往右移动
  78.         int count                = 0;                //返回值
  79.         int* leftBox        = (int*)malloc(sizeof(int) * num);        //临时数组
  80.         int* rightBox        = (int*)malloc(sizeof(int) * num);        //临时数组
  81.         if (leftBox == NULL || rightBox == NULL) return 0;

  82.         memset(leftBox, 0, sizeof(int) * num);
  83.         memset(rightBox, 0, sizeof(int) * num);

  84.         for (int i = 0; i < num; i++)
  85.         {
  86.                 leftBox[i]        = box[i];
  87.                 rightBox[i] = box[i];
  88.         }

  89.         for (int i = 0; i < num; i++)
  90.         {
  91.                 while (leftBox[i] > 1)
  92.                         leftCount += LeftFindNearestZeroAndSwap(leftBox, num, i);
  93.         }

  94.         for (int j = num - 1; j >= 0; j--)
  95.         {
  96.                 while (rightBox[j] > 1)
  97.                         rightCount += RightFindNearestZeroAndSwap(rightBox, num, j);
  98.         }

  99.         free(leftBox);
  100.         free(rightBox);

  101.         count = leftCount > rightCount ? rightCount : leftCount;
  102.         return count;
  103. }

  104. void OperatorCount()
  105. {
  106.         int t = 51;                //数据组数
  107.         int N = 501;        //盒子数量 和 糖果的最大数量
  108.         int** boxs;                //盒子
  109.         int* result;        //结果
  110.         //获取用户输入
  111.         while (t < 0 || t > 50)
  112.         {
  113.                 printf_s("本次输入几组数据?(0 <= t <= 50)\n");
  114.                 scanf_s("%d%*c", &t);
  115.                 if (t < 0 || t > 50)
  116.                         printf_s("输入错误,请重新输入 t\n");
  117.         }

  118.         while (N < 1 || N > 500)
  119.         {
  120.                 printf_s("共有几个盒子?(1 <= N <= 500)\n");
  121.                 scanf_s("%d%*c", &N);
  122.                 if (N < 1 || N > 500)
  123.                         printf_s("盒子数量错误!请重新输入!\n");
  124.         }
  125.         //申请空间
  126.         boxs = (int**)malloc(sizeof(int*) * t);
  127.         if (boxs == NULL) return;                                        //判断是否申请成功
  128.         for (int i = 0; i < t; i++)
  129.         {
  130.                 boxs[i] = (int*)malloc(sizeof(int) * N);
  131.                 if (boxs[i] == NULL) return;
  132.                 memset(boxs[i], 0, sizeof(int) * N);
  133.         }

  134.         result = (int*)malloc(sizeof(int) * t);
  135.         if (result == NULL) return;
  136.         memset(result, 0, sizeof(int) * t);

  137.         for (int i = 0; i < t; i++)
  138.         {
  139.                 for (int j = 0; j < N; j++)
  140.                 {
  141.                         printf_s("请输入第 %d 组 第 %d 个盒子中的糖果数量:", i, j);
  142.                         scanf_s("%d", &boxs[i][j]);
  143.                 }
  144.                 result[i] = HandOut(boxs[i], N);
  145.         }

  146.         for (int i = 0; i < t; i++)
  147.                 printf_s("第 %d 组数据需要至少操作 %d 次\n", i, result[i]);

  148.         free(result);
  149.         for (int i = 0; i < t; i++)
  150.                 free(boxs[i]);
  151.         free(boxs);
  152. }

  153. int main()
  154. {
  155.         OperatorCount();

  156.         system("pause");
  157.         return 0;
  158. }
复制代码

2.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-25 08:42:17 From FishC Mobile | 显示全部楼层
新鱼油不会
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-25 08:57:44 | 显示全部楼层
等我操作
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-25 17:15:51 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-26 14:23:24 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-26 17:35:33 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-1 14:33:07 From FishC Mobile | 显示全部楼层
我哇
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-1 16:28:57 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-3 14:57:03 From FishC Mobile | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-3 16:18:08 From FishC Mobile | 显示全部楼层
好多作业
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-6 11:01:18 From FishC Mobile | 显示全部楼层
不会
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-6 19:18:19 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-12 18:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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