博文
求全排列问题的算法(2008-11-14 18:19:00)
摘要://全排列问题
/*
设R={r1,r2,...rn}是要进行排列的n个元素.Ri=R-{ri}.集合X中元素的全排列记为
Perm(X).(ri)Perm(X)表示在全排列Perm(X)的每一个排列前加上前缀ri得到的排列
R的全排列可归纳定义如下:
当n=1时,Perm(R)=(r),其中r是集合R中唯一的元素;
当r>1时,Perm(R)由(r1)Perm(r1),(r2)Perm(r2).....(rn)Perm(rn)构成.
依此递归定义,Perm(R)的递归算法如下:
*/
#include <iostream>
#include <cstdlib>
using namespace std;
void swap(int & a,int & b)
{
int temp=a;a=b;b=temp;
}
void Perm(int list[],int k,int m)
{
if(k==m)
{
for(int i=0;i<=m;i++)
&......
单向链表的实现(2008-07-07 19:02:00)
摘要:单向链表的实现
原 作 者:querw
原 出 处:VC在线
代码:http://www.vczx.com/article/file/20040822213844_TestQueue.rar
独立于MFC的单项链表模板,多余的话我就不多说了.有附带功能演示用法.希望能给有同样需求的读者一点帮助.
以下是源代码.(不用cpp文件,使用时只要把文件Queue.h包含到工程中即可)
// Queue.h: interface for the CQueue class.
//
/********************************************************************
created: 2004/08/09
created: 9:8:2004 23:33
file base: queue
file ext: h
author: 阙荣文(北方工业大学计算机2000级)
purpose: 构建一个通用的,独立于MFC的单向链表
我想c++的标准库模板应该也有类似功能的类,但是我还是决定自己实现
这样可以使用我自己喜欢的参数调用方式.
希望对用同样需求的编程爱好者有用
*********************************************************************/
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_QUEUE_H__57C46373_D485_43F2_998C_D3EA4984E59A__INCLUDED_)
#define AFX_QUEUE_H__57C46373_D485_43F2_998C_D3EA4984E59A__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
template <......
练习:约瑟夫环(顺序实现)(2006-06-26 00:53:00)
摘要:/*
Name: josh.cpp
Copyright:
Author: zyq
Date: 25-06-06 10:14
Description: 输出约瑟夫环
Dev4.9.9.2
*/
#include <iostream>
using namespace std;
void josh(int n,int k)
{
int j,s,*A;
A=new int[n];
for(int i=0;i<n;i++)
A[i]=1; /*1为是否输出标志*/
j=0;
for(int i=0;i<n;i++)
{
int count=0;
wh......
练习:差分方程求值(用递归的方法)(2006-06-19 02:55:00)
摘要:差分方程求值(用递归的方法)
//计算Pn(x)
/* 1 n=0
Pn(x)= x n=1
(2x-1)*x*Pn-1(x)-(n-1)*Pn-2(x)/n n>1
*/
int pn(int x,int n)
{
int res;
if(n==0)
res=1;
else if(n==1)
res=x;
else
{res=(2*x-1)*x*pn(x,n-1)-(n-1)*pn(x,n-2)/n;}
return res;
}
......
练习:差分方程求值(用递归和非递归两种方法)(1)(2006-06-19 02:52:00)
摘要:差分方程求值(用递归和非递归两种方法)(1)
/*习题6
写出Fn(x)的递归函数
1 n=0;
Fn(x)= 2x n=1;
2xFn-1(x)-2(n-1)Fn-2(x) n>1
*/
#include <stack>
using namespace std;
typedef stack<float> floats;
//递归形式
float fn(float x,int n)
{
float ......
练习:将递归函数(testk())改写成非递归(test())(2006-06-19 02:37:00)
摘要:#include <cstdlib>
#include <iostream>
#include <stack>
using namespace std;
typedef stack<int> ints;
typedef deque<char> chars;
int pn(int x,int n);
//习题1 将递归函数(testk())改写成非递归(test())
void testk (int &sum)
{
int x;
cin>>x;
if(x==0) sum=0;
else{testk(sum);sum+=x;}
cout<<sum<<endl;
}
void test(int &sum)
{
ints L;
int x=1;
sum=0;
while(x!=0)
{
cin>>x;
......
练习:求两个正整数m和n的最大公约数(三种方法)(2006-06-19 02:33:00)
摘要:/*
求两个正整数m和n的最大公约数可用如下gcd(m,n)公式表示
m n=0
gcd(m,n)=
gcd(n,m%n) n>0
*/
#include <stack>
using namespace std;
typedef stack<int> ints;
//递归实现
int gcd(int m,int n)
{
int res;
if(n==0)
res=m;
else
res=gcd(n,m%n);
return res;
}
//非递归实现
int gcd2(int m,int n......
练习:识别回文字符串(2006-06-19 02:29:00)
摘要:/*习题4
识别依次读入的一个以字符序列是否为形为序列1&序列2模式的字符序列。
其中序列1和序列2不含&,且序列2为序列1的逆序
a+b&b+a是 a+b&b-a不是
*/
//devc++中通过
//2006.06.18
//Zyq
#include <iostream>
#include <stack>
#include <cstdlib>
using namespace std;
typedef stack<char> chars;
int check(char a[],int n)
{
chars S;
for(int i=0;i<n/2;i++)
S.push(*(a+i));
char t;
int j=1;
int i=n/2+1;
if(*(a+i-1)!='&')
......
练习:顺序表的所有操作(2006-05-31 02:55:00)
摘要:/*
实验一
文件名:SeqList.cpp
该程序参考网上的一个实验要求编写,用于实验线形表的各种操作
在VC6.0中模拟C环境运行
作者:hanshuyujifen
日期:2006.05.29
*/
#include <cstdio>
#include <iostream>
using namespace std;
/* 定义DataType为int类型 */
typedef int DataType;
/*顺序表存储空间的总分配量*/
#define MAXSIZE 100
/* 顺序存储类型 */
typedef struct
{
DataType data[MAXSIZE]; /*存放线性表的数组*/
int length; /* length是顺序表的长度*/
}SeqList;
/* 初始化顺序表 */
SeqList SeqListInit()
{
SeqList L;
L.length=0;
return L;
}
/* 清空顺序表 */
SeqList ListClear(SeqList L)
{
L.length=0;
&nb......
练习:将有序顺序表归并为一个有序顺序表(2006-05-31 02:52:00)
摘要:/*实验2 顺序表其它操作
实验目的
1.进一步掌握在线性表的顺序存储结构上的一些其它操作。
实验内容
程序2
已知两个非递减有序的线性表LA和LB,将LA和LB合并成一个线性表LC,LC也非递减有序。
*/
#include <iostream>
#include <cstdio>
using namespace std;
#define ElemType int
#define MAXSIZE 100
//顺序表的操作
/* 顺序存储类型 */
typedef struct
{ElemType data[MAXSIZE]; /*存放线性表的数组*/
int length; /* length是顺序表的长度*/
}SeqList;
/* 初始化顺序表 */
SeqList SeqListInit( )
{SeqList L;
L.length=0;
return L;
}
/* 求顺序表长度 */
int ListLength(SeqList L)
{return(L.length);}
/* 遍历顺序表 */
void ListTraverse(SeqList L)
{int i;
if(L.length<=0) printf("顺序表为空\n");
&nb......