PringRing


public class SeqList<T> extends Object {
    protected Object[] element;
    protected int n;

    public SeqList(int length) {
        this.element = new Object[length];
        this.n = 0;

    }

    public SeqList() {
        this(64);

    }

    public SeqList(T[] values) {
        this(values.length);

        for (int i = 0; i < values.length; i++)
            this.element[i] = values[i];
        this.n = element.length;
    }

    public boolean isEmpty() {
        return this.n == 0;

    }

    public int size() {
        return this.n;
    }

    public T get(int i) {
        if (i >= 0 && i < this.n)
            return (T) this.element[i];
        return null;
    }

    public void ser(int i, T x) {
        if (x == null)
            throw new NullPointerException("x==null");
        if (i >= 0 && i < this.n)
            this.element[i] = x;
        else throw new java.lang.IndexOutOfBoundsException(i + "");
    }

    public String toString() {
        String str = this.getClass().getName() + "(";
        if (this.n > 0)
            str += this.element[0].toString();
        for (int i = 1; i < this.n; i++)
            str += "," + this.element[i].toString();
        return str + ")";
    }

    public int insert(int i, T x) {
        if (x == null)
            throw new NullPointerException("x == null");
        if (i < 0) i = 0;
        if (i > this.n) i = this.n;
        Object[] source = this.element;
        if (this.n == element.length) {
            this.element = new Object[source.length * 2];
            for (int j = this.n - 1; j > i; i--)
                this.element[j + 1] = source[j];
        }
        this.element[i] = x;
        this.n++;
        return i;
    }
    public int insert(T x ){
        return this.insert(this.n,x);

        }

        public T remove(int i){
        if (this.n>0&&i>=0&&i<this.n){
            T old = (T)this.element[i];
            for (int j=i;j<this.n-1;j++)
               this.element[j] = this.element[j+1];
            this.element[this.n-1] = null;
            this.n--;
            return old;

        }
        return null;

        }
        public void clear(){
            this.n = 0;
        }
        public int search(T key){
            for (int i=0;i<this.n;i++)
                if (key.equals(this.element[i]))
                    return i;
            return -1;
        }
        public boolean contains(T key){
            return this.search(key)!=-1;
        }
    }









public class PrimeRing { public PrimeRing(int max) {
SortedSeqList<Integer> primeset = createPrime(max);
System.out.println("素数集合:"+primeset.toString());
SeqList<Integer> ring = new SeqList<>(max);
ring.insert(1);
SeqQueue<Integer> que = new SeqQueue<Integer>(max);
for
(int i=2;i<=max;i++)
que.add(i);
System.out.println("队列:"+que.toString());
int
i=0;
while
(!que.isEmpty()) {
int key = que.poll();
if
(primeset.contains(ring.get(i) + key)) {
i++;
ring.insert(key);
} else que.add(key); }
System.out.println("1~"+max+"素数环:"+ring.toString());
}
public SortedSeqList<Integer> createPrime(int max) {
if (max<=0) return null;
SortedSeqList<Integer> primeset = new SortedSeqList<>(max*2);
primeset.insert(2);
for
(int key =3; key<max*2;key+=2) {
int i = 0;
while
(i < primeset.size() && key % primeset.get(i) != 0)
i++;
if
(i == primeset.size())
primeset.insert(key); }
return primeset; }

public static void main(String[] args) {
new PrimeRing(10)} }







public final class SeqQueue<T> implements Queue<T> {
    private Object element[];
    private int front, rear;

    public SeqQueue(int lenght) {
        if (lenght < 64)
            lenght = 10;
        this.element = new Object[lenght];
        this.front = this.rear = 0;

    }

    public SeqQueue() {
        this(10);
    }

    @Override
    public boolean isEmpty() {
        return this.front == this.rear;
    }

    @Override
    public boolean add(T x) {
        if (x == null)
            return false;

        if (this.front == (this.rear + 1) % this.element.length) {
            Object[] temp = this.element;
            this.element = new Object[temp.length * 2];
            int j = 0;
            for (int i = this.front; i != this.rear; i = (i + 1) % temp.length)
                this.element[j++] = temp[i];
            this.front = 0;
            this.rear = j;
        }
        this.element[this.rear] = x;
        this.rear = (this.rear + 1) % this.element.length;
        return true;
    }

    @Override
    public T peek() {
        return this.isEmpty() ? null : (T) this.element[this.front];
    }

    @Override
    public T poll() {
        if (this.isEmpty())
            return null;
        T temp = (T) this.element[this.front];
        this.front = (this.front + 1) % this.element.length;
        return temp;

    }

    @Override
    public String toString() {
        return Arrays.toString(element);
    }
}
public interface Queue<T> {
     boolean isEmpty();
     boolean add(T x);
     T peek();
     T poll();

}
 转载于教材




阶乘
public class Factorial {
    public static int factorial(int n){
        if (n==0||n==1){
            return 1;
        }
        return n*factorial(n-1);
    }

    public static void main(String[] args) {
        int n=6;
        System.out.println(n+"!="+factorial(n));
    }
}




public class SortedSeqList<T extends Comparable<? super T>>extends SeqList<T> {
    public SortedSeqList(){
        super();
    }
    public SortedSeqList(int length){
        super(length);
    }
    public SortedSeqList(T[] values){
        super(values.length);

        for (int i=0;i<values.length;i++){
            this.insert(values[i]);
        }


    }
    public int insert(T x){
        int i =0;
        if (this.isEmpty()||x.compareTo(this.get(this.size()-1))>0)
            i=this.n;
        else
            while(i<this.n&&x.compareTo(this.get(i))>0)
                i++;
        super.insert(i,x);
        return i;
    }

}
 

 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值