2016-06-16 56 views
0

最近我一直努力通過項目歐拉,具體C#焦炭轉化/附加到/與INT,Int64的,串

https://projecteuler.net/problem=4

  • 創建到陣列
  • 它們相乘
  • 轉換CharArry中的號碼
  • 比較號碼
  • 如果屬實,我的問題出現

我試圖將字符轉換回int或長,或字符串, 和 我試圖炭追加到int或長或字符串,或任何

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

究竟是什麼問題? –

+0

你可以簽出可以將'int/double/etc'轉換成'byte []'類的'BitConverter' –

回答

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

我不得不炭數組c []內的每個單獨值轉換爲字符串,然後一個int,然後追加它到t他是絃樂建造者。

然後,我將sb轉換爲字符串(通過ToString())並將其解析爲int。

這似乎是一個漫長的工作,但它的工作原理。

現在我需要對它進行數字排序(另一個障礙)。