算法记录贴

ACM模式输出模板:

import java.util.Scanner;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
}
}

输入浮点:float n = sc.nextFloat();

取整:

1.Math.round(n)

2.ans-(int)ans>=0.5?(int)(ans+1):(int)ans

HJ6质数因子:功能:输入一个正整数,按照从小到大的顺序输出它的所有质因子(重复的也要列举)(如180的质因子为2 2 3 3 5 )

hashMap遍历:

for(Map.Entry num : map.entry.entrySet()){

        System.out.println(num.getKey() + " " +num.getValue());

}

栈遍历:

        Stack<String> st = new Stack<>();
        String[] a = str.split(" ");
        for(int i = 0;i<=a.length-1;i++){
            st.push(a[i]);
        }
        while(!st.isEmpty()){
            System.out.print(st.pop()+" ");
        }

不重复线性单链表:

linkedHashSet set = new LinkedHashSet<String>();

双端链表+字符串连接:

        Deque<String> dq = new LinkedList<>();
        String[] a = str.split(" ");
        for(int i = 0;i<=a.length-1;i++){
            dq.addFirst(a[i]);
        }
        System.out.println(String.join(" ",dq));

优先级队列:

        PriorityQueue<String> pq = new PriorityQueue<>();
        while((n--)>=0){
        String str = sc.nextLine();
            pq.offer(str);
        }
        while(!pq.isEmpty()){
            System.out.println(pq.poll());
        }

Iterator迭代器遍历:

Iterator it = set.iterator();

while(it.hasNext()){

        System.out.print(it.next());

}

map便利四种方法:

Entry:

Iterator<Map.Entry<Integer,Integer>> it = map.entrySet().iterator();

while(it.hasNext()){

        Map.Entry<Ineger,Integer> entry = it.next();

        int key = entry.getKey();

        int value = enry.getValue();

}

enrySet:

Set<Map.entry<String,String>> entrySet = map.entrySet();

for(Map.Entry<String,String> entry : entrySet){

        entry.getKey();

        entry.getValue();

}

KeySet:

Set<String> set = map.keySet();

for(String s : set){

        map.get(s);

}

构造字符串:

StringBuffer sb = new StringBuffer(str);

StringBuilder sb = new StringBuilder(str);

正则判断匹配条件:

public static boolean isValid(String s){
        String pattren = "[AWSD]\\d+{1,2}";  //HJ17 是否AWSD开头+两位数字
        return s.matches(pattren);
    }

Pattern p1 = Pattern.compile("[A-Z]"); //大写A-Z HJ20
Pattern p = Pattern.compile("[a-z]");  //小写a-z
Pattern p = Pattern.compile("[0-9]");  //数字0-9
Pattern p = Pattern.compile("[^a-zA-Z0-9]"); //不属于以上规则
if(p1.matcher(str).find()){return true;}

HJ6求质因子数:

import java.util.Scanner;
import java.util.ArrayList;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 2;i<=n;i++){  //给k值赋值最小质数2
            while(n!=i){
                if(n%i==0){
                    System.out.print(i+" ");
                    n=n/i;
                }else{
                    break;  //如果不能整除,那么就让k++(先跳出while在++)
                }
            }
        }
        System.out.print(n + " ");
    }
}

HJ13句子逆序的逆序:

import java.util.*;
import java.lang.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();

        StringBuffer sb = new StringBuffer();
        Stack<String> st = new Stack<>();
        String[] a = str.split(" ");
        for(int i = 0;i<=a.length-1;i++){
            st.push(a[i]);
        }
        while(!st.isEmpty()){
            sb.append(st.pop()+" ");
        }
        System.out.print(sb.reverse());
    }
}
输入:
I am a boy
输出:
boy a am I
实际输出:
I ma a yob

HJ14 字符串排序(实现比较器比较排序):

import java.util.*;
import java.lang.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
//         实现比较器:
        List<String> list = new ArrayList<>();
        while((n--)>=0){
            String str = sc.nextLine();
            list.add(str);
        }
        sc.close();
        list.sort(new Comparator<String>(){
            @Override
            public int compare(String s1,String s2){
                int i = 0;
                while(i < s1.length() && i<s2.length()){
                    if(s1.charAt(i) != s2.charAt(i)){
                        return (s1.charAt(i) > s2.charAt(i))?1:-1;
                    }
                    i++;
                }
                if(s1.length() == s2.length()){
                    return 0;
                }else{
                    return (s1.length() > s2.length())?1:-1;
                }
            }
        });
        list.remove(0);
        for(int i = 0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }
}

HJ16 购物单(动态规划)

HJ19 简单错误记录(字符串应用练习)

HJ20密码验证(正则判断)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值