请选择 进入手机版 | 继续访问电脑版

马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#中遍历、递归、冒泡

[复制链接]
查看3509 | 回复1 | 2015-8-3 09:51:00 | 显示全部楼层 |阅读模式
1.一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。



[C#] 纯文本查看 复制代码
public class TuZi
    {
        public int TZ(int x)
        {
            if (x == 1 || x == 2)
            {
                return 1;
            }
            else
            {
                return TZ(x - 1) + TZ(x - 2);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            TuZi a = new TuZi();
            Console.WriteLine(a.TZ(12));
        }
    }




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

[C#] 纯文本查看 复制代码
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.实现一个冒泡排序算法
[C#] 纯文本查看 复制代码
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();



C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
卖烤地瓜的 | 2015-8-3 09:56:41 | 显示全部楼层
如果为0呢
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则