二進位制求5個1的格式。。。。

wh7577發表於2021-09-09

package sds.sw.certificert.adv.five.one;

import java.io.FileInputStream;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Scanner;

public class SolutionReview {
static long begin, end, min, max;
static int counter;
static char[] startChars = new char[34];
static char[] endChars = new char[34];

public static void main(String[] args) throws Exception {
    System.setIn(new FileInputStream(
            "C:\Users\Michael\workspace\java\Examnation\src\sds\sw\certificert\adv\five\one\input.txt"));
    Scanner sc = new Scanner(System.in);
    long st = Calendar.getInstance().getTimeInMillis();
    int T = sc.nextInt();
    for (int i = 0; i  min) {
            counter = findMiddle51(startChars, endChars);
        } else {
            min = -1;
            max = -1;
            counter = 0;
        }

        printResult(i + 1);
    }
    sc.close();
    long p = Calendar.getInstance().getTimeInMillis() - st;
    System.out.println("Use time: " + p);

}

static void scanNextTestcase(Scanner sc) {
    begin = sc.nextLong();
    end = sc.nextLong();
    Arrays.fill(startChars, '0');
    Arrays.fill(endChars, '0');

    char[] temp = Long.toBinaryString(begin).toCharArray();
    int offset = startChars.length - temp.length;
    for (int index = 0; index  '01'
        right--;
        if (aArray[right] == '1' && aArray[right + 1] == '0') {
            aArray[right + 1] = '1';
            aArray[right] = '0';
            left = right;
        }

    }
    right = aArray.length - 1;
    left += 2;
    while (left 0
        if (aArray[left] != '0') {
            left++;
        }
        if (aArray[right] != '1') {
            right--;
        }
        if (aArray[left] == '0' && aArray[right] == '1') {
            aArray[left] = '1';
            aArray[right] = '0';
            left++;
            right--;
        }

    }
}

static void findNext51(char[] aArray) {
    int left = 0;
    int right = aArray.length - 1;

    while (left != right) { // right first '01' --> '10'
        right--;
        if (aArray[right] == '0' && aArray[right + 1] == '1') {
            aArray[right + 1] = '0';
            aArray[right] = '1';
            left = right;
        }

    }
    right = aArray.length - 1;
    left++;
    while (left 1
        if (aArray[left] != '1') {
            left++;
        }
        if (aArray[right] != '0') {
            right--;
        }
        if (aArray[left] > aArray[right] && left  5) {
        int index = aArray.length - 1;
        while (amount1 != 5) {
            if (aArray[index] == '1' && amount1 > 5) {
                aArray[index] = '0';
                amount1--;
            }
            index--;
        }

    }

}

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2480/viewspace-2798847/,如需轉載,請註明出處,否則將追究法律責任。

相關文章