Java經典編程題50道之三十六
阿新 • • 發佈:2017-06-08
nbsp 移動 編程 move main new for i++ java System.out.print(r + " ");
}
if (n <= m.length) {
int[] b = new int[m.length];
for (int i = 0; i < m.length - n; i++) {
b[i + n] = m[i];
}
int j = 0;
for (int i = m.length - n; i < m.length; i++) {
b[j] = m[i];
j++;
}
System.out.print("\n移動" + n + "位後的數組為:");
for (int r : b) {
System.out.print(r + " ");
}
} else {
System.out.print("\n移動錯誤!");
}
}
}
有n個整數,使其前面各數順序向後移m個位置,最後m個數變成最前面的m個數。
public class Example36 {
public static void main(String[] args) {
int[] m = { 18, 12, 23, 34, 95, 76, 57, 28, 9 };
moveElement(m, 5);
}
public static void moveElement(int[] m, int n) {
System.out.print("移位前的數組為:");
for (int r : m) {
}
if (n <= m.length) {
int[] b = new int[m.length];
for (int i = 0; i < m.length - n; i++) {
b[i + n] = m[i];
}
int j = 0;
for (int i = m.length - n; i < m.length; i++) {
j++;
}
System.out.print("\n移動" + n + "位後的數組為:");
for (int r : b) {
System.out.print(r + " ");
}
} else {
System.out.print("\n移動錯誤!");
}
}
}
Java經典編程題50道之三十六