数据结构与算法分析(JAVA版)Chapter1练习题

时间:2023-02-14 15:27:58
/**
* 类名:Test1.java
* 说明:返回N的二进制的1个个数
*/
public class Test5 {

public static int getOne(int n){
if(n < 2)
return 1;
return n%2 + getOne(n/2);
}
/**
* 函数名称:main
* 说明:
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根

System.out.println(getOne(255));
}

}


/**
* 类名:Test6.java
* 说明:全排列
*/

public class Test6 {
public static <Type> void perm(Type[] a){
perm(a,0,a.length-1);
}
private static <Type>void perm(Type[]a, int low,int high){
if(low == high){
for(int i = 0; i<=high;i++)
System.out.print(a[i]);
System.out.println();
}else{
for(int i = low;i<=high;i++){
Type temp = a[low];//和后面的交换
a[low] = a[i];
a[i] = temp;
perm(a,low+1,high);
Type temp2 = a[low];//换回来
a[low] = a[i];
a[i] = temp2;
}
}
}
public static void main(String[] args) {
Character[] c = new Character[]{'a','b','c'};
perm(c);

}
}

class Collection<T> {
private Object[] o;
private int size = 0;

Collection(int size) {
o = new Object[size];
}

public boolean isEmpty(){
if(size==0)
return true;
else
return false;
}
public boolean insert(T a){
o[size] = a;
size++;
if(size > o.length)
return false;
else
return true;
}
public boolean makeEmpty(){
size = 0;
return true;
}
public boolean remove(){
if(size > 0){
size--;
return true;
}else
return false;
}
public boolean isPresent(T x){
boolean flag = false;
for(int i = 0;i < size;i++)
if(x.equals(o[i]))
flag = true;
if(flag)
return true;
else
return false;
}
}

public class Test13 {

/**
* 函数名称:main 说明:测试
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
Collection c = new Collection(20);
c.insert(2);
c.insert(3);
System.out.println(c.isPresent(new Integer(3)));
c.remove();
System.out.println(c.isPresent(new Integer(3)));
}

}

/**
* 类名:Test14.java
* 说明:设计一个泛型类OrderedCollection
*/
class OrderedCollection<T extends Comparable<? super T>>{
private Object[] o;
private int size = 0;

public OrderedCollection(int size) {
o = new Object[size];
}

public boolean isEmpty() {
if (size == 0)
return true;
else
return false;
}

public boolean makeEmpty() {
size = 0;
return true;
}

public boolean insert(T a) {

if (size > o.length)
return false;
else {
o[size] = a;
size++;
return true;
}
}

public boolean remove() {
if (size > 0) {
size--;
return true;
} else
return false;
}

public T findMin() {
if (size == 0)
return null;
else {
T min = (T) o[0];
for (int i = 1; i < size; i++) {
if ((((T) o[i]).compareTo(min)) < 0) {
min = (T) o[i];
}
}
return min;
}
}

public T findMax() {
if (size == 0)
return null;
else {
int max = 0;
for (int i = 1; i < size; i++) {
if ((((T) o[i]).compareTo((T) o[max])) > 0) {
max = i;
}
}
return (T)o[max];
}
}

}

public class Test14 {

/**
* 函数名称:main
* 说明:测试
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
OrderedCollection<Integer> c = new OrderedCollection<Integer>(10);
c.insert(5);
c.insert(3);
c.insert(2);
System.out.println(c.findMax()+" "+c.findMin());
c.remove();
System.out.println(c.findMax()+" "+c.findMin());
}

}

import java.util.Comparator;

class Rectangle {
private int width;
private int length;

public Rectangle(int length, int width) {
this.width = width;
this.length = length;
}

public int getWidth() {
return width;
}

public int getLength() {
return length;
}
public String toString(){
return "长方形的长度:"+length+"宽度:"+width;
}
}

class AreaComparator implements Comparator<Rectangle> {
@Override
public int compare(Rectangle o1, Rectangle o2) {
if (o1.getLength() * o1.getWidth() > o2.getLength() * o2.getWidth())
return 1;
else if (o1.getLength() * o1.getWidth() < o2.getLength()
* o2.getWidth())
return -1;
else
return 0;
}

}

class PerimeterComparator implements Comparator<Rectangle> {

@Override
public int compare(Rectangle o1, Rectangle o2) {
if (o1.getLength() + o1.getWidth() > o2.getLength() + o2.getWidth())
return 1;
else if (o1.getLength() + o1.getWidth() < o2.getLength()
+ o2.getWidth())
return -1;
else
return 0;
}

}

/***********************************************
* 类名:Test15.java
* 说明:写一个Rectangle类,并比较
***********************************************/
public class Test15 {


/*************************************************
* 函数名称:findMax
* 说明:根据不同的比较器找到MAX;
*************************************************/
public static <AnyType> AnyType findMax(AnyType[] arr,
Comparator<? super AnyType> cmp) {
int max = 0;
for (int i = 1; i < arr.length; i++) {
if (cmp.compare(arr[max], arr[i]) < 0)
max = i;
}
return arr[max];
}


/*************************************************
* 函数名称:main
* 说明:写一个Rectangle数组,并找到面积和周长最大的对象
*************************************************/
public static void main(String[] args) {
// TODO 自动生成的方法存根
Rectangle[] r = new Rectangle[] { new Rectangle(1, 2),
new Rectangle(5, 5), new Rectangle(6, 5), new Rectangle(4, 6),
new Rectangle(16, 1), new Rectangle(2, 2) };
System.out.println("面积最大的为:"+findMax(r,new AreaComparator()));
System.out.println("周长最大的为:"+findMax(r,new PerimeterComparator()));
}

}