- 计算整数的商并返回余数
点击查看代码
int aa = 5, bb = 3;
var cc = Math.DivRem(bb, aa, out int d);
1.1. 数值比较
点击查看代码
int a = 3, b = 3;
var c1 = Math.Max(a, b);
var c = Math.Ceiling(a / (double)b);
- 二元组用法
点击查看代码
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);
}
}
- 求最大公约数
点击查看代码
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);