【名词解释】
第一行输入一个整数
代表给定的单词个数。
此后
行,每行输入一个长度
,由大小写字母构成的字符串
,代表一个单词。
一共
行,每行输出一个字符串,代表排序后的结果。第一行输出字典序最小的单词。
11 cap to cat card two too up boat boot AA Aa
AA Aa boat boot cap card cat to too two up
#include <stdio.h> #include<string.h> void sort(char strings[][200],int n){ for(int i=0;i<n-1;i++){ for(int j=0;j<n-1-i;j++){ if(strcmp(strings[j],strings[j+1])>0){ char tmp[200]; strcpy(tmp,strings[j]); strcpy(strings[j],strings[j+1]); strcpy(strings[j+1],tmp); } } } return; } int main() { int n; scanf("%d",&n); getchar();//清空输入输出缓冲区 char arr[n][200]; for(int i=0;i<n;i++){ fgets(arr[i],200,stdin); } sort(arr,n); for(int i=0;i<n;i++){ printf("%s",arr[i]); } return 0; }
#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> // int compar(const void* a, const void* b) { // return strcmp((const char*)a, (const char*)b); // } int main() { uint32_t size = 0; scanf("%d",&size); char str[1001][101]; char tmp[101]; for(int i = 0; i < size; i++) { scanf("%s",str[i]); } // qsort(str, size, sizeof(str[0]), compar); for(int i = 0; i < size - 1; i++) { for(int j = 1; j < size - i; j++) { if(strcmp(str[j-1], str[j]) > 0) { strcpy(tmp, str[j-1]); strcpy(str[j-1], str[j]); strcpy(str[j], tmp); } } } for(int i = 0; i < size; i++) { printf("%s\n",str[i]); } return 0; }
#include <stdio.h> #include <string.h> int main() { int n; scanf("%d", &n); char str[100][1000]; for (int j = 0; j < n; j++) { scanf("%s", str[j]); } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (strcmp(str[j], str[j + 1]) == 1) { char temp[1000]; strcpy(temp, str[j + 1]); strcpy(str[j + 1], str[j]); strcpy(str[j], temp); } } } for (int i = 0; i < n; i++) { printf("%s\n", str[i]); } }本地IDEV C运行成功,提交到在线测试里面就不行了,有没有大佬看一下
#include "string.h" #include "stdio.h" //注意:题里提到字符串长度小于等于100,但我的二维数组设定后一维度为100时则发生段错误,设为1000则没有这个问题,不知道咋回事。 int main() { int n,i,a = 1; scanf("%d",&n); char str[1000][1000] = {},temp[1000] = {}; for(i = 0; i < n; i++) { scanf("%s",str[i]); } for(i = 0; i < n; i++) { for(a = 1;a < n-i;a++) { if(strcmp(str[i],str[i+a]) > 0 ) { strcpy(temp, str[i]); strcpy(str[i], str[i+a]); strcpy(str[i+a], temp); } } } for(i = 0;i<n;i++) { printf("%s\n",str[i]); } }
#include <stdio.h> #include <stdlib.h> #include <string.h> int compar(const void* a, const void* b) { return strcmp((const char*)a, (const char*)b); } int main() { int n, i = 0; scanf("%d", &n); char word[1000][101]; while (scanf("%s", word[i++]) != EOF) {} qsort(word, n, sizeof(word[0]), compar); for (int i = 0; i < n; i++) { printf("%s\n", word[i]); } return 0; }
# include <stdio.h> # include<string.h> # include<stdlib.h> //决定用字符串数组来做 定义字符串结构体,定义结构体数组;通过比较排序; int main() { int n,i = 0,j=0; char *p[1000],*temp; scanf("%d",&n); for (i=0; i<n; i++) {p[i] = (char *)malloc(101);scanf("%s",p[i]);} for(i = 0; i<n-1; i++){ for(int k = i+1; k<n; k++){ if(strcmp(p[i], p[k]) >0 ){temp = p[i];p[i] = p[k];p[k] = temp;} } printf("%s\n",p[i]);//puts(p[i]); } printf("%s\n",p[i]); return 0; }
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { int i,j,n; char *tmp; scanf("%d", &n); char *str[n]; //接收字符串 for(i = 0; i < n; i++) { str[i] = (char*)malloc(101); memset(str[i], 0, 101); scanf("%s", str[i]); } //排序 for(i = 0; i < n; i++) { for(j = i + 1; j < n; j++) { if (strcmp(str[i], str[j]) > 0) { tmp = str[i]; str[i] = str[j]; str[j] = tmp; } } } //输出 for(i = 0; i < n; i++) { printf("%s\n", str[i]); free(str[i]); } return 0; }
#include <stdio.h> #include <string.h> //思路:二维数组 & 选择排序法 //将每个字符串看作一个具有大小的数,存入二位数组,采用冒泡/选择排序法实现a~z的排序 int main() { int n, i = 0, j; char dir[1001][101] = { 0 }, mid[101] = { 0 }; scanf("%d", &n); while (~scanf("%s", dir[i])) { i++; } for (i = 0; i <= n - 1; i++) { for (j = i + 1; j <= n - 1; j++) { if (strcmp(dir[i], dir[j]) <= 0) {//"比大小"的行为使用strcmp函数实现,应当注意条件(注意该函数的返回值) strcpy(mid, dir[i]); strcpy(dir[i], dir[j]); strcpy(dir[j], mid); } } } while (n) { printf("%s\n", dir[--n]); } return 0; }
#include <stdio.h> #include <string.h> int main() { char arr[1000][101]={}; int flag[1000]={}; int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%s",arr[i]); flag[i]=1; } char maxArr[101] ={'z'+1}; int minLoc=-1; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(strcmp(arr[j],maxArr)<0&&flag[j]){ strcpy(maxArr, arr[j]); minLoc=j; } } printf("%s\n",arr[minLoc]); flag[minLoc]=0; minLoc=-1; strcpy(maxArr, "{"); } return 0; }每次循环找到最小的值进行输出即可,几道相似的排序题分别使用不同的方式进行处理,还可以按照qsort进行处理,这样或许更快
int mycompare(void * a, void *b){ return (strcmp((char *)a,(char *)b)); //strcmp是字符串比较函数 } int main(void){ int i,n = 0; scanf("%d",&n); //获取数组的一维坐标大小范围 char input[n][101]; //对数组的一维坐标不要直接设定成1000,否则会报内存不足 for(i=0;i<n;i++){ scanf("%s",&input[i]); } qsort(input,n,sizeof(input[0]),mycompare); //直接调用stdlib.h的比较函数 for(i=0;i<n;i++){ printf("%s\n",input[i]); } return 0; }