软考程序员算法实例Word格式.docx
- 文档编号:3109107
- 上传时间:2023-05-01
- 格式:DOCX
- 页数:18
- 大小:20.69KB
软考程序员算法实例Word格式.docx
《软考程序员算法实例Word格式.docx》由会员分享,可在线阅读,更多相关《软考程序员算法实例Word格式.docx(18页珍藏版)》请在冰点文库上搜索。
elseif(k>
right;
printf(“right\n”);
/*printf(“aaaaaaaaaaaaaaaaaa”);
*/
pre=(node*)malloc(sizeof(node));
pre->
if(temp->
key>
k)
temp->
left=pre;
else
right=pre;
PNODcreat()
PNODT=NULL;
intk;
printf(“pleaseinputnumber=”);
scanf(“%d”,&
k);
while(k)
inster(&
T,k);
printf(“\nagain=”);
returnT;
voidfound(PNODt)
PNODpp;
pp=t;
while(pp)
printf(“pp->
key=%dpp->
left=%dpp->
right=%d”,pp->
key,pp->
left->
right->
printf(“\n”);
found(pp->
left);
printf(“]”,pp->
/*printf(“pp->
left=%dandpp=%d”,pp->
left,pp);
getchar();
right);
main()
printf(“***************\n”);
T=creat();
found(T);
全排列的递归算法
usingSystem;
namespaceTotalSort
/**////
///全排列的递归算法
///
classClass1
///应用程序的主入口点。
[STAThread]
staticvoidMain(string[]args)
//char[]s="
abcdefghijklmnopqrstuvwxyz"
.ToCharArray();
char[]s="
abcde"
TotalSort(s,0);
Console.WriteLine("
\n\n总数:
{0}"
resultCount);
Console.ReadLine();
staticintresultCount=0;
publicstaticvoidTotalSort(char[]list,intstart){
intend=list.Length-1;
if(start==end){
resultCount++;
Console.WriteLine(list);
else{
for(inti=start;
i<
=end;
i++){
char[]temp=newchar[list.Length];
list.CopyTo(temp,0);
chartempc=temp[start];
temp[start]=temp[i];
temp[i]=tempc;
TotalSort(temp,start+1);
C语言中trim的实现
用ATL写了个COM,不支持MFC,所以无法用CString,但支持C编码,遇到字符串(字符数组),想去掉字符串中的空格,C下没有TRIM函数,找又没找到,几行代码自己写吧。
往后大家万一遇到用着也方便。
说明:
1.seps是需要去除的字符数组,可以有几个字符,也可以一个。
这里是空格,最常用的。
2.参数也很简单,第一个是结果数组指针,第二个是原字符数组指针,第三个是需要去掉的字符数组指针。
返回的是结果数组指针。
源代码:
#include“stdafx.h”
charseps[] =“”;
char*trim(char*desc,char*src,char*seps);
intmain(intargc,char*argv[])
charszResult[1024]=“”;
memset(szResult,0,1024);
charstrtemp[]=“abcdef”;
printf(“%s
Tokens:
”,strtemp);
trim(szResult,strtemp,seps);
printf(“result:
%s(ok!
)
”,szResult);
return0;
char*trim(char*desc,char*src,char*seps)
char*token=NULL;
/*Establishstringandgetthefirsttoken:
*/
token=strtok(src,seps);
while(token!
=NULL)
/*Whiletherearetokensin“string”*/
printf(“%s
”,token);
strcat(desc,token);
/*Getnexttoken:
token=strtok(NULL,seps);
returndesc;
快速排序算法
lovethea8585人关注Thea更新:
2011/4/9查看评论(0)
给我发信息免费做试题学习交流
代码:
voidQuickSort(intlow,inthigh,int*array)
intpos;
if(low{
pos=SPLIT(low,high,array);
//以array[low]进行划分,pos最为划分点
//前一部分后一部分,反之
QuickSort(low,pos-1,array);
//对划分后的前一部分递归处理
QuickSort(pos+1,high,array);
//对划分后的后一部分递归处理
intSPLIT(intlow,inthigh,int*array)
inttemp=array[low];
//用temp来记录划分数
while(low{
while(array[high]>
temp&
&
low寻找小于temp的数
high--;
if(low==high)
break;
array[low]=array[high];
low++;
while(array[low]寻找大于temp的数
array[high]=array[low];
array[low]=temp;
//最终low=high作为划分点,并将划分数存于array[low]
returnlow;
思想:
就是你从数组中任取一个元素p(可随机取,现在以取第一个为例);
以P作为主元,对数组进行划分,前一部分小于P,后一部分大于p;
最后划分处存储p;
然后分别对划分后的前一部分和后一部分递归调用;
算法平均时间复杂度:
O(nlogn)。
C语言二路归并排序
写了个二路归并的归并排序小代码,直接贴上来:
/*
file:
quick.cpp
author:
*/
#include<
iostream>
usingnamespacestd;
voidMerge(inta[],intlow,intmid,inthigh,intb[]);
voidMSort(inta[],intlow,inthigh,intb[]);
voidmain()
{
inta[]={4,5,9,10,51,6,46,36,6,56,67,45,36};
intb[13];
MSort(a,0,12,b);
for(inti=0;
i<
13;
i++)
cout<
<
b[i]<
"
"
;
endl;
for(intj=0;
j<
j++)
a[j]<
}
voidMerge(inta[],intlow,intmid,inthigh,intb[])
inti=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(i<
=mid)
a[k]=a[i];
while(j<
=high)
a[k]=a[j];
j++;
voidMSort(inta[],intlow,inthigh,intb[])
if(low==high)
b[low]=a[low];
intmid=(low+high)/2;
MSort(a,low,mid,b);
MSort(a,mid+1,high,b);
Merge(a,low,mid,high,b);
朴素字符串匹配算法
作为最原始的字符串匹配算法,它的时间复杂度是O((n-m+1)m)
#include\"
stdio.h\"
//计算字符串的长度
intLength(char*s)
intcount=0;
while(*s++!
=\\\0\)
count++;
returncount;
//字符串匹配
voidNaiveStringMatching(char*t,char*p)
intn=Length(t);
intm=Length(p);
if(n<
M)<
p>
printf(\"
Error:
ThePislongerthanT!
\\n\"
);
boolfind=true;
ThestringTis%s\\n\"
t);
ThestringPis%s\\n\"
p);
for(ints=0;
s<
=n-m;
s++)
find=true;
M;
I++)<
if(t[s+i]!
=p[i])
find=false;
if(find)
Patternoccurswithshift:
%d\\n\"
s+1);
intmain()
chart[]=\"
abcdebcg\"
charp[]=\"
bcdebcg\"
NaiveStringMatching(t,p);
二路归并排序算法
写了个二路归并的归并排序小代码,直接贴上来
/*
quick.cpp
*/
voidmain()
i++)
j++)
voidMerge(inta[],intlow,intmid,inthigh,intb[])
=high))
=a[j])
=mid)
=high)
voidMSort(inta[],intlow,inthigh,intb[])
矩阵求逆算法
/**
*求矩阵A的逆矩阵Ai
*@paramA源矩阵
*@paramAi逆矩阵
*@paramsize矩阵的大小
*@return求解成功返回非零值,失败返回零
intInverseMatrix(double**Ai,double**A,intsize)
inti,j;
double*b,*x;
b=(double*)malloc(sizeof(double)*size);
x=(double*)malloc(sizeof(double)*size);
for(i=0;
size;
i++)
memset(b,0,sizeof(double)*size);
b[i]=1;
if(!
LinearEquation(A,x,b,size))
free(b);
free(x);
for(j=0;
j<
j++)
Ai[j][i]=x[j];
return1;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序员 算法 实例