pipi666


私信TA

用户名:uq_65565366413

访问量:774

签 名:

等  级
排  名 1219
经  验 2976
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:

                1.处理 0 的个数,遍历到0 的时候,判断 左右是否为 数字,为数字 直接continue,不为数字 直接 zero++, zero 表示是0的个数

                2.用count变量 记录0 以外的数字 的个数。之后malloc 申请数组空间 

                3.处理1-9,遍历到1-9范围的数字直接加到temp变量中,遍历到数字以外的 字符时候(此时temp即为字符中的数字),将temp加入到res数组当中,

                4.qsort之后,遍历输出即可

注意事项:
                字符为0的情况,单独处理,测试用例里面 貌似没有0 的情况
参考代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmp(const void *a,const void *b){
    return *(int*)a - *(int*)b;
}

int main()
{
	
	
	char str[300];
	scanf("%s",str);
	int index=0,temp=0,count=0;
	
	for(int i=0;str[i]!='\0';i++){
	    if( str[i]>='0' && str[i]<='9' ){
	        temp=1;
	        break;
	    }
	}
	if(temp==0){
	    printf("0");
	    return 0;
	}
	temp=0;
	
	int zero=0;
	int j=0;
	for( j=0;j<strlen(str)-1;j++){
	    if(str[j]=='0'){
	        if(str[j-1]>='1' && str[j]<='9' && j-1>0){
	            continue;
	        }else if(str[j+1]>='1' && str[j]<='9' && j+1<strlen(str)){
	            continue;
	        }else{
	            zero++;
	        }
	        
	           
	        }
	    }
	
	
	
	
	for(int i=0;str[i]!='\0';i++){
	    
	    
	   if( str[i]>='0' && str[i]<='9' ){
	       temp*=10;
	       temp+=(str[i] - 48);
	   }
	   if( !(str[i]>='0' && str[i]<='9') && temp!=0 ){
	       count++;
	       //printf("%d ",temp);
	       temp=0;
	   }
	   if(i==strlen(str)-1 && (str[i]>='0' && str[i]<='9')){
	       count++;
	       temp=0;
	   }
	    
	}
	
	int *res=malloc(sizeof(int ) * (count+zero));
	temp=0;
		for(int i=0;i<zero;i++)
	    res[index++]=0;
	
	for(int i=0;str[i]!='\0';i++){
	    
	    
	   if( str[i]>='0' && str[i]<='9' ){
	       temp*=10;
	       temp+=(str[i] - 48);
	   }
	   if( !(str[i]>='0' && str[i]<='9') && temp!=0){
	       res[index++]=temp;
	       //printf("%d ",temp);
	       temp=0;
	   }
	   if(i==strlen(str)-1 && (str[i]>='0' && str[i]<='9')){
	       res[index++]=temp;
	       //printf("%d ",temp);
	       temp=0;
	   }
	    
	}
	

	qsort(res,count,sizeof(int) ,cmp);
	
	
	
	for(int i=0;i<count+zero;i++){
	    printf("%d",res[i]);
	    if(i!=count-1)
	        printf(",");
	}
	
	
// 	printf("\ntemp=%d",zero);
	
	
	return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区