Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:为什么一直runtimeError,求大神帮忙~

Posted by donghao526 at 2014-05-19 19:53:37 on Problem 1007
In Reply To:为什么一直runtimeError,求大神帮忙~ Posted by:xueqiang_xide at 2014-05-16 18:37:46
> #include<stdio.h>
> #include<string.h>
> 
> int checkSort(char *str,int length){
>     int i,j,count = 0;
>     for(i=0; i<length-1; i++){
>         for(j=i+1; j<length; j++){
>             if((str[i]-str[j])>0){
>                 count++;
>             }
>         }
>     }
>     return count;
> }
> 
> 
> void sortStr(int *count,int left,int right,char **str){
>     int mid = count[left];
>     int i=left;
>     int j=right;
>     int temp;
>     char *c;
>     if(right-left<1)
>         return;
>     do{
>         while(i<right&&count[i]<mid){
>             i++;
>         }
>         while(j>left&&count[j]>mid){
>             j--;
>         }
>         if(i<=j){
>             temp = count[i];
>             count[i]=count[j];
>             count[j]=temp;
>             c = str[i];
>             str[i]=str[j];
>             str[j]=c;
>             i++;
>             j--;
>         }
>     }while(i<=j);
>     if(i<right){
>         sortStr(count,i,right,str);
>     }
>     if(j>left){
>         sortStr(count,left,j,str);
>     }
> }
> 
> 
> int main(){
>     int n,m;
>     char **str;
>     int *count;
>     int i;
>     if(scanf("%d%d",&n,&m)!=EOF){
>         str = malloc(sizeof(char)*m);  /*str为字符串数组,应该申请内存时为str = malloc(sizeof(char*)*m)*/, char为1个字节,char *为4个字节,你这样做会使内存无法回收完整
>         count = malloc(sizeof(int)*m);
>         for(i=0; i<m; i++){
>             str[i] = malloc(sizeof(char)*n);
>             scanf("%s",str[i]);
>             count[i]=checkSort(str[i],n);
>         }
> 
>         sortStr(count,0,m-1,str);
>         for(i=0; i<m; i++){
>             printf("%s\n",str[i]);
>             free(str[i]);
>         }
> 
>         free(str);
>     }
>     return 0;
> }
申请内存时候的指针书写有误

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator