经典代码收集
??? /// 數(shù)學界把這個數(shù)列叫做斐波那契數(shù)列
??? /// 1、1、2、3、5、8、13、21、34...... 求第n位數(shù)是多少
??? /// n 由用戶輸入
??? /// </summary>
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? string s = Console.ReadLine();
??????????? int num;
??????????? if (int.TryParse(s, out num))
??????????????? Console.WriteLine("結(jié)果是: {0}", countFibonacci(num));
??????????? else
??????????????? Console.WriteLine("無效的輸入...");
??????????? Console.ReadLine();
??????? }?
??????? static int countFibonacci(int n)
??????? {
?? ? ? ? ??if (n < 2)
??????????????? return n;
??????????? return countFibonacci(n - 1) + countFibonacci(n - 2);
??????? }
??????? //countFibonacci(n - 1)求出它的前一個數(shù)
??????? //countFibonacci(n - 2)求出它的前的第二個數(shù)
??? }
?? ? ? ?/// <summary>
??????? /// 字符串反轉(zhuǎn)的算法
??????? /// </summary>
??????? /// <param name="str"></param>
??????? /// <returns></returns>
??????? public static string Reverse(string str)
??????? {
??????????? if (string.IsNullOrEmpty(str))
??????????? {
??????????????? throw new ArgumentException("參數(shù)不合法");
??????????? }
??????????? //當使用StringBuilder時,請注意,應在構(gòu)造StringBuilder對象時指明初始容量,
??????????? //否則默認容量是16個字符,當由于追加字符而超出默認容量時,
??????????? //就會分配一個新的串緩沖區(qū),大小是原緩沖區(qū)的兩倍
??????????? StringBuilder sb = new StringBuilder(str.Length);
?
??????????? for (int index = str.Length - 1; index >= 0; index--)
??????????? {
??????????????? sb.Append(str[index]);
??????????? }
??????????? return sb.ToString();
?? ? ? ?}?
?
?
?
?? ?/// <summary>
??? /// 從 1 加到 100 的C#算法
??? /// </summary>
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? int Num = 100;
??????????? int Sum = 0;
??????????? for (int i = 1, j = Num; i <= Num / 2; i++, j--)
??????????? {
??????????????? Sum += i + j;
??????????? }
??????????? Console.WriteLine(Sum.ToString());
??????????? Console.ReadLine();
??????? }
??? }?
?
?? ?/// <summary>
??? /// 求 1 到 100 的素數(shù)
??? /// 素數(shù):是這樣的整數(shù),它除了能表示為它自己和1的乘積以外,
??? /// 不能表示為任何其它兩個整數(shù)的乘積
??? /// </summary>
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
???????? Console.Write(2 + " ");
???????? for (int m = 3; m <= 100; m+=2)
???????? {
???????????? bool a = true;
???????????? if (m%2!=0)
???????????? {
??????????????? for (int i = 3; i < m/2; i+=2)
??????????????? {
??????????????????? if (m%i==0)
??????????????????? { a = false; break; }
??????????????? }
??????????? }
??????????? if (a == true)
??????????? {
??????????????? Console.Write(m.ToString() + " ");
??????????? }
??????? }
??????? Console.ReadLine();
??????? }
?? ?}?
轉(zhuǎn)載于:https://www.cnblogs.com/NetDeng/archive/2010/03/04/1678094.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
- 上一篇: [转+总结]Linux虚拟系统安装VMw
- 下一篇: Windows Mobile 6.5.3