代码解题之“*升水,有个*升,和*升的空桶,让你取出*升水”

  bool get = false;
void Start ()
    {
        ExchangeWater(12, 9, 5, 6);
       // ExchangeWater(10, 7, 3, 5);
}


    /// <summary>
    /// 12升水,有个9升,和5升的空桶,让你取出6升水
    /// </summary>
    /// <param name="fullCansLitre"></param>
    /// <param name="emptyCansLitre1"></param>
    /// <param name="emptyCansLitrel2"></param>
    /// <param name="evaluation"></param>
    private void ExchangeWater(int fullCansLitre, int emptyCansLitre1, int emptyCansLitre2, int evaluation)
    {
        List<int[]> resoult = new List<int[]>();
        Recursion(new int[3] { fullCansLitre, emptyCansLitre1, emptyCansLitre2 }, evaluation, new int[3] { fullCansLitre, 0, 0 }, ref resoult);
    }
    /// <summary>
    /// 三个瓶中某一个向另外两个倾倒水
    /// </summary>
    /// <param name="one"></param>
    /// <param name="two"></param>
    /// <param name="evaluation"></param>
    /// <param name="resoult"></param>
    private void PourAwayTheWater(int[] one, int[] two, int evaluation, ref List<int[]> resoult) 
    {
        int m,n;
        for (int i = 0; i < one.Length; i++)
        {
            m = (i + 1) % 3;
            for (int j = 0; j < 2; j++)
            {
                n = (m + j) % 3;
                if (one[i] > 0 && two[n] != one[n])
                {
                    int deltaWater = 0;
                    int[] tempBottle = new int[3] { two[0], two[1], two[2] };
                    if (one[n] - two[n] >= two[i])
                    {
                        deltaWater = two[i];
                    }
                    else
                    {
                        deltaWater = one[n] - two[n];
                    }
                    tempBottle[i] -= deltaWater;
                    tempBottle[n] += deltaWater;
                    Recursion(one, evaluation, tempBottle, ref resoult);
                }
            }
        }
    }
    /// <summary>
    /// 递归向后遍历
    /// </summary>
    /// <param name="fullCansLitre"></param>
    /// <param name="emptyCansLitre1"></param>
    /// <param name="emptyCansLitre2"></param>
    /// <param name="evaluation"></param>
    /// <param name="bottle"></param>
    /// <param name="resoult"></param>
    private void Recursion(int[] cansLitre, int evaluation, int[] bottle, ref List<int[]> resoult)
    {
        if (get) return;
        if (AdjustCotains(resoult, bottle))
        {
           // Debug.Log("此结果已出现." + bottle[0] + "," + bottle[1] + "," + bottle[2]);
            return;
        }
        Debug.Log("添加" + bottle[0] + "," + bottle[1] + "," + bottle[2]);
if(bottle[0]== evaluation ||bottle[1]== evaluation ||bottle[2]== evaluation )
{
get = true;
Debug.Log("您要的结果已出现" + bottle[0] + "," + bottle[1] + "," + bottle[2]);
}
        resoult.Add(bottle);
        PourAwayTheWater(cansLitre, bottle, evaluation, ref resoult);
    }
    /// <summary>
    /// 判断是否已经包含此结果
    /// </summary>
    /// <param name="resoult"></param>
    /// <param name="temp"></param>
    /// <returns></returns>
    private bool AdjustCotains(List<int[]> resoult,int[] temp) 
    {
        if (resoult.Count == 0) 
            return false;
        int same;
        foreach (int[] item in resoult)
        {
            same = 0;
            for (int i = 0; i < item.Length; i++)
            {
                if (temp[i] == item[i])
                {
                    same++;
                }
            }
            if (same == 3)
                return true;
        }
        return false;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是桶排序的C++实现,带有注释: ```c++ void bucketSort(float arr[], int n) { // 创建一个桶数组,每个桶都是一个 vector vector<float> buckets[n]; // 将元素分配到各自的桶中 for (int i = 0; i < n; i++) { int bucketIndex = n * arr[i]; buckets[bucketIndex].push_back(arr[i]); } // 对每个桶中的元素进行插入排序 for (int i = 0; i < n; i++) { sort(buckets[i].begin(), buckets[i].end()); } // 将桶中的元素按顺序合并到原始数组中 int index = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < buckets[i].size(); j++) { arr[index++] = buckets[i][j]; } } } ``` 以下是桶排序的伪代码: ``` procedure bucketSort(arr: list of sortable items) n = length(arr) buckets = new array of n empty lists for i = 0 to n-1 do bucketIndex = n * arr[i] append arr[i] to buckets[bucketIndex] for i = 0 to n-1 do sort buckets[i] index = 0 for i = 0 to n-1 do for j = 0 to length(buckets[i])-1 do arr[index] = buckets[i][j] index = index + 1 end for end procedure ``` ### 回答2: 桶排序是一种效率较高的排序算法。它通过将元素分散到不同的桶中,再对每个桶中的元素进行排序,最后合并所有的桶,得到一个有序的序列。 以下是基于C语言的桶排序实现的伪代码及注释: 1. 首先确定桶的数量和每个桶的范围大小 - 桶的数量 k = ceil((max - min) / range),其中 max 为待排序数组的最大值,min 为最小值,range 为每个桶的范围大小 2. 创建 k 个空桶,用于存放元素 - 定义一个二维数组 buckets[k][n],其中 k 表示桶的数量,n 表示待排序数组的长度 3. 将待排序的元素分别放入对应的桶中 - 将待排序的元素 x 放入桶 buckets[j] 中,其中 j = floor((x - min) / range) 4. 对每个桶中的元素进行排序 - 使用任意一种排序算法(例如插入排序)对桶 buckets[i] 进行排序,其中 i 为桶的索引 5. 合并所有桶中的元素 - 创建一个新的数组 sorted[],用于存放排序后的元素 - 对于每个桶 buckets[i],将其中的元素依次放入 sorted[] 中 6. 返回排序后的数组 sorted[] 以下为对应的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> void bucketSort(int arr[], int n) { // 找出数组中的最大值和最小值 int max = arr[0], min = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) max = arr[i]; if (arr[i] < min) min = arr[i]; } // 确定桶的数量和范围大小 int range = (max - min + 1) / n; int k = (max - min) / range + 1; // 创建 k 个桶 int **buckets = (int **)malloc(k * sizeof(int *)); for (int i = 0; i < k; i++) { buckets[i] = (int *)malloc(n * sizeof(int)); } // 将元素放入对应的桶中 for (int i = 0; i < n; i++) { int j = (arr[i] - min) / range; int *bucket = buckets[j]; int index = 0; while (bucket[index] != 0) { index++; } bucket[index] = arr[i]; } // 对每个桶中的元素进行排序(这里使用插入排序) for (int i = 0; i < k; i++) { int *bucket = buckets[i]; int j = 1; while (bucket[j] != 0) { int temp = bucket[j]; int k = j - 1; while (k >= 0 && bucket[k] > temp) { bucket[k + 1] = bucket[k]; k--; } bucket[k + 1] = temp; j++; } } // 合并所有桶中的元素 int index = 0; for (int i = 0; i < k; i++) { int *bucket = buckets[i]; int j = 0; while (bucket[j] != 0) { arr[index++] = bucket[j]; j++; } } // 释放桶的内存 for (int i = 0; i < k; i++) { free(buckets[i]); } free(buckets); } int main() { int arr[] = {29, 10, 14, 37, 13}; int n = sizeof(arr) / sizeof(arr[0]); bucketSort(arr, n); printf("排序后的数组:"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } ``` 希望对你有所帮助! ### 回答3: 桶排序(Bucket Sort)是一种线性时间复杂度的排序算法,适用于已知待排序数组范围的情况。它的基本原理是将待排序元素按照一定规则映射到有限数量的桶中,再对每个桶内的元素进行排序,最后依次将桶内元素拼接起来,即可得到有序数组。 以下是桶排序的C语言实现: ```c #include <stdlib.h> // 定义桶结点,用于存储元素 typedef struct BucketNode { int data; struct BucketNode* next; } BucketNode; void BucketSort(int array[], int n) { const int bucketNum = 10; // 定义桶的数量,这里假设为10 BucketNode* buckets[bucketNum]; // 定义存放桶的数组,指针数组 int i, j; // 初始化桶数组中的每个元素为空指针 for (i = 0; i < bucketNum; i++) { buckets[i] = NULL; } // 将待排序元素按照一定规则放入对应的桶中 for (i = 0; i < n; i++) { BucketNode* newNode = (BucketNode*)malloc(sizeof(BucketNode)); // 创建新的桶结点 newNode->data = array[i]; newNode->next = NULL; // 将桶结点插入对应的桶中(使用头插法) int bucketIndex = array[i] / bucketNum; // 计算桶的索引 if (buckets[bucketIndex] == NULL) { buckets[bucketIndex] = newNode; } else { BucketNode* curNode = buckets[bucketIndex]; while (curNode->next != NULL) { curNode = curNode->next; } curNode->next = newNode; } } // 对每个桶中的元素进行排序 for (i = 0; i < bucketNum; i++) { if (buckets[i] != NULL) { BucketNode* curNode = buckets[i]; int index = 0; while (curNode != NULL) { array[index++] = curNode->data; BucketNode* tempNode = curNode; curNode = curNode->next; free(tempNode); // 释放桶结点的内存 } } } } // 测试 int main() { int array[] = {8, 4, 2, 9, 7, 1, 3, 6, 5}; int n = sizeof(array) / sizeof(array[0]); BucketSort(array, n); // 输出排序结果 for (int i = 0; i < n; i++) { printf("%d ", array[i]); } return 0; } ``` 以上是使用C语言实现的桶排序算法。首先定义了一个结构体`BucketNode`用来存储桶中的元素,其中`data`用于存储数据,`next`用于指向下一个结点。然后,通过循环将待排序数组中的元素按照桶的索引插入对应的桶中,这里使用了头插法。接着,对每个桶中的元素进行排序,排序完成后即可得到有序数组。最后,在`main`函数中进行测试,输出排序结果。 备注:以上是伪代码为基础的C语言实现桶排序的代码,具体实现细节可能需要根据具体情况进行微调和调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值