欢迎使用CSDN-markdown编辑器

算法与数据结构代码存档

import java.util.Arrays;
import java.util.Scanner;

class Queue{
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;

public Queue(int s){
    maxSize = s;
    queArray = new long[maxSize];
    front=0;
    rear=-1;
    nItems=0;
}

public void insert(long j){
    if(rear==maxSize-1){
        rear=-1;
    }
    queArray[++rear]=j;
    nItems++;

}

public long remove(){
    long temp = queArray[front++];
    if(front==maxSize){
        front=0;
    }
    nItems--;
    return temp;
}

public long peekFront(){
    return queArray[front];
}

public boolean isEmpty(){
    return(nItems==0);
}

public boolean isFull(){
    return(nItems==maxSize);
}

public int size(){
    return nItems;
}

}

class StackX{
private int maxSize;
private long[] stackArray;
private int top;

public StackX(int s){
    maxSize=s;
    stackArray = new long[maxSize];
    top=-1;
}

public void push(long j){
    stackArray[++top]=j;
}

public long pop(){
    return stackArray[top--];
}

public long peek(){
    return stackArray[top];
}

public boolean isEmpty(){
    return (top==-1);
}

public boolean isFull(){
    return (top==maxSize-1);
}

}

class QueueX{

private int maxSize;
private long[] queArray;
private int front;
private int rear;

public QueueX(int s){
    maxSize =s+1;
    queArray = new long[maxSize];
    front = 0;
    rear=-1;
}

public void insert(long j){

    if(rear==maxSize-1){
        rear=-1;
    }
    queArray[++rear]=j;

}

public long remove(){
    long temp = queArray[front++];
    if(front==maxSize){
        front=0;
    }
    return temp;
}

public long peek(){
    return queArray[front];
}

public boolean isEmpty(){
    return(rear+1==front||(front+maxSize-1==rear));
}

public boolean isFull(){
    return(rear+2==front||(front+maxSize-2==rear));
}

public int size(){

    if(rear>=front){
        return rear-front+1;
    }else{
        return (maxSize-front)+(rear+1);
    }

}

}

class Link{

public int iData;
public double dData;
public Link next;

public Link(int iData,double dData){
    this.iData=iData;
    this.dData=dData;
}

public void displayLink(){

    System.out.print("{"+iData+','+dData+"}");
}

}

class LinkList{

private Link first;

public LinkList(){

}

public boolean isEmpty(){
    return (first==null);
}

public void insertFirst(int id,double dd){

    Link newLink = new Link(id,dd);
    newLink.next=first;
    first=newLink;
}

public Link find(int key){
    Link current = first;
    while(current.iData !=key){
        if(current.next==null){
            return null;
        }else{
            current=current.next;
        }
    }

    return current;
}

public Link delete(int key){
    Link current = first;
    Link previous = first;
    while(current!=null&&current.iData!=key){
        if(current.next == null){
            return null;
        }else{
            previous = current;
            current = current.next;
        }
    }

    if(current==first){
        first=first.next;
    }else{
        previous.next=current.next;
    }
    return current;
}

public Link deleteFirst(){

    Link temp = first;
    first = first.next;
    return temp;
}


public void displayList(){

    System.out.print("List(first---->last):");
    Link current=first;
    while(current!=null){
        current.displayLink();
        current=current.next;
        System.out.print(" ");
    }
    System.out.println();
}

}

class LinkX{
public long dData;
public LinkX next;

public LinkX(long dd){
    dData =dd;
}

public void displayLink(){
    System.out.print(dData+" ");
}

}

class LinkListX{
private LinkX first;

public LinkListX(){
    first = null;
}

public LinkX getFisrst(){
    return first;
}

public void setFirst(LinkX f){
    first = f;
}

public boolean isEmpty(){
    return first == null;
}

public ListIterator getIterator(){
    return new ListIterator(this);
}

public void displayList(){

    LinkX current = first;
    while(current!=null){
        current.displayLink();
        current=current.next;
    }
    System.out.println();
}

}

class ListIterator{

private LinkX current;
private LinkX previous;
private LinkListX ourList;

public ListIterator(LinkListX list){
    ourList = list;
    reset();
}

public void reset(){

    current = ourList.getFisrst();
    previous = null;
}

public boolean atEnd(){
    return (current.next==null);
}

public void nextLink(){
    previous = current;
    current = current.next;
}

public LinkX getCurrent(){
    return current;
}

public void insertAfter(long dd){

    LinkX newLink = new LinkX(dd);

    if(ourList.isEmpty()){

        ourList.setFirst(newLink);
        current = newLink;
    }else{

        newLink.next = current.next;
        current.next=newLink;
        nextLink();
    }
}

public void insertBefore(long dd){

    LinkX newLink = new LinkX(dd);

    if(previous == null){

        newLink.next = ourList.getFisrst();
        ourList.setFirst(newLink);
        reset();
    }else{

        newLink.next = previous.next;
        previous.next = newLink;
        current = newLink;
    }
}

public long deleteCurrent(){

    long value=current.dData;
    if(previous==null){

        ourList.setFirst(current.next);
        reset();

    }else{

        previous.next = current.next;
        if(atEnd()){
            reset();
        }else{
            current=current.next;
        }

    }

    return value;
}

}

public class TestForString {

static int total=0;

public static void main(String[] arg){

    LinkListX theList = new LinkListX();
    ListIterator iter1 = theList.getIterator();
    long value;

    iter1.insertAfter(20);
    iter1.insertAfter(40);
    iter1.insertAfter(80);
    iter1.insertBefore(60);

    while(true){
        System.out.print("Enter first letter of show,reset, ");


    }
}








//n个人围城一圈报数,是3的就退出,求剩下的人
static void baoShu(int n){

    int[] arr=new int[n];
    for(int i=1;i<=n;i++){
        arr[i-1]=i;
    }

    int count=0;
    int baoShu=0;
    while(count<n-1){

        for(int i=0;i<n;i++){

            if(arr[i]!=0){
                baoShu++;
                if(baoShu==3){
                    baoShu=0;
                    arr[i]=0;
                    count++;
                }
            }
        }
    }
    for(int i=0;i<n;i++){
        if(arr[i]!=0){
            System.out.println(arr[i]);
        }
    }
}

//将数组的最后num位移到前面去
static void swapArray(int[] arr,int num){

    int[] container=new int[num];
    int containIndex=0;
    for(int i=arr.length-num;i<arr.length;i++){
        container[containIndex++]=arr[i];
    }
    for(int i=arr.length-1;i>num-1;i--){
        arr[i]=arr[i-num];
    }
    for(int i=0;i<num;i++){
        arr[i]=container[i];
    }
}

static void pickBiggestAndSmallest(int[] arr){

    int bigIndex=0;
    int smallIndex=0;
    for(int i=0;i<arr.length;i++){

        if(arr[i]>arr[bigIndex]){
            bigIndex=i;
        }
        if(arr[i]<arr[smallIndex]){
            smallIndex=i;
        }
    }
    swap(arr,0,bigIndex);
    swap(arr,arr.length-1,smallIndex);

}

static void yangHuiSanJiao(int row){

    int[] old=new int[row];
    int[] present=new int[row];
    present[0]=1;
    present[1]=1;
    for(int i=0;i<row;i++){
        System.out.print(" ");
    }
    System.out.println(1);
    for(int i=0;i<row-2;i++){
        System.out.print(" ");
    }
    System.out.println(1+" "+1);
    for(int i=3;i<=10;i++){

        for(int m=i;m<=row;m++){
            System.out.print(" ");
        }
        for(int j=0;j<i-1;j++){

            old[j]=present[j];
        }
        for(int k=0;k<i;k++){

            if(k==0||k==i-1){
                present[k]=1;
            }else{
                present[k]=old[k]+old[k-1];

            }
            System.out.print(present[k]+"  ");
        }
        System.out.println();
    }

}

//带有重复元素的全排列
static void perm(char[] arr,int startIndex,int length){

    if(startIndex==length-1){
        total++;
        String str=new String(arr);
        System.out.println(str);
        return;
    }else{
        for(int i=startIndex;i<length;i++){

            int mark=0;
            for(int j=startIndex;j<i;j++){
                if(arr[j]==arr[i]){
                    mark=1;
                    break;
                }
            }

            if(mark==0){
                swap(arr,startIndex,i);
                perm(arr,startIndex+1,length);
                swap(arr,startIndex,i);
            }
        }
    }
    if(startIndex==0){
        System.out.println("***************************************"+total);
    }

}

static void arrangeSortForPaiChu(char[]arr,int startIndex,int length){
    if(startIndex==(length-1)){
        String result=new String(arr);
        if(result.contains("53")||result.contains("35")||(result.indexOf('4')==2)){
            return;
        }
        System.out.println(result);

        total++;
    }else{
        for(int i=startIndex;i<length;i++){
            swap(arr,i,startIndex);
            arrangeSortForPaiChu(arr,startIndex+1,length);
            swap(arr,i,startIndex);
        }
    }
    if(startIndex==0){
        System.out.println("***********************"+total);
        total=0;
    }
}

static void numberAnylize(int test){

    int wei=0;
    if(test>0&&test<9){
        wei=1;
    }else if(test>10&&test<100){
        wei=2;
    }else if(test>100&&test<1000){
        wei=3;
    }else if(test>1000&&test<10000){
        wei=4;
    }else if(test>10000){
        wei=5;
    }
    System.out.println("*****************"+wei+"*******************");


    for(int i=wei;i>0;i--){
        System.out.print(test%10);
        test/=10;
    }



}

static void myPrint(int num,int wei){

    if(wei==1){
        System.out.print(num);
    }else{
        int factor=1;
        for(int i=1;i<wei;i++){
            factor*=10;
        }
        System.out.print(num/factor);
        myPrint(num%factor,wei-1);
    }
}
static int diGuiLeiCheng(int n){
    if(n==1){
        return 1;
    }else{
        return n*diGuiLeiCheng(n-1);
    }
}

static void shuLieHe(){
    double up=1;
    double down=2;
    double total=1/2;
    for(int i=2;i<=20;i++){
        double temp=down;
        down=up+down;
        up=temp;
        total+=up/down;
    }
    System.out.println(total);
}

static void leiCheng(int n){

    int sumN=1;
    int total=0;
    for(int i=1;i<=n;i++){

        sumN*=i;
        total+=sumN;
    }
    System.out.println(total);
}

static void pingPongTeanm(){

    char[] pair1=new char[2];
    pair1[0]='A';
    char[] pair2=new char[2];
    pair2[0]='B';
    char[] pair3=new char[2];
    pair3[0]='C';
    char[] team1=new char[]{'A','B','C'};
    char[] team2=new char[]{'X','Y','Z'};

    for(int i=0;i<3;i++){

        if(team2[i]!=' '&&team2[i]!='X'&&team2[i]!='Z'){
            pair3[1]=team2[i];
            team2[i]=' ';
        }
    }
    for(int i=0;i<3;i++){
    }


}

static void countBeach(int day,int leftBeforEat){

    int total=0;
    for(int i=9;i>0;i--){
        leftBeforEat=(leftBeforEat+1)*2;
    }
    System.out.println("一共有 "+leftBeforEat+" 个");
}

static void arrangeSort(int[]arr,int startIndex,int length){
    if(startIndex==(length-1)){
        for(int i=0;i<length;i++){
            System.out.print(arr[i]);
        }
        System.out.println();

        total++;
    }else{
        for(int i=startIndex;i<length;i++){
            swap(arr,i,startIndex);
            arrangeSort(arr,startIndex+1,length);
            swap(arr,i,startIndex);
        }
    }
    if(startIndex==0){
        System.out.println("***********************"+total);
        total=0;
    }
}

static void chengFaBiao(){

    for(int i=1;i<=9;i++){

        for(int j=1;j<=i;j++){

            System.out.print(i+"x"+j+"="+i*j+" ");
        }
        System.out.println();
    }

}

static void swap(char[] arr,int indexI,int indexJ){
    char temp=arr[indexI];
    arr[indexI]=arr[indexJ];
    arr[indexJ]=temp;
}

static void swap(int[] arr,int indexI,int indexJ){

    int temp=arr[indexI];
    arr[indexI]=arr[indexJ];
    arr[indexJ]=temp;
}

static void changePram(int[] arr){
    arr=new int[10];
    for(int i=0;i<10;i++){
        arr[i]=i;
    }

}

static void quickSort(int[] arr,int length){

    int temp;
    for(int i=length-1;i>0;i--){


        int k=i;
        for(int j=0;j<i;j++){
            if(arr[j]>arr[k]){
                k=j;
            }
        }
        if(arr[i]<arr[k]){
            temp=arr[i];
            arr[i]=arr[k];
            arr[k]=temp;
        }
    }
}

static void bubbleSortDown(int[] arr,int length){
    for(int j=length-1;j>0;j--){
        for(int i=0;i<j;i++){
            if(arr[i]<arr[i+1]){
                int temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        }
    }
}

static void bubbleSort(int[]arr,int length){

    for(int j=length-1;j>0;j--){
        for(int i=0;i<j;i++){
            if(arr[i]<arr[i+1]){
                int temp= arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;

            }
        }
    }
}

static void swap(int a,int b){
    int temp=a;
    a=b;
    b=temp;
}

static void quickDown(int[] arr,int left,int right){

    if(left>=right){

        return;
    }

    int i=left;
    int j=right;
    int temp=arr[i];

    while(i<j){

        while(i<j&&arr[j]<temp){
            j--;
        }
        if(i<j){
            arr[i]=arr[j];
            i++;
        }

        while(i<j&&arr[i]>temp){
            i++;
        }
        if(i<j){
            arr[j]=arr[i];
            j--;
        }
    }
    arr[i]=temp;

    quickDown(arr,left,i-1);
    quickDown(arr,i+1,right);
}




static  void quickSort(int[] arr,int left,int right){

    if(left>=right){
        return;
    }

    int temp=arr[left];
    int j=right;
    int i=left;

    while(j>i){

        while(j>i&&arr[j]>temp){
            j--;
        }
        if(j>i){
            arr[i]=arr[j];
            i++;
        }
        while(i<j&&arr[i]<temp){
            i++;
        }
        if(i<j){
            arr[j]=arr[i];
            j--;
        }
    }
    arr[i]=temp;
    quickSort(arr,left,i-1);
    quickSort(arr,i+1,right);
}

static void sort(){
    Scanner scan = new Scanner(System.in);
    int a=scan.nextInt();
    int b=scan.nextInt();
    int c=scan.nextInt();

    int[] arr=new int[3];
    if(a>b){
        if(a>c){
            arr[0]=a;
            if(c>b){
                arr[1]=c;
                arr[2]=b;
            }else{
                arr[1]=b;
                arr[2]=c;
            }
        }else{
            arr[0]=c;
            arr[1]=a;
            arr[2]=b;
        }
    }else{
        //a<b
        if(c<b){
            arr[0]=b;
            if(a<c){
                arr[1]=c;
                arr[2]=a;
            }else{
                arr[1]=a;
                arr[2]=c;
            }
        }else{
            arr[0]=c;
            arr[1]=b;
            arr[2]=a;
        }
    }
    for(int i=0;i<3;i++){
        System.out.println(arr[i]);
    }
}

static void countDay(){
    Scanner scan = new Scanner(System.in);
    int year=scan.nextInt();
    int month=scan.nextInt();
    int day=scan.nextInt();

    int totalDay=0;
    if(month>1){
        totalDay=countByMonth(month-1);
        if(month>2){
            if(isRunNian(year)){
                totalDay+=1;
            }
        }

    }
    totalDay+=day;

    System.out.println(totalDay);

}

static int countByMonth(int month){
    int totalDay=0;
    for(int i=1;i<=month;i++){
        if(i%2==1){
            totalDay+=31;
        }else{
            totalDay+=30;
        }
    }

    if(month>=2){
        totalDay-=2;
    }
    return totalDay;
}

static boolean isRunNian(int year){
    if(year%100==0){
        if(year%400==0){
            return true;
        }else{
            return false;
        }
    }else if(year%4==0){
        return true;
    }else{
        return false;
    }
}
static void makeNumber(){
    int[] four=new int[]{1,2,3,4};
    int count=0;
    int ge=0;
    int shi=0;
    int bai=0;
    for(int i=0;i<4;i++){

        for(int j=0;j<4;j++){
            if(j!=i){
                ge=four[j];
                for(int k=0;k<4;k++){
                    if(k!=i&&k!=j){
                        shi=four[k]*10;
                        for(int z=0;z<4;z++){
                            if(z!=k&&z!=j&&z!=i){
                            bai=four[z]*100;
                            count++;
                            System.out.println(bai+ge+shi);}
                        }
                    }
                }
            }
        }
    }
    System.out.println("*********************************"+count);
}

static double[] luoDi(int count){
    double[] a=new double[2];

    if(count==1){
        a[0]=100;
        a[1]=50;

    }
    else if(count==2){
        a[0]=200;
        a[1]=25;

    }
    else{
        double lastTotal=200;
        double lastJump=25;
        for(int i=3;i<=count;i++){
            lastTotal+=lastJump*2;
            lastJump/=2;

        }
        a[0]=lastTotal;
        a[1]=lastJump;

    }
    return a;

}

static boolean wanQuanShu(int test){
    int total=0;
    if(test==1||test==2){
        return false;
    }
    int sqr=(int) Math.sqrt(test);
    for(int i=1;i<=sqr;i++){
        if(test%i==0){
            total+=i;
            total+=test/i;
        }
    }
    total-=test;
    if(total==test){
        return true;
    }
    return false;
}
static void addNumber(){
    Scanner scan=new Scanner(System.in);
    int factor=scan.nextInt();
    int number=scan.nextInt();

    int total=0;
    for(int i=number;i>0;i--){
        total+=factor*tenFactor(number-i)*i;
    }
    System.out.println(total);

}
static int tenFactor(int i){
    int sum=1;
    for(;i>0;i--){
        sum*=10;
    }
    return sum;
}
static void countChar(){
    int number=0;
    int space=0;
    int letter=0;
    int others=0;
    Scanner scan= new Scanner(System.in);
    String str=scan.nextLine();
    System.out.println(str);
    char[] charArray=str.toCharArray();
    for(char i:charArray){
        System.out.print(i);
        if((i>='a'&&i<='z')||(i>='A'&&i<='Z')){
            letter++;
        }else if(i>='0'&&i<='9'){
            number++;
        }else if(i==' '){
            space++;
        }else{
            others++;
        }
    }
    System.out.println();
    System.out.println("number:"+number);
    System.out.println("other:"+others);
    System.out.println("letter:"+letter);
    System.out.println("space:"+space);
}

static int zuiDaGongYueShu(int a,int b){
    if(a<b){
        int temp =a;
        a=b;
        b=temp;
    }
    if(a%b==0){
        return b;
    }else{
        return zuiDaGongYueShu(b,a%b);
    } 
}

static int function(int month){
    int total=1;
    if(month==1||month==2){
        total=1;
    }
    int pre1=1;
    int pre2=1;
    if(month>2){

        int nextCount=3;
        while(nextCount<=month){
            total=pre1+pre2;
            int temp=pre1;
            pre1=total;
            pre2=temp;
            nextCount++;
        }
    }
    return total;
}

static String grade(double mark){
    String gra=(mark>=90)?"A":((mark>=60)?"B":"C");
    return gra;
}

static boolean shuShu(int test){
    if(test==2){
        return true;
    }
    int squrt=(int) Math.sqrt(test);
    for(int i=2;i<=squrt;i++){
        if(test%i==0){
            return false;
        }
    }
    return true;
}

static boolean isShuiXianHua(int test){
    int ge=test%10;
    int shi=(test/10)%10;
    int bai=(test/100);
    int sum = ge*ge*ge+shi*shi*shi+bai*bai*bai;
    if(sum==test){ 
        return true;
    }
    return false;
}



static void fenjie(int test){
    int yueShu1=(int) Math.sqrt(test);
    while(true){
        if(test%yueShu1==0){
            break;
        }else{
            yueShu1--;
        }
    }

    int yueShu2=test/yueShu1;
    if(shuShu(yueShu1)){
        System.out.println(yueShu1);
    }else{
        fenjie(yueShu1);
    }

    if(shuShu(yueShu2)){
        System.out.println(yueShu2);
    }else{
        fenjie(yueShu2);
    }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值