C#查找排序练习

在这里插入图片描述

Fibonacci数列

using System;

namespace Fibonacci
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] f = new int[30];
            f[0] = 1;f[1] = 1;
            //int [] f = { 1, 1 }; 
            for(int i=2;i<f.Length-1;i++)
            {
                 f[i] = f[i-1]+f[i-2];
                if(i==28)
                {
                    Console.WriteLine(f[i]);
                }
                
            }
        }
    }

倒序输出字符串

using System;

namespace daoxu
{
    class Program
    {
        public  string  DaoXu(string[] a)
        {
            int i;
            for ( i=a.Length-1;i>=0;i--)
            {
                Console.Write(a[i]+" ");
              
            }
           return a[i];
            
        }

        static void Main(string[] args)
        {
            Program n = new Program();
            string[] strs = { "i", "am", "a", "good", "student" };
            string s = n.DaoXu(strs);
            Console.Write(s);
            
        }

    }
}

二分查找

using System;

namespace Binary_Search
{
    class Program
    {
      
        static void Main(string[] args)
        {
            int low, high, mid,key=0;
            low = 0;high = 7;
            int []a={ 3,8,24,35,55,70,75,98};
            while(low<=high)
            {
                mid=(low + high) / 2;
                if(24<a[mid])
                {
                    high = mid - 1;
                    key++;
                }
                if(24>a[mid])
                {
                    low = mid + 1;
                    key++;
                }
                else
                {
                    Console.WriteLine(key);
                    break;
                }
               
            }
           
         
        }

    }
}

冒泡排序

using System;

namespace MaoPao
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 6, 5, 9, 8, 7, 3, 2 };
           for(int n=0;n<7;n++)
                {
                    for(int m=0;m<7-n-1;m++)
                    {
                        if(array[m]<array[m+1])
                        {
                            int t = array[m];
                            array[m] = array[m + 1];
                            array[m + 1] = t;
                        }
                    }
                }

            for(int i=0;i<7;i++)
            {
                Console.WriteLine(array[i]);
            }
        }
    }
}

5

using System;

namespace Arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            int max = 0;
            int i=0, j=0;
            int [,] a={{5,6,7,8},{4,3,2,1},{9,9,8,7}};
            max = a[0, 0];
            for (i=0;i<3;i++)
            {
                for(j=0;j<4;j++)
                {
                   
                    if(a[i,j]>max)
                    {
                        max = a[i, j];
                    }
                }
            }
            Console.WriteLine("最大值为" + max);
            Console.WriteLine("行号为{0},列号为{1}", i-1, j-1);
       
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值