小算法:汉诺塔、冒泡法

汉诺塔:

// Hanoi.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 
//#include 
//#include 

using namespace std;

const int LEN=20;

long int count=0;

void Move(int n,char A,char B,char C)
{
	
	if(n==1)
	{
		cout << n << ": " << A << "--->" << C << " count=" << count++ << endl;
	}
	else
	{
		Move(n-1,A,C,B);
		cout << n << ": " << A << "--->" << C << " count=" << count++ << endl;
		Move(n-1,B,A,C);
	}

}

int main(void)
{
	char A='A',B='B',C='C';
	cout << "begin:=======================";
	Move(LEN,A,B,C);
	system("pause");
	
	return 0;
}


冒泡法:

// BubbleSort.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 

using namespace std;

const int LEN=5;

void show(int* a,int length)
{
	for(int i=0;ia[j+1])
			{
				//cout << "a[" << j << "]=" << a[j] << " a[" << j+1 << "]=" << a[j+1] << endl;
				tmp=a[j];
				a[j]=a[j+1];
				a[j+1]=tmp;
				//cout << "a[" << j << "]=" << a[j] << " a[" << j+1 << "]=" << a[j+1] << endl;
			}
			show(a,LEN);
			//cout << "  i=" << i <



你可能感兴趣的:(C&C++)