大雨


私信TA

用户名:BigRain

访问量:971

签 名:

等  级
排  名 38156
经  验 405
参赛次数 0
文章发表 1
年  龄 0
在职情况 学生
学  校 安徽师范大学
专  业

  自我简介:

解题思路:

                  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分

1 人评分

新上线《蓝桥杯辅导》课程,近五年的蓝桥杯省赛与国赛真题都有,从读题开始理解题意、梳理思路、实现代码再提交评测全过程,可有效提升获奖比例甚至进国赛!课程介绍、试听请猛击这里

  评论区

  • «
  • »