C语言多个索引对二维字符数组排序
2020-06-22 04:33
477 查看
输入
第一行输入国家个数 N
接下来 N 行分别输入字符串 金牌数 银牌数 铜牌数,空格分隔开。
排序
排序要求:金牌数,银牌数,铜牌数,如果全部相同就按照字符串升序排序。
输出
一行一个输出国家名称,金牌数,银牌数,铜牌数
#include <stdio.h> #include <string.h> void swap_str(char s1[],char s2[]){ char temp[80]; strcpy(temp,s1); strcpy(s1,s2); strcpy(s2,temp); } void swap_int(int *i,int *j){ int temp; temp = *i; *i = *j; *j = temp; } int main(){ // input int N; scanf("%d",&N); char name[N][80]; int G[N]; int S[N]; int B[N]; int i; for(i=0;i<N;i++){ scanf("%s %d %d %d",name[i],&G[i],&S[i],&B[i]); } int j; //bubble sort for(i=0;i<N-1;i++){ for(j=0;j<N-1-i;j++){ if(G[j]<G[j+1]) { swap_str(name[j],name[j+1]); swap_int(&G[j],&G[j+1]); swap_int(&S[j],&S[j+1]); swap_int(&B[j],&B[j+1]); } else if(G[j]==G[j+1] && S[j]<S[j+1]) { swap_str(name[j],name[j+1]); swap_int(&G[j],&G[j+1]); swap_int(&S[j],&S[j+1]); swap_int(&B[j],&B[j+1]); } else if(G[j]==G[j+1] && S[j] == S[j+1] && B[j]<B[j+1]) { swap_str(name[j],name[j+1]); swap_int(&G[j],&G[j+1]); swap_int(&S[j],&S[j+1]); swap_int(&B[j],&B[j+1]); } else if(G[j]==G[j+1] && S[j] == S[j+1] && B[j]==B[j+1] && (strcmp(name[j],name[j+1])) > 0) { swap_str(name[j],name[j+1]); swap_int(&G[j],&G[j+1]); swap_int(&S[j],&S[j+1]); swap_int(&B[j],&B[j+1]); } } } // output, use pipeline to do the input // ./test.out < input.txt for(i=0;i<N;i++){ printf("%-10s %-3d %-3d %-3d\n",name[i],G[i],S[i],B[i]); } return 0; }
可供参考的输入。
7 austria 30 89 20 japan 25 89 32 india 29 95 21 russia 30 90 23 america 25 89 32 china 30 100 56 canada 30 89 60
相关文章推荐
- 1214 1159,二维字符数组sort排序
- 《IOS_C语言》一维数组、数组排序、字符数组
- 对二维字符数组排序(2sort和1qsort)
- 【学习ios之路:C语言】一维数组,数组排序,字符数组
- 使用qsort对二维字符数组排序疑难问题调试及解决过程
- 对二维字符数组排序(2sort和1qsort)
- 如何对二维字符数组进行排序
- [C语言]给一个二维字符数组赋值的问题
- C语言二维字符数组的运用—模拟对话
- sort对二维字符数组排序
- Matlab笔记:(入门示例)矩阵和数组、数组索引、文本和字符、调用函数、二维图和三维图
- C语言中二维字符数组的定义和初始化
- C语言中二维字符数组的定义和初始化
- 对二维字符数组排序(2SORT和1QSORT)
- 指针动态创建数组,二维指针处理字符数组,链表对象排序
- C语言中二维字符数组的定义和初始化
- 利用指针对二维字符数组进行排序
- sort对二维字符数组排序
- 对二维字符数组排序(2sort和1qsort)
- C语言使用qsort排序字符串数组中的字符