C/C++ 寻找最短字符串:高效算法实现与代码解析
#include <stdio.h> #include <string.h>
void find(char *name[], int n, int *p);
int main() { char *name[100]; int n = 0; printf('请输入字符串,以####结束:\n'); while (1) { char *str = (char *) malloc(sizeof(char) * 7); scanf('%s', str); if (strcmp(str, '####') == 0) { free(str); break; } name[n++] = str; } int p; find(name, n, &p); printf('最小的字符串为:%s', name[p]); for (int i = 0; i < n; i++) { free(name[i]); } return 0; }
void find(char *name[], int n, int *p) { *p = 0; for (int i = 1; i < n; i++) { if (strcmp(name[i], name[*p]) < 0) { *p = i; } } }
原文地址: https://www.cveoy.top/t/topic/ojLd 著作权归作者所有。请勿转载和采集!