编程:用java 语言实现,输入一个数,就相应地输出的几维数组!||用1、2、2、3、4、5这六个数字,用java写一个函数,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
2013-12-05 12:46
1931 查看
public class ArrayDemo {
private static int[][] createArray(int num) {
int[][] intArray = new int[num][num];
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
int tmp = 0;
if (i == j) {
tmp = i;
} else if (i < j) {
tmp = i;
} else if (i > j) {
tmp = j;
}
intArray[i][j] = tmp;
}
}
return intArray;
}
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入 一个正整数:");
int num = scanner.nextInt();
System.out.println("开始输出【" + num + "】维数组:");
int[][] intArray = createArray(num);
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
System.out.print(intArray[i][j]);
System.out.print(" ");
}
System.out.println("");
}
}
}
public class ArrayDemo {
public static Set<String> set = new TreeSet<String>();
public static void perm(char[] n, int beg, int end) {
if (beg == end) {
addNumber(String.valueOf(n));
} else {
for (int i = beg; i <= end; ++i) {
swap(n, beg, i);
perm(n, beg + 1, end);
swap(n, beg, i);
}
}
}
public static void swap(char[] n, int x, int y) { //内层循环。
if (x == y || n[x] == n[y]) {
return;
}
char temp = n[x];
n[x] = n[y];
n[y] = temp;
}
public static void addNumber(String str) { //添加到set集合里边,其中的if语句是排除那些条件的。
if (str.charAt(2) == '4' || str.contains("35") || str.contains("53")) {
return;
}
set.add(str);
}
public static void main(String args[]) {
char[] number = new char[] { '1', '2', '2', '3', '4', '5' };
perm(number, 0, number.length - 1); //外层循环
System.out.println(set.size());
int cols = 10;
for (String s : set) {
System.out.print(s + " ");
if (cols-- == 1) {
System.out.println();
cols = 10;
}
}
}
}
private static int[][] createArray(int num) {
int[][] intArray = new int[num][num];
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
int tmp = 0;
if (i == j) {
tmp = i;
} else if (i < j) {
tmp = i;
} else if (i > j) {
tmp = j;
}
intArray[i][j] = tmp;
}
}
return intArray;
}
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入 一个正整数:");
int num = scanner.nextInt();
System.out.println("开始输出【" + num + "】维数组:");
int[][] intArray = createArray(num);
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
System.out.print(intArray[i][j]);
System.out.print(" ");
}
System.out.println("");
}
}
}
public class ArrayDemo {
public static Set<String> set = new TreeSet<String>();
public static void perm(char[] n, int beg, int end) {
if (beg == end) {
addNumber(String.valueOf(n));
} else {
for (int i = beg; i <= end; ++i) {
swap(n, beg, i);
perm(n, beg + 1, end);
swap(n, beg, i);
}
}
}
public static void swap(char[] n, int x, int y) { //内层循环。
if (x == y || n[x] == n[y]) {
return;
}
char temp = n[x];
n[x] = n[y];
n[y] = temp;
}
public static void addNumber(String str) { //添加到set集合里边,其中的if语句是排除那些条件的。
if (str.charAt(2) == '4' || str.contains("35") || str.contains("53")) {
return;
}
set.add(str);
}
public static void main(String args[]) {
char[] number = new char[] { '1', '2', '2', '3', '4', '5' };
perm(number, 0, number.length - 1); //外层循环
System.out.println(set.size());
int cols = 10;
for (String s : set) {
System.out.print(s + " ");
if (cols-- == 1) {
System.out.println();
cols = 10;
}
}
}
}
相关文章推荐
- 1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等.要求:"4"不能在第三位,"3"与"5"不能相连.
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。用1、2、2、3、4、5这六
- 用1、2、3、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列 要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、3、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列 要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连
- Java经典算法集——如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
- 该公司笔试题就1个,要求在10分钟内作完。 题目如下:用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列, 如:512234、412325等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列, 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
- // 40、用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列, // 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
- 用 1,2,2,3,4,5 六个数字,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
- 【源码】用1,2,2,3,4,5这六个数字,写一个函数,打印出所有不同的排序,要求:4不能放在第三位,3与5不能相连(C语言实现)
- 【每天算法1】:用java 语言实现,输入一个数,就相应地输出的几维数组
- 2013年用友校招编程题:用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列
- 用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列
- 有两个升序排列的数组A1和A2,给A1开辟的剩余内存有足够空间容纳A1,请实现一个函数,把A2中所有数字插入到A1中,并且是按照升序排列的
- 从标准输入读入一个由字母构成的串(不大于30个字符)。从该串中取出3个不重复的字符,求所有的取法。取出的字符,要求按字母升序排列成一个串。不同的取法输出顺序可以不考虑。
- 问题:利用输入输出流重载实现一个整数数组的输入和输出。 要求:输入一个数组大小 及 所有元素的值,排序后输出该数组的所有元素。