汉诺塔问题
#include<stdio.h>
void hanoi(int n,char a,char b,char c){
if(1==n){
printf("%c->%c\n",a,c);
}else{
hanoi(n-1,a,c,b);
printf("%c->%c\n",a,c);
hanoi(n-1,b,a,c);
}
}
int main(){
hanoi(3,'A','B','C');
}
字符串交换问题:字符串以A开头和以K结尾的单词进行交换
如Before: I AM A OK BOY VERY OK
After:I OK OK A BOY VERY AM
#include<stdio.h>
#include<string.h>
#define N 60
int main(){
char orginal[N]="I AM A OK BOY VERY OK";
char *p,*delim=" ",*r[N]={0};
int k=0,i,j,len;
printf("\nOrginal:\n");
p=strtok(orginal,delim);
printf("%s\t",p);
r[k++]=p;
while((p=strtok(NULL,delim))){
printf("%s\t",p);
r[k++]=p;
}
///change process
i=0,j=k-1;
while(i<=j){
len=strlen(r[j]);
if(r[i][0]!='A')
i++;
if(r[j][len-1]!='K')
j--;
if(r[i][0]=='A' && r[j][len-1]=='K'){
p=r[i];
r[i]=r[j];
r[j]=p;
i++;
j--;
}
}
printf("\nResult:\n");
for(i=0;i<k;i++){
printf("%s\t",r[i]);
}
printf("\n");
return 0;
}