约瑟夫题目,c 排序算法,c 非递回回溯法[宝典]约瑟夫题目,c 排序算法,c 非递回回溯法[宝典]
#约瑟夫问题,c#排序算法,C#非递归回溯法
===============================================================================
===============
用C#实现约瑟夫问题
using System;
namespace heut
{
class Class1
{
public static void Main()
{
int[] a = new int[17];
i...
约瑟夫题目,c 排序算法,c 非递回回溯法[宝典]
#约瑟夫问题,c#排序算法,C#非递归回溯法
===============================================================================
===============
用C#实现约瑟夫问题
using System;
namespace heut
{
class Class1
{
public static void Main()
{
int[] a = new int[17];
int sum = 17;//共有8个人;
int k = 3;//每次数到3就退出;
int count = 0;//
退出的人数;
int i = 0, j = 0;
for (int m = 0; m < sum; m++)
a[m] = 1;//数组元素全部初始化为1;
while (count < sum - 1)
{
if (a[i] != 0)
j++;
if (j == k)
{
a[i] = 0;
count++;
j = 0;//重新开始,找下一个值!
}
i++;
if (i == sum)
i = 0;//实现环(即围成一圈);
}
for (int m = 0; m < sum; m++)
if (a[m] != 0)
Console.Write(m + 1 + "\n");
Console.ReadKey();
}
}
===============================================================================
===============
一、冒泡排序(Bubble) using System;
namespace BubbleSorter {
public class BubbleSorter {
public void Sort(int[] list) {
int i,j,temp;
bool done=false;
j=1;
while((j
list[i+1]) {
done=false;
temp=list[i];
list[i]=list[i+1];
list[i+1]=temp;
}
}
j++;
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
BubbleSorter sh=new BubbleSorter(); sh.Sort(iArrary);
for(int m=0;m0)&&(list[j-1]>t)) {
list[j]=list[j-1];
--j;
}
list[j]=t;
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};
InsertionSorter ii=new InsertionSorter();
ii.Sort(iArrary);
for(int m=0;m0;inc/=3)
{
for(int i=inc+1;i<=list.Length;i+=inc) {
int t=list[i-1];
int j=i;
while((j>inc)&&(list[j-inc-1]>t)) {
list[j-1]=list[j-inc-1];
j-=inc;
}
list[j-1]=t;
}
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
ShellSorter sh=new ShellSorter(); sh.Sort(iArrary);
for(int m=0;m=i; j--)
{
//交换条件
if(R[j+1] empLhs.salary) ? true : false;
}
}
static void Main(string[] args)
{
Employee[] employees =
{
new Employee("Bugs Bunny", 20000),
new Employee("Elmer Fudd", 10000),
new Employee("Daffy Duck ", 25000),
new Employee("Wiley Coyote", (decimal)100000.38),
new Employee("Foghorn Leghorn", 23000),
new Employee("RoadRunner", 50000),
};
CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater);
BubbleSorter.Sort(employees, employeeCompareOp);
foreach (Employee employee in employees)
{
Console.WriteLine(employee.ToString());
}
Console.ReadKey();
}
}
}
==============================================================================================
非递归回溯法
using System;
using System.Collections.Generic; using System.Text;
/*
* 回溯是一种比较有用的算法,其思想比较简单,也是源于生活。
* 若某种猜测行不通,则撤回,并用另一种猜测替换它,这种反向折回并试探新步骤的策略称为回溯。 * 其经典实例就是八皇后的问题。在国际象棋中棋盘有八行八列,其64个方格,其中最强大的莫过于皇后, * 可以攻击同一行,同一列,同一对角线的棋子,八皇后问题要求给出解决,八个皇后放在棋盘上,并且她们无法相互功击
* 回溯法经常应用于模拟人类思维,即人工智能
*/
namespace 非递归回溯
{
class Program
{
const int SIZE = 8;
static int[] Queen = new int[SIZE]; //皇后的位置
static int sum = 0; //解决方案的数量
static void Main(string[] args)
{
Trial();
}
static void Trial()
{
int row = 0, column = 0;
Queen[0] = -1;
while (true)
{
for (column = Queen[row] + 1; column < SIZE; column++)
{
//判断新皇后的位置是否合法
if (!IsConfine(row, column))
{
break;
}
}
//没有合适的位置放皇后,回溯到上一行
if (column == SIZE)
{
//如果没有合适的位置放皇后并且已回溯到第一行
//退出循环
if (0 == row)
{
break;
}
//回溯到上一行
Queen[row] = -1;
row--;
}
else //有合适的位置放皇后
{
Queen[row] = column;
//进入到下一行
row++;
//如果没有放完皇后,则继续循环
if (row < SIZE)
{
Queen[row] = -1;
}
else //得到一种解决方案
{
Console.WriteLine("第" + (++sum) + "种方案");
Write();
//继续回溯寻找下一种解决方案
row = SIZE - 1;
}
}
}
}
//判断皇后的放置是否合法
static bool IsConfine(int row, int column)
{
for (int i = 0; i < row; i++)
{
if (Queen[i] == -1)
{
break;
}
if (column == Queen[i] || column == Queen[i] + row - i || column == Queen[i] - row + i)
{
return true;
}
}
return false;
}
//输出解决方案
static void Write()
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (Queen[i] == j)
{
Console.Write("?");
}
else if (i % 2 == 0 && j % 2 == 0 || i % 2 != 0 && j % 2 != 0)
{
Console.Write("?");
}
else
{
Console.Write("?");
}
}
Console.WriteLine();
}
Console.WriteLine();
Console.ReadKey();
}
}
}
本文档为【约瑟夫题目,c 排序算法,c 非递回回溯法[宝典]】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。