33,028
社区成员
发帖
与我相关
我的任务
分享#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char a5[5][5]={
'a','b','c','d','e',
'1','2','3','4','5',
'f','g','h','i','j',
'6','7','8','9','0',
'k','l','m','n','o'};
char a4[4][4]={
'1','2','3','4',
'a','b','c','d',
'5','6','7','8',
'e','f','g','h'};
void find(char *a,int m,int f,char *s){
int t,i,j,k;
char *tmp;
char *p;
if(f!=1&&f!=2&&f!=3&&f!=4) return;
tmp=(char*)malloc(sizeof(char)*(m*m+1));
tmp[0]='\0';k=0;
for(t=0;t<m;t++){
if(f==1){
i=t;j=0;
while(1){
tmp[k++]=*(a+i*m+j);
if(i==0) break;
i--;j++;
}
f=2;
}else if(f==2){
j=t;i=0;
while(1){
tmp[k++]=*(a+i*m+j);
if(j==0) break;
i++;j--;
}
f=1;
}else if(f==3){
i=t;j=m-1;
while(1){
tmp[k++]=*(a+i*m+j);
if(i==0) break;
i--;j--;
}
f=4;
}else if(f==4){
i=0;j=m-1-t;
while(1){
tmp[k++]=*(a+i*m+j);
if(j==m-1) break;
i++;j++;
}
f=3;
}
}
for(t=1;t<m;t++){
if(f==1){
i=m-1;j=t;
while(1){
tmp[k++]=*(a+i*m+j);
if(j==m-1) break;
i--;j++;
}
f=2;
}else if(f==2){
j=m-1;i=t;
while(1){
tmp[k++]=*(a+i*m+j);
if(i==m-1) break;
i++;j--;
}
f=1;
}else if(f==3){
i=m-1;j=m-1-t;
while(1){
tmp[k++]=*(a+i*m+j);
if(j==0) break;
i--;j--;
}
f=4;
}else if(f==4){
i=t;j=0;
while(1){
tmp[k++]=*(a+i*m+j);
if(i==m-1) break;
i++;j++;
}
f=3;
}
}
tmp[k++]='\0';
printf("[%s] match [%s]: ",s,tmp);
p=strstr(tmp,s);
if(p==0) printf("not match!\n");
else printf("pos %d\n",p-tmp+1);
free(tmp);
}
int main(){
char s[100];
sprintf(s,"d3g");
find(&a5[0][0],5,1,s);
find(&a5[0][0],5,2,s);
sprintf(s,"i0o9h2a");
find(&a5[0][0],5,3,s);
find(&a5[0][0],5,4,s);
sprintf(s,"b5e6c");
find(&a4[0][0],4,1,s);
find(&a4[0][0],4,2,s);
find(&a4[0][0],4,3,s);
find(&a4[0][0],4,4,s);
return 0;
}[d3g] match [ab1f2cd3g6k7h4e5i8lm9j0no]: pos 7
[d3g] match [a1bc2f6g3de4h7kl8i5j9mn0o]: not match!
[i0o9h2a] match [ed5j4cb3i0o9h2a1g8nm7f6lk]: pos 9
[i0o9h2a] match [e5dc4j0i3ba2h9on8g1f7ml6k]: not match!
[b5e6c] match [12a5b34c6ef7d8gh]: not match!
[b5e6c] match [1a23b5e6c4d7fg8h]: pos 5
[b5e6c] match [43d8c21b7hg6a5fe]: not match!
[b5e6c] match [4d32c8h7b1a6gf5e]: not match!
Press any key to continue