递归、遍历、冒泡

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1.一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。

public class TuZi

{

public int TZ(int x)

{

if (x == 1 || x == 2)

{

return 1;

}

else

{

return TZ(x - 1) + TZ(x - 2);

}

}

}

classPRogram

{

static void Main(string[] args)

{

TuZi a = new TuZi();

Console.WriteLine(a.TZ(12));

}

}

2.遍历页面上所有的textbox页面并将它赋值为string.empty?

foreach (System.Windows.Forms.Control item in this.Controls)

{

if (item is System.Windows.Forms.TextBox)

{

System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)item;

tb.Text = string.Empty;

}

}

control类是所有控件的基类

3.实现一个冒泡排序算法

int[] arr = { 10, 29, 30, 40, 5 };

for (int i = arr.Length; i >= 2;i-- )

{

for (int j = 0; j <i - 1;j++ )

{

if (arr[j] < arr[j + 1])

{

int temp = arr[j + 1];

arr[j + 1] = arr[j];

arr[j] = temp;

}

}

}

foreach (int item in arr)

{

Console.Write(item+" ");

}

Console.Read();

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航