pta的一些题

1折半查找(二分查找)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int c=sc.nextInt();
        int[] num=new int[c];
        for(int i=0;i<c;i++){
            num[i]=sc.nextInt();
        }
        int s=sc.nextInt();
        int l=0;
        int r=num.length-1;
        int y=0;
        boolean flag=true;
        int count=0;
        while(l<=r){
            y=(l+r)/2;
            count+=1;
            if(num[y]==s){
                flag=false;
                System.out.println("总共比较了"+count+"次");
                System.out.print("下标位置"+y);
            }
            if(num[y]<s){
                l=y+1;
            }
            else{
                r=y-1;
            }
        }
        if(flag){
            System.out.println("总共比较了"+count+"次");
            System.out.print("下标位置"+"-1");}
    }
}
 

2队列操作

import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Queue<Integer> c = new LinkedList<Integer>();
        int n=sc.nextInt();
        for(int i=0;i<n;i++){
           int x=sc.nextInt();
           if(x==1){
               int b=sc.nextInt();
               c.offer(b);
           }else{
               if(c.size()==0){
                    if(i!=n-1)System.out.println("invalid");
                    else System.out.print("invalid");
               }else{
                   int s=c.remove();
                   if(i!=n-1)System.out.println(s);
                   else System.out.print(s);
               }
           }
        }
    }
}

3前k个最大的数

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num=sc.nextInt();
        ArrayList<Integer> s= new ArrayList<Integer>();
        for(int i=0;i<num;i++){
            int c=sc.nextInt();
            s.add(c);
        }
        Collections.sort(s);
        int y=sc.nextInt();
        for(int i=num-1;i>=0 && y!=0;i--,y--){
            System.out.print(s.get(i)+" ");
        }
    }
}

4顺序查找 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a=in.nextInt();
        int []n=new int[a];
        for(int i=0;i<a;i++){
            n[i]=in.nextInt();
        }
        int b=in.nextInt();
        int x=0,y=a-1;
        
        boolean flag=true;
        while(x<=y){
        int N=(x+y)/2;
        if (n[N]==b){
            flag=false;
            System.out.print(N);
           }
        if(n[N]<b){
            x=N+1;
        }
        else{
            y=N-1;
            }
        }
        if (flag){
        System.out.print("-1");
    
        }
    }
}
5括号匹配

import java.util.Scanner;
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String a=input.nextLine();
        char[] b=new char[a.length()];
        for(int i=0;i<a.length();i++){
            b[i]=a.charAt(i);
        }
        ArrayList<Character> c=new ArrayList<Character>();
        if(a.length()<=1){
            System.out.print("括号不匹配");
        }
        for(char i:b){
            if(i=='{' || i=='(' || i=='['){
                c.add(i);
            }
            if(c.size()!=0 && i==')' && c.get(c.size()-1)=='('){
                c.remove(c.size()-1);
            }
            if(c.size()!=0 && i==']' && c.get(c.size()-1)=='['){
                c.remove(c.size()-1);
            }
            if(c.size()!=0 && i=='}' && c.get(c.size()-1)=='{'){
                c.remove(c.size()-1);
            }
            if(c.size()!=0 && i==']' && c.get(c.size()-1)=='['){
                c.remove(c.size()-1);
            }
        }
        if(c.size()==0){
            System.out.print("括号匹配");
        }
        else{System.out.print("括号不匹配");}
    }
}
 

6判断回文

import java.util.Scanner;
public class Main{  
public static void main(String[] args) {  
    String str = "";  
    Scanner input = new Scanner(System.in);  
    str = input.next();  
    StringBuffer temp = new StringBuffer(str);  
    temp.reverse();
    int count = 0;  
    for (int i = 0; i < str.length(); i++) {  
        if (str.charAt(i) == temp.charAt(i)) {  
            count++;  
        }  
    }  
    if (count == str.length()) {   
        System.out.print("是回文");  
    } else {  
        System.out.print("不是回文");  
    }  
}  
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值