将以下10个整数(1,3,5,7,9,2,4,6,8,10),放入一个一维数组中,然后将第1个元素与第10个元素互

2024-12-20 03:58:04
推荐回答(2个)
回答1:

#include
using namespace std;

int main() {
int a[10] = {1,3,5,7,9,2,4,6,8,10};//原数组
int b[10]; //新数组
int n,m=10;//下标元素
for(n=0;n//将原数组第一个元素依次复制到新数组最后一位中
b[m-1] = a[n];
}

//输出原数组
for(n=0;ncout << a[n] << ',' << '\n';
}
//输出新数组
for(n=0;ncout << b[n] << ',' << '\n';
}
return 0;
}

回答2:

package zhangyw.test;

import java.util.Arrays;

public class Test {

public static void main(String[] args) throws Exception {
int[] values = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
int[] orginal = Arrays.copyOf(values, values.length);

for (int i = 0; i < values.length / 2; i++) {
int tailIndex = values.length - i - 1;
int temp = values[i];
values[i] = values[tailIndex];
values[tailIndex] = temp;
}

System.out.println("交换前:");
for (int i = 0; i < orginal.length; i++) {
System.out.print(orginal[i] + " ");
}

System.out.println("\n交换后:");
for (int i = 0; i < values.length; i++) {
System.out.print(values[i] + " ");
}
}
}