递归实现变位数 (就是出现那些可能性)

变为数 说白了 把出现的可能 都现实出来

直接代码

package com.lm.digui;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* 变位数数直白了就是把所有的可能都显示出来

* 那么我们开始 就会从哪里就开始呢

*/

public class changZM {

static int size;

static int count;

static char[] ch = new char[100];

public static void main(String[] args) throws IOException {

System.out.println("请输入字母");

// 键盘输入

String s = getString();

// 获取字节长度

size = s.length();

for (int i = 0; i < size; i++) {

// 将字母 分解成字节

ch[i] = s.charAt(i);

}

// 输入数字

doChange(size);

}

// 向前移动n-1次 ,循环n次

private static void doChange(int n) {

if (n == 1) {

return;

}

for (int i = 0; i < n; i++) {

doChange(n - 1);

if (n == 2) {

printDislay();

}

move(n);

}

}

/**

* \

* 打印出来函数

*/

private static void printDislay() {

if (count < 99) {

System.out.print(" ");

}

if (count < 9) {

System.out.print(" ");

}

System.out.print(++count + ":");

System.out.print(" ");

for (int i = 0; i < size; i++) {

System.out.print(ch[i]);

}

System.out.print(" ");

System.out.flush();

if (count % 6 == 0) {

System.out.println();

}

}

private static void move(int n) { // 把后面的字母向前移动一位

int j;

int posistation = size - n;

char temp = ch[posistation];

for (j = posistation + 1; j < size; j++) {

ch[j - 1] = ch[j];

}

ch[j - 1] = temp;

}

/**

* 键盘输入

*

* @return

*/

private static String getString() throws IOException {

InputStreamReader in = new InputStreamReader(System.in);

BufferedReader bf = new BufferedReader(in);

String string = bf.readLine();

return string;

}

}

 

猜你喜欢

转载自www.cnblogs.com/limingming1993/p/12153996.html