zoukankan      html  css  js  c++  java
  • 输入五个数字,按从大到小的顺序输出

                        此处使用C#控制台

    方法一:直接使用if判断语句进行排序(此处应用到冒泡排序的思想)

    Console.WriteLine("请输入五个数字:");

      int a,b,c,d,e,t;

      a=int.Parse(Console.ReadLine());

      b=int.Parse(Console.ReadLine());

      c=int.Parse(Console.ReadLine());

      d=int.Parse(Console.ReadLine());

      e=int.Parse(Console.ReadLine());

      if( a < b) { t = a; a = b; b = t;}

      if (a < c) { t = a; a = c; c = t; }
      if (a < d) { t = a; a = d; d = t; }
      if (a < e) { t = a; a = e; e = t; }
      if (b < c) { t = b; b = c; c = t; }
      if (b < d) { t = b; b = d; d = t; }
      if (b < e) { t = b; b = e; e = t; }
      if (c < d) { t = c; c = d; d = t; }
      if (c < e) { t = c; b = d; d = t; }
      if (d < e) { t = d; d = e; e = t; }

    Console.WriteLine("输出结果为:{0}>{1}>{2}>{3}>{4}",a,b,c,d,e);

    Console.ReadKey();

    此处附加优化(方法提取):

     

     

    方法二:使用冒泡排序

      int[]arr = new int[5];

      for(int i= 0; i<arr.Length;i++){

        Console.WriteLine("请输入第{0}个数字",i+1);

        arr[i]=int.Parse(Console.ReadLine());

      }

      int x = 0;
      for (int i = 0; i < a.Length - 1; i++)//外循环为排序趟数,len个数进行len-1趟 
      {
        for (int j = 0; j < a.Length - 1 - i; j++)// 内循环为每趟比较的次数,第i趟比较len-i次
        {
          if (a[j] < a[j + 1])//相邻的两个元素进行比较,若是从大到小则进行交换(即大的在左,小的在右)
          {
            x = a[j + 1];
            a[j + 1] = a[j];
            a[j] = x;
          }
        }
      }

      Console.WriteLine("输出结果为:");

      for(int i= 0; i<arr.Length;i++){

        Console.Write("{0}>",arr[i]);

      }

     

  • 相关阅读:
    缓存一致性问题
    缓存雪崩
    mysql Replication机制
    数据库水平切分、拆库拆表
    mysql分表和分区实际应用简介
    mysql中间件
    mysql基础知识
    go语言redis使用(redigo)
    nginx location配置与rewrite配置
    PDO驱动使用
  • 原文地址:https://www.cnblogs.com/Kai-YoungMaster/p/12375070.html
Copyright © 2011-2022 走看看