C#五道笔试题

C#五道笔试题

1、 请编程实现一个冒泡排序算法?

class Program
    {
        static void Main(string[] args)
        {
            int[] test = { 5, 6, 2, 1, 8, 4, };
            popSort.popSort1(test);
        }
    }
    public static class popSort
    {
        public static void popSort1(int[] a)
        {
            int temp = 0;
            for (int i = 0; i < a.Length - 1; i++)
                for (int j = i + 1; j < a.Length; j++)
                {
                    if (a[i] > a[j])
                    {
                        temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            foreach (var n in a)
            {
                Console.WriteLine(n);
            }

        }


    }
}

2、 产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。

 class Program
    {
        static void Main(string[] args)
        {
            ArrayList a = new ArrayList();
            Random r = new Random();

            for (int j = 0; j <= 100; j++)
            {
                int temp = r.Next(1, 101);
                if (!a.Contains(temp))
                {
                    a.Add(temp);
                }
                else
                {
                    j--;
                }

            }
               a.Sort();
            foreach (var i in a)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }
    }

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

class Program
    {
        static void Main(string[] args)
        {
            int i = 30;
            int temp = Fibonacci.Fib(i);

            Console.WriteLine(temp);
            Console.ReadKey();
        }
    }
    public static class Fibonacci
    {
        public static int Fib(int a)
        {
            if (a <= 0)

                return 0;

            if (a < 3)
            {
                return 1;
            }
            else
            {
                return Fib(a - 1) + Fib(a - 2);
            }

        }

    }
}

4、 给定任一32位无符号整型数,判断是否为2的n次方。

class Program
    {
        static void Main(string[] args)
        {
            int a = 8;
            bool b = Judgement.Jud(a);
            Console.WriteLine(b);
        }
    }
    public static class Judgement
    {
        public static bool Jud(int i)
        {
            if(i==1)
            {
               return true;
            }
            else
            {
            do
            {
                if (i % 2 == 0)
                    i = i / 2;
                else
                    return false;
            }
            while (i != 1);
            }
            return true;
        }
    }
}

5、 已知一个三位数加上另一个三位数之和为四位数,算式中数字恰好为0、1、2、3、4、5、6、7、8、9各出现一次,请编程来求这个算式

 class Program
    {
        static void Main(string[] args)
        {
                int count=0;
                for (int a = 203; a <= 595; a++)
                for (int b = 987; b >= 595; b--)
                {
                    int result = a + b;
                    
                    if (isTrue(result, a, b))
                    {
                        count = count + 1;
                        Console.Write(a + " + ");
                        Console.Write(b + " = ");
                        Console.WriteLine(result);
                    }
                }
                        
                    Console.WriteLine(count);
                    Console.ReadKey();

        }
        
        public static bool isTrue(int i, int j, int k)
        {
            string str = "0123456789";
            string s = i + "" + j + "" + k;
            for (int t = 0; t < str.Length; t++)
            {
                if (!s.Contains(str[t]))
                {
                    return false;
                }
            }

                    return true;
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值