有以下程序 #include <stdio.h> #include <string.h> void fun(char s[ ][10],int n) { char t; int i,j; for(i=0;i<n-1;i+ +) for(j=i+1;j<n;

admin2019-08-10  20

问题 有以下程序
#include <stdio.h>
#include <string.h>
void fun(char s[ ][10],int n)
{ char t; int i,j;
for(i=0;i<n-1;i+ +)
for(j=i+1;j<n;j+ +)
if(s[0]>s[j][0]){t=s[0];s[0]=s[j][0];s[j][0]=t;}
}
main( )
{  char ss[5][10]={"bcc","bbcc","xy","aaaacc","aabcc"};
fun(ss,5); printf("%s,%s\n",ss[0],ss[4]);
}
程序的运行结果是

选项 A、aaaacc,xy
B、bcc,aabcc
C、xy,aaaacc
D、aabcc,bcc

答案C

解析 函数fun(char s[ ][ 10],int n)通过两重for循环语句,比较二维字符数组s[ ][ 10]的每个字符串的首字符大小,如果前一个字符串首字符大于后一个字符串的首字符,则交换这两个字符串的首字符。在主函数中,语句fun( ss,5)的作用是对二维字符数组ss[5][10]的5个字符串的首字符进行从小到大的排序,不影响各个字符串另外的字符。所以排序结束后,5个字符串的第一个字符依次变为a、a、b、b和x;最后输出第1个和第4个字符串。
转载请注明原文地址:https://kaotiyun.com/show/ZgRp777K
0

最新回复(0)