移位器程序

时间:2015-07-06 22:20:26

标签: java arrays

所以我有一个只允许正整数的Shifter程序。 问题:我该如何制作呢?它也会收到负数。 I.e
程序要求换班次数,我输入3并输出
3 2 1 15 14 13 12 11 10 9 8 7 6 5 4

 public static void shiftRight(int[] list)
 {
     if (list.length < 2) return;

     int last = list[list.length - 1];

     for(int i = list.length - 1; i > 0; i--) {
         list[i] = list[i - 1];
     }
     list[0] = last;
 }

如果要向右移动。我怎么做到这将根据我输入的数字而改变?即为正面,左边为负面。

2 个答案:

答案 0 :(得分:0)

我会根据您的方法实施dim tmpCell as range set tmpCell = Sheet3.("A1") set myFile = "C:\Users\User1\Desktop\Folder1\page.xls" set myFileName = "page" set mySheetName = "Sheet1" set myRange = "A1" workbooks.open fileName:=myFile tmpCell.Value = Workbooks(myFileName).Sheets(mySheetName).range(myRange).value workbooks.close fileName:=myFile 方法,然后决定使用哪种

shiftLeft(int[])
顺便说一句,这种转变不是很有效的方式。

答案 1 :(得分:0)

您可以使用以下功能。它使用临时数组存储旋转/移位的数组,然后复制回原始数组。原始数组和临时数组的数组遍历两次,因此它可以有效地多次调用数组上的移位函数。

/**
 * This function performs the right/left shift operations
 * on the array of integers.
 *
 * @param   int[]   array   array of integers
 * @param   int     shift   Amount of right or
 *                          left shift. If shift
 *                          is grater than zero, 
 *                          function performs
 *                          right shift, otherwise
 *                          the function perform
 *                          left shift.
 */
public static void shift_array (int[] array, int shift) {

    /*
     * If array is null, then return 
     * from the function.
     */
    if (array == null) {
        return;
    }

    boolean is_left_shift = (shift < 0); // store whether   
                                         // left shift is
                                         // intended.
    int shift_amount;   // store the amount of shift
    int array_length = array.length;    // length of the 
                                        // array

    if (shift < 0) {
        shift_amount = shift * -1; // if shift is less than zero
                                   // then shift_amount is set as 
                                   // product of shift and -1.
    } else {
        shift_amount = shift;
    }

    /*
     * Make sure the shift_amount is within the
     * length of the array.
     */     
    shift_amount = shift_amount % array_length;

    /*
     * If the shift_amount is zero, we 
     * have nothing to do. Return.
     */
    if (shift_amount == 0) {
        return;
    }

    /*
     * Create a temp array and copy the 
     * elements in the array at appropriate
     * positions.
     */
    if (!is_left_shift) {

        /*
         * Temp array to store elements at shifted
         * positions.
         */
        int[] temp = new int[array_length];

        /*
         * Iterate throught the array
         */ 
        for (int i = 0; i < array_length; ++i) {

            int temp_index;

            /*
             * Map the elements to appropriate 
             * positions in the temp array.
             */
            if ((i + shift_amount) < array_length) {
                temp_index = i + shift_amount;      
            } else {
                temp_index = (i + shift_amount) % array_length;;        
            }

            /*
             * Copy the array element into the temp array.
             */ 
            temp[temp_index] = array[i]; 
        }

        /*
         * Copy back from temp array to array
         */
        for (int i = 0; i < array_length; ++i) {
            array[i] = temp[i];
        }
    } else {

        /*
         * Temp array to store elements at shifted
         * positions.
         */
        int[] temp = new int[array_length];

        /*
         * Iterate throught the array
         */ 
        for (int i = 0; i < array_length; ++i) {

            int temp_index;

            /*
             * Map the elements to appropriate 
             * positions in the temp array.
             */
            if ((i - shift_amount) >= 0) {
                temp_index = i - shift_amount;
            } else {
                temp_index = i - shift_amount + array_length;
            }

            /*
             * Copy the array element into the temp array.
             */ 
            temp[temp_index] = array[i]; 
        }

        /*
         * Copy back from temp array to array
         */
        for (int i = 0; i < array_length; ++i) {
            array[i] = temp[i];
        }
    }
}