解题思路:
注意事项:
参考代码:
public class 不容易系列2 { //满足条件的计数器 public static int count = 0; /** * 全排列算法 * @param chars * @param from * @param to */ public static void qpl(char [] chars,int from,int to) { if(from == to) { int i; for(i=0;i<=to;i++) { if(i==Integer.valueOf(String.valueOf(chars[i]))) { break; } } if(i==to+1) { count++; } return; } for(int i=from;i<=to;i++) { swap(chars, from, i); qpl(chars, from+1, to); swap(chars, i, from); } } /** * 交换数 * @param chars * @param i * @param j */ public static void swap(char [] chars,int i,int j) { char temp; temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); while(in.hasNext()) { count = 0; int n = in.nextInt(); StringBuffer str = new StringBuffer(""); for(int i=0;i<n;i++) { str.append(i); } //进行全排列 String s = new String(str); qpl(s.toCharArray(), 0, s.length()-1); //输出满足条件的错误方式 System.out.println(count); } } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:508 |
分糖果 (C++代码)浏览:1438 |
C语言程序设计教程(第三版)课后习题8.1 (C语言代码)浏览:1242 |
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:879 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:468 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:606 |
矩阵加法 (C语言代码)浏览:1719 |
出圈】指针malloc版浏览:355 |
企业奖金发放 (C语言代码)浏览:2315 |
前10名 (C语言代码)浏览:726 |