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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· Qt个人项目总结 —— MySQL数据库查询与断言