| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:为什么一直runtimeError,求大神帮忙~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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator