DoubleKill2014 于 2014.12.17 09:56 提问

C语言派寻问题求解答！！！

4个回答

lx624909677      2014.12.17 10:02

#include

int main()
{
int num[3][2] = {{5,2},{3,1},{2,9}};
int temp;

``````for(int i = 0;i<3-1; i++)
{
for(int j = 0;j<3-1-i; j++)
{
if(num[j][0]>num[j+1][0])
{
for(int k=0; k<2;k++)
{
temp = num[j][k];
num[j][k] = num[j+1][k];
num[j+1][k] = temp;
}
}
}
}

for(int i = 0; i<3; i++)
{
for(int j = 0; j<2; j++)
{
printf("%d ",num[i][j]);
}
printf("\n");
}
return 0;
``````

}

``````
``````
lx624909677      2014.12.17 10:03

#include

int main()
{
int num[3][2] = {{5,2},{3,1},{2,9}};
int temp;

``````for(int i = 0;i<3-1; i++)
{
for(int j = 0;j<3-1-i; j++)
{
if(num[j][0]>num[j+1][0])
{
for(int k=0; k<2;k++)
{
temp = num[j][k];
num[j][k] = num[j+1][k];
num[j+1][k] = temp;
}
}
}
}

for(int i = 0; i<3; i++)
{
for(int j = 0; j<2; j++)
{
printf("%d ",num[i][j]);
}
printf("\n");
}
return 0;
``````

}

``````
``````
xuzuning      2014.12.17 10:29
`````` void main() {
int ar[3][2] = {{5,2},{3,1},{2,9}};
int temp[2], i, j;
int len = 3;

for(i = 0;i<len-1; i++) {
for(j = i+1;j<len; j++) {
if(ar[i][0] > ar[j][0]) {
memcpy(temp, ar[i], sizeof(temp));
memcpy(ar[i], ar[j], sizeof(temp));
memcpy(ar[j], temp, sizeof(temp));
}
}
}

for(i = 0; i<len; i++) {
printf("[%d, %d]\n", ar[i][0], ar[i][1]);
}
}
``````
xuzuning      2014.12.17 10:30

`````` void main() {
int ar[3][2] = {{5,2},{3,1},{2,9}};
int temp[2], i, j;
int len = 3;

for(i = 0;i<len-1; i++) {
for(j = i+1;j<len; j++) {
if(ar[i][0] < ar[j][0]) {
memcpy(temp, ar[i], sizeof(temp));
memcpy(ar[i], ar[j], sizeof(temp));
memcpy(ar[j], temp, sizeof(temp));
}
}
}

for(i = 0; i<len; i++) {
printf("[%d, %d]\n", ar[i][0], ar[i][1]);
}
}
``````