package ;
import ;
public class VectorRotation {
private VectorRotation() { }
public static int[] leftShift4(int[] arr, int i)
{
int shiftBits = processParameters(arr, i);
if (shiftBits == 0) {
return arr;
}
int arrlen = ;
int[] temp = new int[shiftBits];
for (int k=0; k < shiftBits; k++) {
temp[k] = arr[k];
}
for (int k=shiftBits; k < arrlen; k++) {
arr[k-shiftBits] = arr[k];
}
for (int k = 0; k < shiftBits; k++) {
arr[k + arrlen-shiftBits] = temp[k];
}
return arr;
}
public static int[] leftShift3(int[] arr, int i)
{
int shiftBits = processParameters(arr, i);
if (shiftBits == 0) {
return arr;
}
int arrlen = ;
for (int k=0; k < gcd(, shiftBits); k++) {
int temp = arr[k];
int foreIndex = k;
int afterIndex = k + shiftBits;
while (afterIndex != k) {
arr[foreIndex] = arr[afterIndex];
foreIndex = (foreIndex + shiftBits) % arrlen;
afterIndex = (afterIndex + shiftBits) % arrlen;
}
arr[foreIndex] = temp;
}
return arr;
}
private static int gcd(int m, int n)
{
if (m < 0 || n < 0) {
throw new IllegalArgumentException("参数错误,必须均是正整数!");
}
if (m % n == 0) {
return n;
}
return gcd(n, m%n);
}
public static int[] leftShift2(int[] arr, int i)
{
int shiftBits = processParameters(arr, i);
if (shiftBits == 0) {
return arr;
}
int beginIndex = 0;
int endIndex = -1;
int varlength = endIndex - beginIndex + 1;
while (true) {
if (varlength == 2 * shiftBits) {
exchange(arr, beginIndex, beginIndex + shiftBits, shiftBits);
break;
} else if (varlength > 2 * shiftBits) {
exchange(arr, beginIndex, varlength-shiftBits, shiftBits);
endIndex -= shiftBits;
} else if (varlength < 2 * shiftBits) {
exchange(arr, beginIndex, beginIndex + shiftBits, varlength - shiftBits);
beginIndex += varlength - shiftBits;
shiftBits = 2 * shiftBits - varlength;
}
varlength = endIndex - beginIndex + 1;
}
return arr;
}
private static void exchange(int[] arr, int beginIndex1, int beginIndex2, int length)
{
checkParametersForExchange(arr, beginIndex1, beginIndex2, length);
for (int k=0; k < length; k++) {
int temp = arr[k+beginIndex1];
arr[k+beginIndex1] = arr[k+beginIndex2];
arr[k+beginIndex2] = temp;
}
}
private static void checkParametersForExchange(int[] arr, int beginIndex1, int beginIndex2, int length)
{
if (beginIndex1 + length-1 >= || beginIndex2 + length-1 >= ) {
throw new IllegalArgumentException("参数错误,指定数组区域超过数组范围!");
}
if ((beginIndex1 - beginIndex2) + 1 <= length)
throw new IllegalArgumentException("参数错误,指定数组区域不能重叠!");
}
public static int[] leftShift(int[] arr, int i)
{
int shiftBits = processParameters(arr, i);
if (shiftBits == 0) {
return arr;
}
reverse(arr, 0, shiftBits-1);
reverse(arr, shiftBits, -1);
reverse(arr, 0, -1);
return arr;
}
public static void reverse(int[] arr, int beginIndex, int endIndex)
{
checkParameterForReverse(arr, beginIndex, endIndex);
int length = endIndex - beginIndex + 1;
for (int k=beginIndex; k < beginIndex + (length+1)/2; k++) {
int temp = arr[k];
arr[k] = arr[beginIndex + endIndex -k];
arr[beginIndex + endIndex -k] = temp;
}
}
private static void checkParameterForReverse(int[] arr, int beginIndex, int endIndex)
{
if (beginIndex < 0 || endIndex < 0 || beginIndex >= || endIndex >= ) {
throw new IllegalArgumentException("指定区域 [" + beginIndex + "," + endIndex + "] 错误, 参数必须均为正整数,且不能超过数组长度 " + );
}
if (beginIndex > endIndex) {
throw new IllegalArgumentException("指定区域 [" + beginIndex + "," + endIndex + "] 错误,第一个参数必须不大于第二个参数!");
}
}
private static int processParameters(int[] arr, int i)
{
if (i < 0) {
throw new IllegalArgumentException("参数错误,指定移位位数必须是正整数!");
}
int shiftBits = i % ;
return shiftBits;
}
static class Tester {
public static void testLeftShift(int[] arr, int i) {
("将向量 " + (arr) + " 循环左移 " + i + " 位:/t");
try {
int[] copy = (arr, );
((leftShift(copy, i)) + " /t*** leftShift ");
copy = (arr, );
((leftShift2(copy, i)) + " /t*** leftShift2 ");
copy = (arr, );
((leftShift3(copy, i)) + " /t*** leftShift3 ");
copy = (arr, );
((leftShift4(copy, i)) + " /t*** leftShift4 ");
} catch (Exception e) {
(());
}
}
public static void testExchange() {
int[] arr = new int[] {1,2,3,4,5,6,7,8,9};
for (int i = 1; i <= 6; i++) {
int[] copy = (arr, );
try {
exchange(copy, 2, 5, i);
("i = " + i + "/t" + (copy));
} catch (Exception e) {
(());
}
}
}
public static void testReverse() {
int[] arr = new int[] {1,2,3,4,5,6,7,8,9};
for (int i = 0; i < ; i++) {
int[] copy = (arr, );
try {
reverse(copy, i, -i);
("i = " + i + "/t" + (copy));
} catch (Exception e) {
(());
}
}
}
public static void testGCD()
{
int n = 200, m = 100;
while(n>=-10 && m >=-10) {
try {
("[" + n + "," + m + "] 的最大公约数是 :" + gcd(m, n));
} catch (Exception e) {
(());
}
n-= 5; m-=3;
}
}
public static void main(String[] args)
{
("************* 最大公约数 **************");
testGCD();
("************* 数组区域内容交换 ****************");
testExchange();
("************* 数组逆置 ****************");
testReverse();
("************* 向量旋转 ****************");
testLeftShift(new int[] {1,2,3,4,5,6,7,8,9,10}, 3);
testLeftShift(new int[] {1,2,3,4,5,6,7,8,9,10}, 4);
testLeftShift(new int[] {1,2,3,4,5,6,7,8,9,10}, 8);
testLeftShift(new int[] {1,2,3,4,5,6,7,8,9,10}, 13);
testLeftShift(new int[] {1,2,3,4,5,6,7,8,9,10}, 30);
testLeftShift(new int[] {1,2,3,4,5,6,7,8,9,10}, 0);
testLeftShift(new int[] {1,2,3,4,5,6,7,8,9,10}, -1);
}
}
}