當前位置:編程學習大全網 - 編程語言 - 壹個正整數有可能可以被表示為 n(n>=2) 個連續正整數之和

壹個正整數有可能可以被表示為 n(n>=2) 個連續正整數之和

個正整數有可能可以被表示為n(n>=2)個連續正整數之和,如:

15=1+2+3+4+5

15=4+5+6

15=7+8

請編寫程序,根據輸入的任何壹個正整數,找出符合這種要求的所有連續正整數序列。

考慮到等差數列,從而大概判斷數列長度,然後得到數值.在輸入整數7位長度以內,還是能很快的計算出來結果,9為長度的時候就死機了感覺.應該在運算的時候有更好的關於長度的算法,比如字符串的滑塊移動算法壹樣,不過我壹下子想不出來怎麽做.只能用這種循環了.

private String Result(UInt64 Input)

{

String result = String.Empty;

for (UInt32 i = 2; i <=(UInt32) System.Math.Sqrt(Input * 2); i++)

{

if ((Input / i) <= (i - 1) / 2)

continue;

switch (i%2)

{

case 0 :

if (((float)(Input % i)/(float)i)==0.5)

result = result + i + '*';

else

break;

continue;

case 1 :

if ((Input % i) == 0)

result = result + i + '*';

else

break;

continue;

}

}

if(String.IsNullOrEmpty(result))

return "沒有符合的整數";

UInt32[] temp = Array.ConvertAll<String, UInt32>(result.Trim('*').Split('*'),new Converter < String, UInt32 >(Conver));

result = String.Empty;

foreach (UInt32 j in temp)

{

for (UInt32 k = 0; k < j; k++)

{

result = result + (((float)Input / (float)j) - (((float)(j - 1)) / 2.0) + k).ToString()+" ";

}

result = result + System.Environment.NewLine;

}

return result;

}

public static UInt32 Conver(String Value)

{

return Convert.ToUInt32(Value);

}

  • 上一篇:微信營銷的10種方法技巧?微信營銷軟件哪個好用?
  • 下一篇:web前端和C++哪個更好?
  • copyright 2024編程學習大全網