关于字符串、递归、冒泡排序的一些操作

本文介绍了使用递归来计算斐波那契数列的具体实现,并通过一个实例展示了如何求得斐波那契数列第30位的数值。此外,还提供了一种冒泡排序算法的实现方式及字符串到整型数组的转换方法。
            a = textBoxX1.Text.ToString();
            b = textBoxX2.Text.ToString();
            d = a.Contains(b).ToString();//判断a中是否包含b 返回 true false
            MessageBox.Show (d);

            //分组
            string[] b1 = a.Split('*');//通过*号分割字符串a
            c = b1[0];//通过索引位置取值

            //判断并返回坐标
            int index = a.IndexOf("*");//判断a中是否包含*,返回*的索引位置
            if (index > -1)
            {
                c = a.Substring(0, index - 1);//截取字符串  0,*索引号-1
            }

 

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

        //\\//\\//\\//\\递归//\\//\\//\\//\\
        public static int Foo(int i)
        {
            if (i <= 0)
                return 0;
            else if (i > 0 && i <= 2)
                return 1;
            else
                return Foo(i - 1) + Foo(i - 2);
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            int i = int.Parse(textBoxX3.Text.ToString());
            labelX1.Text = Foo(i).ToString();
        }

 

冒泡排序+数组类型转换

        //\\//\\//\\//\\//冒泡排序\\//\\//\\//\\//\\//
        private void MaoPao(int[] a)
        {
            int[] array = a;
            int temp = 0;
            for (int i = 0; i < array.Length - 1; i++)
            {
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (array[j] < array[i])
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }
        //\\//\\//\\//\\数组类型转换//\\//\\//\\
        public static int StrToInt(string str)
        {
            return int.Parse(str);
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            string[] arrs =textBoxX4.Text.ToString().Split(',');
            int[] arri = Array.ConvertAll(arrs, new Converter<string, int>(StrToInt));
            MaoPao(arri);
            for (int i = 0; i <=arri.Length-1; i++)
            {
                textBoxX5.Text += arri[i]+" ";                
            }
        }

 

转载于:https://www.cnblogs.com/zhizhuo-1991/p/5565951.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值