- 博客(0)
- 资源 (5)
空空如也
用c#实现金字塔图案
using System;
using System.Collections.Generic;
using System.Text;
namespace 金字塔
{
class jinzita
{
public void fa()
{
Console.WriteLine("请输入一个一到九的整数");
int n = 0;
n = Convert.ToInt32(Console .ReadLine ());
Console.WriteLine("您输入的数是:"+n);
//遍历行
for (int hang = 1; hang <= n;hang++ )
{
//留空格
for (int count = 1; count <= n - hang;count++ )
{
Console.Write(" ");
}
//写左边数字
for (int i = 1; i <= hang;i++ )
{
Console.Write(i+" ");
}
if(hang==1)
{
Console.WriteLine("");
}
else
{
//写右边数字
for (int j = hang - 1; j >= 1;j-- )
{
Console.Write(j+" ");
if(j==1)
{
Console.WriteLine("");
}
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
jinzita da = new jinzita();
da.fa();
Console.ReadLine();
}
}
}
2009-06-14
归并排序&&快速排序c#源码
//快速排序
public void q_sort(int low, int high, int[] a)
{
int pivot, i, j,temp;
if (low > high)
return;
i = low + 1;
j = high;
pivot=a[low];
while (i <= j)
{
while ((i <= high) && (a[i] = low) &&(a[j] > pivot) )//搜索小于枢轴的元素
{
j--;
}
if (i < j)
{
temp = a[i];
a[i]=a[j];
a[j] = temp;
}
}
if (low = high)
{
return;
}
mid = (low + high) / 2;
MergeSort(low,mid,a);
MergeSort(mid+1,high,a);
i = low;
j = mid + 1;
k = low;
while ((i <= mid) && (j <= high))
{
if (a[i] <= a[j])
{
b[k] = a[i];
i++;
}
else
{
b[k]=a[j];
j++;
}
k++;
}
while (j <= high)//如果第二个中仍然有某些元素追加到新列表的子列表
{
b[k] = a[j];
j++;
k++;
}
while (i <= mid)//如果在第一个子列表中仍然有一些元素将它们追加到新类别中
{
b[k] = a[i];
i++;
k++;
}
for (int ii = low; ii <=high; ii++)
{
a[ii]=b[ii];
}
}
public void display(int[] a)
{
int n = a.Length;
Console.WriteLine("排序后的数据:");
for (int i = 0; i < n; i++)
{
Console.WriteLine(a[i]);
}
}
}
2009-06-14
选择排序和插入排序c#源码
选择排序&插入排序
//选择排序
public void select_sort(int[] a)
{
int n = a.Length;
int minIndex=0;
int temp;
for (int i = 0; i < n - 1; i++)
{
minIndex = i;
for (int j = i; j < n; j++)
{
if (a[minIndex] > a[j])
minIndex = j;
}
if (minIndex != i)
{
temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
}
//插入排序
public void insert_sort(int[] a)
{
int n = a.Length;
int temp;
int j;
for (int i = 1; i < n; i++)
{
temp=a[i];
for (j = i - 1; j >= 0 && temp < a[j]; j--)
{
//if (temp < a[j])
// {
a[j + 1] = a[j];
// }
//else
//{
// // a[j + 1] = temp;
// break;
//}
}
// if (j < 0)
// {
a[j + 1] = temp;
// }
}
}
2009-06-14
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人