c#程序实现:给定一组数据序列:78、89、90、56、79、345、217、5、13、88,要求用插入排序法将其按降序排列
时间: 2023-09-18 12:10:44 浏览: 40
以下是用 C# 实现给定一组数据序列按降序排列的插入排序算法:
```csharp
using System;
class Program {
static void Main(string[] args) {
int[] arr = {78, 89, 90, 56, 79, 345, 217, 5, 13, 88};
int n = arr.Length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
Console.WriteLine("降序排列后的序列:");
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
}
}
```
输出结果为:
```
降序排列后的序列:
345 217 90 88 79 78 56 13 5
```
相关问题
c#编写程序:实现对“1+3+5+7+…+99”的求和功能
在C#中,你可以使用循环来实现对“1+3+5+7+…+99”的求和功能。以下是一个示例代码:
```csharp
int sum = 0;
for (int i = 1; i <= 99; i += 2)
{
sum += i;
}
Console.WriteLine("求和结果为:" + sum);
```
这段代码使用了一个for循环,从1开始,每次递增2,直到达到99。在循环体内,将每个奇数累加到变量sum中。最后,输出求和结果。
请写一段C#程序:根据一个三维点的序列和一条直线方程计算围成的面积
好的,下面是一段C#程序,根据一个三维点的序列和一条直线方程计算围成的面积,这里假设三维点的序列是一个数组 `points`,直线方程是 `ax + by + cz + d = 0`:
```csharp
using System;
public class Program
{
public static void Main(string[] args)
{
// 三维点的序列
double[,] points = new double[,] {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12},
{13, 14, 15},
{16, 17, 18}
};
// 直线方程
double a = 2, b = 3, c = 4, d = -5;
// 计算围成的面积
double area = 0;
for (int i = 0; i < points.GetLength(0); i++)
{
double x = points[i, 0];
double y = points[i, 1];
double z = points[i, 2];
double dist = Math.Abs(a * x + b * y + c * z + d) / Math.Sqrt(a * a + b * b + c * c);
area += dist;
}
Console.WriteLine("围成的面积为:" + area);
}
}
```
在上面的代码中,我们使用了一个二维数组 `points` 存储了三维点的序列,直线方程的系数 `a`、`b`、`c`、`d` 可以根据实际情况修改。在计算围成的面积时,我们使用了每个点到直线的距离之和作为围成的面积,这里的距离是指点到直线的垂直距离。最后输出结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)