题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int contain(char *arry, char c,int len)
{
int i = 0;
for(i = 0; i < len; i++)
{
if(arry[i] == c)
{
arry[i] = '!';
return 1;
}
}
return 0;
}
int compare(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main() {
int i;
int arrry_cnt;
char arry[1000][11];
char key[11];
char tagrtlist[1000][11];
int keylen;
int outcont;
scanf("%d",&arrry_cnt);
for(i = 0; i < arrry_cnt; i++)
{
scanf("%s",arry[i]);
}
scanf("%s",key);
keylen = strlen(key);
scanf("%d",&outcont);
int n = 0;
int j;
char test[11];
for(i = 0; i < arrry_cnt; i++)
{
if(keylen == strlen(arry[i]))
{
if(strcmp(key,arry[i]) != 0)
{
//strcpy(tagrtlist[n++], key[i]);
j = 0;
strcpy(test,arry[i]);
for (j = 0; j < keylen; j++)
{
if(contain(test,key[j],keylen) == 0)
{
break;
}
}
if(j == keylen)
{
//n++;
strcpy(tagrtlist[n++], arry[i]);
}
}
}
}
char t[11];
for(i = 0; i < n; i++)
{
for(j = 0; j < n - i -1; j++)
{
if(strcmp(tagrtlist[j], tagrtlist[j+1]) > 0)
{
strcpy(t,tagrtlist[j]);
strcpy(tagrtlist[j],tagrtlist[j+1]);
strcpy(tagrtlist[j + 1],t);
}
}
}
//qsort(tagrtlist,n,sizeof(char*),compare);
printf("%d\n%s",n,tagrtlist[outcont-1]);
}
查看12道真题和解析