标签:
Description
Input
Output
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6
代码提交结果:compile error
困惑,不知道为什么
#include <stdio.h>
#include <stdlib.h>
void merging(int *list1,int list1_size,int *list2,int list2_size,int num,int *count)
{
int i,j,k,m;
i=j=k=0;
int temp[num],a,b;
while (i<list1_size && j<list2_size) {
a=list1[i];
b=list2[j];
if(list1[i]<list2[j])
{
temp[k++]=list1[i++];
}else{
temp[k++]=list2[j++];
(*count)=(*count)+list1_size-i;
}
}
while (i<list1_size) {
temp[k]=list1[i];
k++;
i++;
}
while (j<list2_size) {
temp[k]=list2[j];
k++;
j++;
}
for (m=0; m<list1_size+list2_size; m++) {
list1[m]=temp[m];
}
}
void sort(int *p,int num,int *count)
{
if (num>1) {
int *list1=p;
int list1_size=num/2;
int *list2=p+num/2;
int list2_size=num-list1_size;
sort(list1, list1_size,count);
sort(list2, list2_size,count);
merging(list1,list1_size,list2,list2_size,num,count);
}
}
int main() {
int num;
int *p;
int i;
int *count;
int result;
while (scanf("%d",&num)&&num!=0) {
result=0;
count=&result;
p=(int *)malloc(num*sizeof(int));
for (i=0; i<num; i++) {
scanf("%d",&p[i]);
}
sort(p,num,count);
printf("%d\n",(*count));
}
return 0;
}
0
标签:
原文地址:http://www.cnblogs.com/candybread/p/5338442.html