Math、Tuple、公约数用法

1. 计算整数的商并返回余数

点击查看代码
 int aa = 5, bb = 3;
 var cc = Math.DivRem(bb, aa, out int d);

2. 数值比较

点击查看代码
 int a = 3, b = 3;
 var c1 = Math.Max(a, b);
 var c = Math.Ceiling(a / (double)b);

3. 二元组用法

点击查看代码
  List<Tuple<int, int>> tt = new List<Tuple<int, int>>()
            {
                Tuple.Create(4,0),
                Tuple.Create(4,0),
                Tuple.Create(5,0)
            };
            int maxProportionalUnits = 0;
            for (int i = 0; i < (tt.Count - 1); i++)
            {
                var cur = tt[i];
                var next = tt[i + 1];
                if (!cur.Equals(next))
                {
                    var max = Math.Max(cur.Item1, next.Item1);
                    maxProportionalUnits = Math.Max(max, maxProportionalUnits);
                }
            }

4. 求最大公约数

点击查看代码
  static int GCD(List<int> listOri)
        {
            List<int> list = new List<int>(listOri);
            int c = 1;
            for (int i = 1; i < list.Count; i++)
            {
                if (list[i - 1] < list[i])
                {
                    list[i - 1] = list[i - 1] + list[i];
                    list[i] = list[i - 1] - list[i];
                    list[i - 1] = list[i - 1] - list[i];
                }

                for (c = list[i]; c >= 1; c--)
                {
                    if (list[i - 1] % c == 0 && list[i] % c == 0)
                        break;
                }

                list[i] = c;
            }

            return c;
        }

  List<int> ori = new List<int>() { 4, 6, 0 };
  var gcd = GCD(ori);
posted @   希腊若蝶  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· Qt个人项目总结 —— MySQL数据库查询与断言
点击右上角即可分享
微信分享提示