两组数组一样大,我们用i代表数组a的下标,用j代表数组b的下标。 如果a数组的下标等于b数组的下标,则进行交换 代码如下:
if(i
== j
)
{
t
= a
[i
];
a
[i
] = b
[j
];
b
[j
] = t
;
}
具体实现如下:
#include<stdio.h>
#include <stdlib.h>
int main() {
int j
= 0, t
= 0;
int a
[10] = { 1,2,3,4,5,6,7,8,9,0 };
int b
[10] = { 0,9,8,7,6,5,4,3,2,1 };
for (int i
= 0; i
< 10; i
++, j
++) {
if (i
== j
) {
t
= a
[i
];
a
[i
] = b
[j
];
b
[j
] = t
;
}
printf("-", a
[i
]);
}
printf("\n");
for (int j
= 0; j
< 10; j
++) {
printf("-", b
[j
]);
}
printf("\n");
system("pause");
return 0;
}
转载请注明原文地址: https://win8.8miu.com/read-5392.html