简体   繁体   中英

C# char Conversion/Append to/with int, int64, string

Lately I have been working through Project Euler, specifically

https://projecteuler.net/problem=4

  • I create to arrays
  • Multiply them together
  • Convert the number in a CharArry
  • Compare the numbers
  • If true, my problem arises

I attempt to convert the char back to an int, or long, or string, and I have attempted to append the char to an int, or long, or string, or whatever

    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;
}

After some time (resting the mind is always beneficial) I found a solution:

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++;
            }
        }

I had to convert each individual value within the char array c[] to a string, then an int, then append it to the string builder sb.

After that I then convert sb to a string (via ToString()) and Parse it to an int.

It seems like a long work around, but it works.

Now I need to Sort it numerically (another hurdle).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM