第一章课后习题1.6

1.6 编写带有下列声明的例程:

public void permute(String str);

private void permute(char[] str, int low, int high);

第一个例程是个驱动程序,它调用第二个例程并显示String str中的字符的所有排列。例如,str是"abc", 那么输出的串则是abc,acb,bac,bca,cab,cba,第二个例程使用递归。

package com.algorithm.chapterone;

/**
 * 编写带有下列声明的例程:
 * public void permute(String str);
 * private void permute(char[] str, int low, int high);
 * 第一个例程是个驱动程序,它调用第二个例程并显示String str中的字符的所有排列。
 * 例如,str是"abc", 那么输出的串则是abc,acb,bac,bca,cab,cba,第二个例程使用递归。
 * @author Gao·Rongzheng
 *
 */
public class QuestionFive {
    public static int count = 0;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        permute("abcde");
        System.out.println(count);
    }

    public static void permute(String str) {
        char[] charArray = str.toCharArray();
        permute(charArray, 0, charArray.length);
    }

    private static void permute(char[] str, int low, int high) {
        if (low == high-1) {
            for (int i=0; i<high; i++) {
                System.out.print(str[i] + " ");
            }
            System.out.println();
            count++;
        } else {
            for (int i=low; i<high; i++) {
                swap(str, low, i);
                permute(str, low+1, high);
                swap(str, low, i);
            }
        }

    }

    public static void swap(char str[], int i, int j){
        char temp = str[i];
        str[i] = str[j];
        str[j] = temp;
   }
}

原文地址:https://www.cnblogs.com/code-future/p/11419267.html

时间: 2024-08-01 00:35:46

第一章课后习题1.6的相关文章

谭浩强 c++程序设计第一章课后习题 第7题

#include <iostream> using namespace std; int main() { int a,b,c; int f(int x,int y,int z);//这是函数的声明 //cin sonsole控制台 cout<<"请输入三个整数类型的数字:" <<endl; cin>>a>>b>>c; c=f(a,b,c);//abc有具体值,称为实际参数 cout<<c<<

谭浩强 c++程序设计第一章课后习题 第10题

#include <iostream> using namespace std; int main() { int a,b,c; cout<<"请输入三个整数类型的数字:" <<endl; cin>>a>>b>>c; void sort(int x,int y,int z); sort(a,b,c);//abc有具体值,称为实际参数 return 0; } void sort(int x,int y,int z)/

第一章课后习题

1-1:数据压缩的一个基本问题是“我们要压缩什么”,对此你是怎么理解的? 答:数据压缩,就是指不丢失有用信息的前提下,以最少的数码表示信号源所发的信号,减少容纳给定消息集合或数据采样集合的信号空间. 1-2:数据压缩的另一个基本问题是“为什么进行压缩”,对此你是怎么理解的? 答:因为多媒体技术所处理的对象包括图像.视频和声音等多种媒体.它们的数据量非常大. 如果不进行数据压缩传输和存储都难以实用化.而经过数据压缩可以将一些占用内存比较 大多媒体数据,压缩成可以缩小的文件内存,这样可以方便传递,节

第一章 课后习题 10

1 #include <iostream> 2 using namespace std; 3 int main() 4 { void sort(int x,int y,int z); 5 int x,y,z; 6 cin>>x>>y>>z; 7 sort(x,y,z); 8 return 0; 9 } 10 void sort(int x,int y,int z) 11 { 12 int temp; 13 if(x>y) {temp=x;x=y;y=t

第一章 课后习题 7

1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int a,b,c; 6 int f(int x,int y,int z); 7 cin>>a>>b>>c; 8 c=f(a,b,c); 9 cout<<c<<endl; 10 return 0; 11 } 12 int f(int x,int y,int z) 13 { 14 int m; 15 if(x&l

第一章课后习题1.5

1.5 编写一种递归方法,它返回数N的二进制中表示1的个数.利用这样一个事实:N为奇数,其1的个数为N/2的二进制中1的个数加1. package com.algorithm.chapterone; /** * 编写一种递归方法,它返回数N的二进制中表示1的个数.利用这样一个事实:N为奇数,其1的个数为N/2的二进制中1的个数加1. * @author Gao·Rongzheng * */ public class QuestionFour { public static void main(S

《计算机网络&amp;#183;自顶向下方法》第七版 第三章 课后习题与问题 答案

非官方答案,本人已尽最大努力(包括参考官方答案),使结果正确,如有错误,请大佬指出 正文: 3.1~3.3节 R1 a.如果只是简单想把信件送到,那么所有的头部信息只需要一个目的地址就够了,题目给出端口号四个字节,所有分组的头部那就只需四个字节 此协议规定,运输层的全部任务就是,将应用层的数据,切成最大1196字节的块,把每一块加上目的主机对应程序的端口号,并将得到的分组交付给网络层 在接收方,运输层将网络层报文取回,去掉头部信息,将数据拼接成应用层需要的信息,根据端口号交付给应用层即可 不过话

《鸟哥linux》--第十一章课后习题答案

1.在linux上可以找到哪些shell?哪个档案记录可用的shell?儿linux预设的shell是? 1./bin/bash,/bin/tcsh,/bin/csh 2./etc/shells 3.bash,亦即是/bin/bash 2.在shell环境下,有个提示符(prompt),他可以修改吗?要改什么?默认的提示符内容是? 可以修改,改PS1变量,这个PS1的变量默认的内容为:[\[email protected]\h\W]\$ 3.如何显示HOME这个变量 echo $HOME 4.如

C Primer Plus 第十二章课后习题……2015.5.10

第十二章课后习题 1.自动存储 寄存器存储 静态空连接 2.静态空连接 内部链接 外部链接 3.静态外部链接  静态内部链接 4.空连接 5.在声明中使用表面这个变量或函数已经定义过 6.都分配一个具有100个int值的数组,calloc还把每个元素设置为零. 7.daisy全局变量  lily局部变量 8.#include<stdio.h> char color='B'; void first(void); void second(void); int main(void) { extern