ctyaa 发表于 2021-5-12 13:42:32

{:10_243:}

xxm8023 发表于 2021-5-12 21:35:13

{:10_249:}

yuxijian2020 发表于 2021-5-12 23:25:28

本帖最后由 yuxijian2020 于 2021-5-12 23:33 编辑

工具人报到{:10_256:}
太晚了,就弄了第一个{:10_258:}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

        while (box != 0)
        {
                distance++;
                cur = (cur - 1) < 0 ? num - 1 : cur - 1;
        }

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

        while (box != 0)
        {
                distance++;
                cur = (cur + 1) < num ? cur + 1 : 0;
        }

        return distance;
}
//优先向左边填充
int LeftFindNearestZeroAndSwap(int* box, int num, int pos)
{
        int leftZero = LeftFindZero(box, num, pos);
        int rightZero = RightFindZero(box, num, pos);
        int cur = 0;

        if (leftZero > rightZero)
        {
                cur = rightZero + pos >= num ? (rightZero + pos - num) : rightZero + pos;
                box++;
                box--;
                return rightZero;
        }
        else
        {
                cur = pos - leftZero < 0 ? (pos + num - leftZero) : pos - leftZero;
                box++;
                box--;
                return leftZero;
        }
}
//优先向右边填充
int RightFindNearestZeroAndSwap(int* box, int num, int pos)
{
        int leftZero = LeftFindZero(box, num, pos);
        int rightZero = RightFindZero(box, num, pos);
        int cur = 0;

        if (rightZero > leftZero)
        {
                cur = pos - leftZero < 0 ? (pos + num - leftZero) : pos - leftZero;
                box++;
                box--;
                return leftZero;
        }
        else
        {
                cur = rightZero + pos >= num ? (rightZero + pos - num) : rightZero + pos;
                box++;
                box--;
                return rightZero;
        }
}
//分发
int HandOut(int* box, int num)
{
        if (num <= 0) return 0;

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

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

        for (int i = 0; i < num; i++)
        {
                leftBox        = box;
                rightBox = box;
        }

        for (int i = 0; i < num; i++)
        {
                while (leftBox > 1)
                        leftCount += LeftFindNearestZeroAndSwap(leftBox, num, i);
        }

        for (int j = num - 1; j >= 0; j--)
        {
                while (rightBox > 1)
                        rightCount += RightFindNearestZeroAndSwap(rightBox, num, j);
        }

        free(leftBox);
        free(rightBox);

        count = leftCount > rightCount ? rightCount : leftCount;
        return count;
}

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

        while (N < 1 || N > 500)
        {
                printf_s("共有几个盒子?(1 <= N <= 500)\n");
                scanf_s("%d%*c", &N);
                if (N < 1 || N > 500)
                        printf_s("盒子数量错误!请重新输入!\n");
        }
        //申请空间
        boxs = (int**)malloc(sizeof(int*) * t);
        if (boxs == NULL) return;                                        //判断是否申请成功
        for (int i = 0; i < t; i++)
        {
                boxs = (int*)malloc(sizeof(int) * N);
                if (boxs == NULL) return;
                memset(boxs, 0, sizeof(int) * N);
        }

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

        for (int i = 0; i < t; i++)
        {
                for (int j = 0; j < N; j++)
                {
                        printf_s("请输入第 %d 组 第 %d 个盒子中的糖果数量:", i, j);
                        scanf_s("%d", &boxs);
                }
                result = HandOut(boxs, N);
        }

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

        free(result);
        for (int i = 0; i < t; i++)
                free(boxs);
        free(boxs);
}

int main()
{
        OperatorCount();

        system("pause");
        return 0;
}

最强废铁h 发表于 2021-5-25 08:42:17

新鱼油不会

王之叹息 发表于 2021-5-25 08:57:44

等我操作{:5_104:}

江河之缘 发表于 2021-5-25 17:15:51

{:5_103:}

jia1230 发表于 2021-5-26 14:23:24

{:10_277:}

狱鬼·泪魂 发表于 2021-5-26 17:35:33

{:5_109:}

小徐是我呀 发表于 2021-6-1 14:33:07

我哇

万千只cnm 发表于 2021-6-1 16:28:57

{:5_90:}

hoh478 发表于 2021-6-3 14:57:03

{:10_245:}

怀瑾啊怀瑾 发表于 2021-6-3 16:18:08

好多作业

小灰灰呀 发表于 2021-6-6 11:01:18

不会

a2316565897 发表于 2021-6-6 19:18:19

{:5_90:}
页: 1 [2]
查看完整版本: 用C语言写出如下条件程序