比较两个数大小并返回(不许使用内置方法或者排序)

  • 比较两个整数并返回较小的
1 public static int Min(int a, int b)
2 {
3     long l1 = (long)a;
4     long l2 = (long)b;
5     return (int)((l1 + l2 - Math.Abs(l1 - l2)) / 2);
6 }
  •  比较两个float类型的数并返回较大的
1 public static float Max(float a, float b)
2 {
3     double d1 = (double)a;
4     double d2 = (double)b;
5     return (float)((d1 + d2 + Math.Abs(d1 - d2)) / 2);
6 }
原文地址:https://www.cnblogs.com/panchunting/p/interview_tech_algorithm_001.html