返回错误的输出

时间:2019-04-24 14:47:09

标签: c#

挑战说明:

让函数KaprekarsConstant(num)接受要传递的num参数,该参数将是一个4位数字,至少包含两个不同的数字。

您的程序应对该号码执行以下例程:

  • 按降序和升序排列数字(添加 零以使其适合4位数字),然后减去较小的 从较大的数字开始。然后重复上一步。
  • 执行此例程将始终使您达到固定的数字:6174。
  • 然后在6174上执行例程将始终为您提供6174(7641-1467 = 6174)。 您的程序应返回该例程必须执行的次数,直到达到6174。

例如:如果num为3524,则由于以下步骤,您的程序应返回3:

  1. 5432-2345 = 3087
  2. 8730-0378 = 8352
  3. 8532-2358 = 6174

我参加了此挑战Coderbyte的网站

问题:

一切正常,直到在Foo()返回结果为止,我不知道为什么,但是它有时会调用此函数,直到Count==2

请帮助。抱歉,如果我犯了错误并且我的代码真的很糟糕,因为我是9年级学生,并且已经编程半年了

using System;

class MainClass
{ 
    public static int Foo(int num,int Counter)
    {
        int Count = Counter;
        int[] arr = new int[4];
        arr[0] = num / 1000;
        arr[1] = num % 10;
        arr[2] = (num / 100) % 10;
        arr[3] = (num % 100) / 10;
        Array.Sort(arr);
        int[] AscArr = new int[4];
        arr.CopyTo(AscArr, 0);
        Array.Reverse(arr);
        int[] DescArr = arr;
        int sub = 0;
        string AscStr = string.Empty;
        string DescStr = string.Empty;
        for (int i = 0; i < AscArr.Length; i++)
        {
            AscStr += AscArr[i];
        }
        for (int i = 0; i < DescArr.Length; i++)
        {
            DescStr += DescArr[i];
        }
        int b = int.Parse(AscStr);
        int a = int.Parse(DescStr);
        sub = a - b;

        if (sub!=6174)
        {
            Count++;
            Foo(sub,Count);
        }
        if (sub==6174)
        {
            Count++;


        }

        return Count;

    }
    public static int KaprekarsConstant(int num)
    {
        int[] arr=new int[4];
        arr[0] = num / 1000;
        arr[1] = num % 10;
        arr[2] = (num / 100) % 10;
        arr[3] = (num % 100) / 10;
        Array.Sort(arr);
        int[] AscArr=new int[4];
        arr.CopyTo(AscArr,0);
        Array.Reverse(arr);
        int[] DescArr = arr;
        int sub = 0 ;
        string AscStr=string.Empty;
        string DescStr = string.Empty;
        for (int i = 0; i < AscArr.Length; i++)
        {
            AscStr += AscArr[i];
        }
        for (int i = 0; i < DescArr.Length; i++)
        {
            DescStr += DescArr[i];
        }
        int b = int.Parse(AscStr);
        int a = int.Parse(DescStr);
        sub = a - b;
        int Counter =1;
        int Count=0;
        if (Count!=6174)
        {
            Count = Foo(sub, Counter);
        }



        return Count;

    }

    static void Main()
    {
        // keep this function call here
        Console.WriteLine(KaprekarsConstant(int.Parse(Console.ReadLine())));
    }

}

1 个答案:

答案 0 :(得分:0)

您的代码过于复杂,此外,将数字除以array的方式给出了错误的结果。

// this is wrong you can print array, the numbers goes into wrong indexes 
arr[0] = num / 1000;
arr[1] = num % 10;
arr[2] = (num / 100) % 10;
arr[3] = (num % 100) / 10;

使用此:

using System;
class MainClass
{ 
    public static int count = 0;
    public static void KaprekarsConstant(int num)
    {
        if (num == 6174)  // base case
            return;
        count++;

        string[] Aarr=new string[4];
        string[] Darr = new string[4];
        string asc = "", dsc = "";

        Aarr[3] = (num % 10).ToString();
        Darr[3] = (num % 10).ToString();
        num /= 10;
        Aarr[2] =(num % 10).ToString();
        Darr[2] = (num % 10).ToString();
        num /= 10;
        Aarr[1] = (num % 10).ToString();
        Darr[1] = (num % 10).ToString();
        num /= 10;
        Aarr[0] =(num % 10).ToString();
        Darr[0] = (num % 10).ToString();

        Array.Sort(Aarr);  // ascneding sorted
        Array.Sort<string>(Darr, new Comparison<string>( (i1, i2) => i2.CompareTo(i1)));   // descending sorted

        for(int i = 0; i< 4;i++)
        {
            asc += Aarr[i];
            dsc += Darr[i];
        }
        KaprekarsConstant(Convert.ToInt32(dsc) -Convert.ToInt32(asc) );
}

    static void Main()
    {
        KaprekarsConstant(int.Parse(Console.ReadLine()));
        Console.WriteLine("\nIt took "+count + "times to reach 6174");
    }
}