解题思路:

                  1、第一步:用gets()方法的到输入的字符串。

                  2、第二步:(此处为难点)将字符串中的数字拆解出来,放到新的定义的数组当中。

                  3、第三步:对定义的数组进行选择排序。

                  对第二步详解:假设输入的例子是:asdnnn123,58 oo 95或者是按照空格隔开的如45 52 1 455 354 369 21 54 6这样的例子都可以用第二步骤来进行拆解。定义temp的作用是。通过temp来表记是否一个数字后面是否还有数字,如123,当第一个是1的时候temp会修改为1,接着得到12,遇到3时temp依旧是1,得到123,当遇到不是数字的字符时,就会得到temp等于0.

代码解释:a[i]=b[j]-'0';这一句是将char型的数字改为int型数字。

                  对第三步详解:选择排序就是用两个for循环来解决问题,两层for循环得到数组中最小的数字的位置下表,记录在k中,然后将a[k]替换到前面就好。

参考代码:


                                                                #include

                                                                int main(){

                                                                 int a[10],i=0,j=0,temp=0,p,q,k,temp1,r;

                                                                 char b[100];

                                                                 gets(b);//第一步

                                                                 while(b[j]!='\0'){

                                                                    if(b[j]>='0'&&b[j]<='9'){

                                                                         if(temp==0){

                                                                             temp=1;

                                                                             a[i]=b[j]-'0';

                                                                             i++;

                                                                         }else{

                                                                         a[i-1]=a[i-1]*10+b[j]-'0';

                                                                        }

                                                                     }else{

                                                                         temp=0;

                                                                         }

                                                                         j++;

                                                                 }//第二步结束

                                                                 for(p=0;p<i;p++){

                                                                 k=p;

                                                                 for(q=p+1;q<i;q++){

                                                                     if(a[k]>a[q])k=q;

                                                                 } 

                                                                 temp1=a[p];

                                                                 a[p]=a[k];

                                                                 a[k]=temp1;

                                                                 }//第三步结束

                                                                 for(r=0;r<i;r++){

                                                                     printf("%d\n",a[r]);

                                                                 }

                                                                 return 0;

                                                                }


点赞(0)
 

0.0分

1 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论