.net提供了基础的数学函数
Math常用方法 |
|
绝对值 | Abs |
返回两个32位大数乘积 |
BigMul |
接近数 |
Ceiling Floor Round |
求余和商 | DivRem |
数字比较 |
Max Min |
返回指定数字的平方根 |
Sqrt |
返回幂 |
Pow |
三角函数 |
Acos Asin Atan Cos Cosh Sin Tan |
1 System.Console.WriteLine(System.Math.Ceiling(1.1415926));
2 System.Console.WriteLine(System.Math.Floor(1.1415926));
3 System.Console.WriteLine(System.Math.Min(1, 10));
4 System.Console.WriteLine(System.Math.Max(1, 10));
5 //对于下面的Round请注意观察
6 System.Console.WriteLine(System.Math.Round(3.44, 1));
7 System.Console.WriteLine(System.Math.Round(3.45, 1));
8 System.Console.WriteLine(System.Math.Round(3.46, 1));
9 System.Console.WriteLine(System.Math.Round(3.54, 1));
10 System.Console.WriteLine(System.Math.Round(3.55, 1));
11 System.Console.WriteLine(System.Math.Round(3.56, 1));
12 System.Console.WriteLine(System.Math.Sqrt(2));
2 System.Console.WriteLine(System.Math.Floor(1.1415926));
3 System.Console.WriteLine(System.Math.Min(1, 10));
4 System.Console.WriteLine(System.Math.Max(1, 10));
5 //对于下面的Round请注意观察
6 System.Console.WriteLine(System.Math.Round(3.44, 1));
7 System.Console.WriteLine(System.Math.Round(3.45, 1));
8 System.Console.WriteLine(System.Math.Round(3.46, 1));
9 System.Console.WriteLine(System.Math.Round(3.54, 1));
10 System.Console.WriteLine(System.Math.Round(3.55, 1));
11 System.Console.WriteLine(System.Math.Round(3.56, 1));
12 System.Console.WriteLine(System.Math.Sqrt(2));
有两个数:m和n。不使用第3变量交换这两个值
1 int m = 76;
2 int n = 100;
3
4 m = m + n;
5 n = m - n;
6 m = m - n;
7
8 System.Console.WriteLine("m:{0}", m);
9 System.Console.WriteLine("n:{0}", n);
2 int n = 100;
3
4 m = m + n;
5 n = m - n;
6 m = m - n;
7
8 System.Console.WriteLine("m:{0}", m);
9 System.Console.WriteLine("n:{0}", n);
给定一个整数,不使用模的方式判断其是不是偶数
1 for (int n = 1; n <= 100; n++)
2 {
3 int m = 0;
4 int i = 0;
5 do
6 {
7 m = i * 2;
8 i++;
9 }
10 while (m < n);
11 if (m == n)
12 {
13 System.Console.WriteLine("{0}是偶数", n);
14 }
15 else
16 {
17 System.Console.WriteLine("{0}是奇数", n);
18 }
19 }
2 {
3 int m = 0;
4 int i = 0;
5 do
6 {
7 m = i * 2;
8 i++;
9 }
10 while (m < n);
11 if (m == n)
12 {
13 System.Console.WriteLine("{0}是偶数", n);
14 }
15 else
16 {
17 System.Console.WriteLine("{0}是奇数", n);
18 }
19 }
同理求质数
1 for (int k = 0; k <= 100; k++)
2 {
3 bool isSearch = false;
4 for (int i = 2; i <= k - 1; i++)
5 {
6 for (int j = i; j <= k - 1; j++)
7 {
8 if (i * j == k)
9 {
10 isSearch = true;
11 break;
12 }
13 }
14 if (isSearch == true)
15 {
16 break;
17 }
18 }
19 if (isSearch == true)
20 {
21 System.Console.WriteLine("{0}不是质数", k);
22 }
23 else
24 {
25 System.Console.WriteLine("{0}是质数", k);
26 }
27 }
2 {
3 bool isSearch = false;
4 for (int i = 2; i <= k - 1; i++)
5 {
6 for (int j = i; j <= k - 1; j++)
7 {
8 if (i * j == k)
9 {
10 isSearch = true;
11 break;
12 }
13 }
14 if (isSearch == true)
15 {
16 break;
17 }
18 }
19 if (isSearch == true)
20 {
21 System.Console.WriteLine("{0}不是质数", k);
22 }
23 else
24 {
25 System.Console.WriteLine("{0}是质数", k);
26 }
27 }