例如,如果币= [1,2,5]和N = 11,如果币= [3,77]且N = 100,则返回true,返回

时间:2017-02-27 02:35:08

标签: objective-c

在线等待.~

给出一些具有不同面额的硬币,例如: [1,2,5]并测试它们是否可用于弥补一定数量(N),假设您可以在每种面额中使用无限数量的硬币。例如,如果币= [1,2,5]且N = 11,如果币= [3,77]且N = 100则返回true,返回

1 个答案:

答案 0 :(得分:-1)

这个想法是使用递归函数(这里它将使用一个第一枚硬币与其他硬币的数组计算,然后以递归方式减小硬币阵列的大小)。

但是我不熟悉Objective-C所以我用C#编写了一个。使用时应将其转换为Objective-C。

bool CanDo(int n, int [] arr)  
{
    if (arr.Length == 1)
    {
        if (n % arr[0] == 0)
        {
            return true;
        }
    }
    else
    {
        var ls = new List<int>(arr);
        ls.RemoveAt(0);
        int [] newarr = ls.ToArray();    //Create New array by deleting first element(current calculated element) of old array

        for(int i = 0; i <= n/arr[0]; i++)
        {
            int next_n = n - i * arr[0];
            if (next_n == 0)
            {
                return true;
            }
            else if (next_n < 0)
            {
                break;
            }
            else if(next_n > 0)
            {
                if( CanDo(next_n, newarr) )
                {
                    return true;
                }
            }
        }
    }
    return false;
}

这是C#中的完整代码,可以打印到控制台首先找到的解决方案。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static List<string> resultString = new List<string>();
        static bool CanDo(int n, int [] arr)
        {
            if (arr.Length == 1)
            {
                if (n % arr[0] == 0)
                {
                    resultString.Add(n/ arr[0] + "*" + arr[0]);
                    return true;
                }
            }
            else
            {
                var ls = new List<int>(arr);
                ls.RemoveAt(0);
                int [] newarr = ls.ToArray();    //Create New array by deleting first element of old array

                for(int i = 0; i <= n/arr[0]; i++)
                {
                    if (resultString.Count > 0)
                    {
                        resultString.RemoveAt(resultString.Count - 1);
                    }

                    int next_n = n - i * arr[0];
                    if (next_n == 0)
                    {
                        resultString.Add(i + "*" + arr[0]);
                        return true;
                    }
                    else if (next_n < 0)
                    {
                        break;
                    }
                    else if(next_n > 0)
                    {
                        if (i != 0)
                        {
                            resultString.Add(i + "*" + arr[0] + " + ");
                        }
                        if( CanDo(next_n, newarr) )
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        static void Main(string[] args)
        {
            try
            {

                int[] arr = { 3, 5, 7 };
                int N = 20;
                resultString = new List<string>();

                if (CanDo(N, arr))
                {
                    resultString.ForEach(Console.WriteLine);
                    Console.Read();
                }
                else
                {
                    Console.Write("Can't do");
                    Console.Read();
                }

            }
            catch (Exception ex)
            {
                //handle exception
            }

        }
    }
}