C#char转换/追加到/,带有int,int64,string

时间:2016-06-16 20:11:52

标签: c#

最近我一直在使用Project Euler,特别是

https://projecteuler.net/problem=4

  • 我创建数组
  • 将它们相乘
  • 转换CharArry中的数字
  • 比较数字
  • 如果是,我的问题就出现了

我尝试将char转换回int,long或string, 和 我试图将char附加到int,long或string,或任何

    void Main()
{
    int[] arrOne = new int[900];                            // Initializing Array One
    int[] arrTwo = new int[900];                            // Initializing Array Two

    Console.WriteLine(PopulateAndConvert(arrOne, arrTwo));  // Sending info into class

}

int PopulateAndConvert(int[] a, int[] b)
{
    char[] c = new char[1];                                 // char used to store tested number
    //string[] m = new string[a.Length*b.Length];
    long l = 0;                                             // Used for testing code

    for(int i = 0; i < a.Length; i++)                       // Populating Arrays One and Two
    {
        a[i] = i + 100;
        b[i] = i + 100;
    }

    for(int j = a.Length-1; j >= 0; j--)                    // Beginning for-loops for multiplication and testing
    {       
        //Console.WriteLine(j);
        for(int k = b.Length-1; k >= 0; k--)                // Second part of for-loop previously mentioned
        {
            //Console.WriteLine(k);
            c = (a[j] * b[k]).ToString().ToCharArray();     // Where the math and conversion happens
            //Console.WriteLine(c);

            if(c.Length > 5)                                // Checking if digit of product is greater than 5
            {
                if((c[0] == c[c.Length-1]) &&               // Comparing first and second half of product
                    (c[1] == c[c.Length-2]) && 
                    (c[2] == c[c.Length-3])) 
                {
                    /*for(int n = 0; n < c.Length; n++)     // Last tidbit of code that was being attempted
                        sb[l].Append(Convert.ToInt32(c[0])); 
                    l++;
                    Console.WriteLine(sb); */
                }
            }
            else if (c.Length < 5)                          // Product with less than 6 digits go here
            {               
                if((Convert.ToInt32(c[0]) == Convert.ToInt32(c[4])) && 
                    (Convert.ToInt32(c[1]) == Convert.ToInt32(c[3]))) 
                {
                    //m[l] = Convert.ToChar(c); l++;
                }
            }
        }
    }
    // Everything below was used to check the code that I have been trying to work through
    // And to place the given products in a ascending or descending order
    //foreach (char x in m)
    //  Console.WriteLine(m);


    //IEnumerable<char> sortDescendingQuery =
    //  from num in c
    //  orderby num descending
    //  select num;

    return 0;
}

1 个答案:

答案 0 :(得分:1)

过了一段时间(休息时心灵总是有益的)我找到了解决方案:

if(c.Length > 5)                                // Checking if digit of product is greater than 5
        {
            int[] n = new int[c.Length];
            StringBuilder sb = new StringBuilder();

            if((c[0] == c[c.Length-1]) &&               // Comparing first and second half of product
                (c[1] == c[c.Length-2]) && 
                (c[2] == c[c.Length-3])) 
            {
                for(int l = 0; l < c.Length; l++)       // Converting each value in the char array to a stringbuilder
                {
                    sb.Append(Convert.ToInt32(new string(c[l], 1)));
                }
                m[q] = Int32.Parse(sb.ToString());      // Converting stringbuilder into string and then into a long
                q++;
            }
        }

我必须将char数组c []中的每个单独值转换为字符串,然后转换为int,然后将其附加到字符串生成器sb。

之后我将sb转换为字符串(通过ToString())并将其解析为int。

这似乎是一项长期工作,但它确实有效。

现在我需要用数字排序(另一个障碍)。