将整数序列{3,15,28,11,34,78,95,27,18}首末颠倒过来。
int a[ ] = {3,15,28,11,34,78,95,27,18};
int i, length, temp;
length = a.length;
for(i=0;i<(length/2);i++){
//以下将数组元素a[i]和a[length -1 - i]的值互换
temp = a[i];
a[i] = a[length -1 –i];
a[length -1 –i] = temp;
}
for(i=0;i<length;i++){
System.out.print(a[i] + “ ”);
} |
|