$ar1= array('one'=>"zero",3=>"one",4=>"two");
排序后是:
Array
(
[0] => one
[1] => two
[2] => zero
)
也就是顺序调整为原来的1、2、0,第二个数组也会按照1、2、0的顺序调整为:
four、five、three同意一楼方法,讲二维数组存到一维数组中,然后用qsort排序,再存到二维数组当中
代码敲了一下,发现C语言快忘光了
#include <stdioh>
#include <stdlibh>
int comp(const void a , const void b){
return (int )b - (int )a; //这是b-a,如果是a-b就是从小到大排序
}
int main(){
int a[3][3];
int b[9];
int n;
int i , j , k;
for(i = 0 , k = 0; i < 3 ; i ++)
for(j = 0 ; j < 3 ; j ++){
scanf("%d",&a[i][j]);
b[k++] = a[i][j]; // k ++别看漏
}
qsort(b,9,sizeof(int),comp);//qsort函数很简单的,看看就会,不怕麻烦,comp函数是指定排序是从小到大还是从大到小
for(i = 0 , k = 0; i < 3 ; i ++)
for(j = 0 ; j < 3 ; j ++){
a[i][j] = b[k++];
printf("%d",a[i][j]);
}
system("pause");
return 0;
}
qsort看不懂再给你个百科的链接
>先把数组按升序排序,把有四个相同的提出来到临时数组,再三个,按你给的条件一项项的满足,我觉得这样的思路比较靠谱下面给一段升序和把四个相同提出来的代码:
For n = 1 To 19 '对xia(n)进行排序
For i = 1 To 20 - n
If xia(i) > xia(i + 1) Then
tmp = xia(i + 1)
xia(i + 1) = xia(i)
xia(i) = tmp
End If
Next i
Next n
count4 = 0
For i = 1 To 17 '把4个相同的找出来
If xia(i + 1) = xia(i) Then
If xia(i + 2) = xia(i + 1) Then
If xia(i + 3) = xia(i + 2) Then
m = i
count4 = count4 + 1 '标记已有一组4个相同的
temp(4 (count4 - 1) + 1) = xia(m)
temp(4 (count4 - 1) + 2) = xia(m + 1)
temp(4 (count4 - 1) + 3) = xia(m + 2)
temp(4 (count4 - 1) + 4) = xia(m + 3)
For n = 1 To 4
xia(m + n - 1) = xia(m + 3) '把4个相同的从xia数组删除
Next n
ReDim xia(1 To 20 - count4 4)
End If
End If
End If
Next i
count3 = 0
For i = 1 To 18 - count4 4 '把3个相同的找出来
If xia(i + 1) = xia(i) Then
If xia(i + 2) = xia(i + 1) Then
m = i
count3 = count3 + 1 '标记已有一组3个相同的
temp(4 count4 + 3 (count3 - 1) + 1) = xia(m)
temp(4 count4 + 3 (count3 - 1) + 2) = xia(m + 1)
temp(4 count4 + 3 (count3 - 1) + 3) = xia(m + 2)
For n = 1 To 3
xia(m + n - 1) = xia(m + 2) '把3个相同的从xia数组删除
Next n
'ReDim xia(1 To 20 - count4 4 - count3 3)
End If
End If
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)