头像

yoiy-sk




离线:26分钟前


最近来访(0)

活动打卡代码 AcWing 4213. 最小结果

yoiy-sk
1天前
import java.util.*;

public class Main{
    static long ans = (long) 1e18;
    static char[] op = new char[4];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Long> num = new ArrayList<Long>();
        for(int i = 0 ; i < 4 ; i++)  num.add(sc.nextLong());
        for(int i = 0 ; i < 3 ; i++)  op[i] = sc.next().charAt(0);
        dfs(num,0);
        System.out.println(ans);
    }
    static void dfs(List num,int u){
        if(num.size() == 1) ans = Math.min(ans, (Long) num.get(0));
        for(int i = 0 ; i < num.size(); i++){
            for (int j = i + 1; j < num.size(); j++) {
                List<Long> t = new ArrayList<>();
                for(int k = 0 ; k < num.size(); k++){
                    if(k != i && k != j) t.add((Long) num.get(k));
                }
                if(op[u] == '*') t.add((Long)num.get(i) * (Long)num.get(j));
                else t.add((Long)num.get(i) + (Long)num.get(j));

                dfs(t,u+1);
            }
        }
    }
}



活动打卡代码 AcWing 4214. 三元组

yoiy-sk
1天前
import java.util.*;
public class Main{
    static int N = 3010,INF = (int) 1e9;
    static int[] s = new int[N];
    static int[] c = new int[N];
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 0 ; i < n ; i++) s[i] = sc.nextInt();
        for(int i = 0 ; i < n ; i++) c[i] = sc.nextInt();

        int res = INF;
        for(int j = 0 ; j < n ; j++){
            //在s[i] < s[j] 的集合中找到最小的c[i]
            int left = INF;
            for(int i = 0 ; i < j ; i++){
                if(s[i] < s[j]){
                    left = Math.min(left,c[i]);
                }
            }
            //在s[k] > s[j] 的集合中找到最小的c[k]
            int right = INF;
            for(int k = j + 1; k < n ; k++){
                if(s[k] > s[j]){
                    right = Math.min(right,c[k]);
                }
            }
            res = Math.min(res,left + c[j] + right);
        }
        if(res == INF) System.out.println(-1);
        else System.out.println(res);
    }
}


活动打卡代码 AcWing 4212. 字符串比较

yoiy-sk
1天前
/*
    字符串比较,“比较第一个不同的字符”
    1、通过for 循环比较
    2、通过java 自带的 compareTo() 函数
    3、不进行大小写转换 通过compareToIngoreCase() 函数
*/
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String a = sc.next().toLowerCase();
        String b = sc.next().toLowerCase();
        // int res1 = 0 ;
        // for (int i = 0; i < a.length(); i++) {
        //     res1 = a.charAt(i)-b.charAt(i);
        //     if(res1 > 0) {
        //         System.out.println(1);
        //         break;
        //     }else if(res1 < 0){
        //         System.out.println(-1);
        //         break;
        //     }else if(i == a.length() - 1){
        //         System.out.println(0);
        //         break;
        //     }
        // }
        int res2 = a.compareTo(b);
        // int res2 = a.compareToIngoreCase(b);
        if(res2 > 0) System.out.println(1);
        else if(res2 == 0 )System.out.println(0);
        else System.out.println(-1);
    }
}



yoiy-sk
1天前
/*
    字符串比较,“比较第一个不同的字符”
    1、通过for 循环比较
    2、通过java 自带的 compareTo() 函数
    3、不进行大小写转换 通过compareToIngoreCase() 函数
*/
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String a = sc.next().toLowerCase();
        String b = sc.next().toLowerCase();
        // int res1 = 0 ;
        // for (int i = 0; i < a.length(); i++) {
        //     res1 = a.charAt(i)-b.charAt(i);
        //     if(res1 > 0) {
        //         System.out.println(1);
        //         break;
        //     }else if(res1 < 0){
        //         System.out.println(-1);
        //         break;
        //     }else if(i == a.length() - 1){
        //         System.out.println(0);
        //         break;
        //     }
        // }
        int res2 = a.compareTo(b);
        // int res2 = a.compareToIngoreCase(b);
        if(res2 > 0) System.out.println(1);
        else if(res2 == 0 )System.out.println(0);
        else System.out.println(-1);
    }
}


活动打卡代码 AcWing 868. 筛质数

yoiy-sk
2天前
import java.util.*;
public class Main{
    static int N = 10000010;
    static int primes[] = new int[N],cnt; //primes[]存储所有素数
    static boolean st[] = new boolean[N];//st[x]存储x是否被筛掉
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();   
        get_primes1(n);
        System.out.println(cnt);
    }
    //朴素筛法求素数 
    static void get_primes1(int n){
        for(int i = 2 ; i <= n ; i++){
            if(!st[i]){
                primes[cnt++] = n;  
                for(int j = i+i ; j <= n ; j += i) st[j] = true;//将每个质因子的倍数筛掉
            }
        }
    }
    //线性筛法求素数
    static void get_primes2(int n){
        for (int i = 2; i <= n; i ++ ){
            if (!st[i]) primes[cnt ++ ] = i;
            for (int j = 0; primes[j] <= n / i; j ++ )
            {
                st[primes[j] * i] = true;
                if (i % primes[j] == 0) break;
            }
        }
    }
}


活动打卡代码 AcWing 867. 分解质因数

yoiy-sk
2天前
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(n--> 0){
            int x = sc.nextInt();
            divide(x);
            System.out.println();
        }
    }
    static void divide(int n){
        for(int i = 2 ; i <= n / i ; i++){
            if(n % i == 0){
                int s = 0;
                while(n % i == 0){
                    n /= i;
                    s++;
                }
                System.out.println(i + " " + s);
            }
        }
        if(n > 1) System.out.println(n + " " + 1);
    }
}



yoiy-sk
2天前
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(n--> 0){
            int x = sc.nextInt();
            if(is_prime(x)) System.out.println("Yes");
            else System.out.println("No");
        }
    }
    static boolean is_prime(int n){
        if(n < 2 ) return false; 
        //每次枚举一半的质因数,(质因数成对存在)
        for(int i = 2 ; i <= n / i ; i++){
            if(n % i == 0){
                return false;
            }
        }
        return true;
    }
}


活动打卡代码 AcWing 1884. COW

yoiy-sk
2天前
import java.util.*;
public class Main{
    static int N = 100010;
    static int n;
    static char[] c = new char[N];
    static int pre_c[] = new int[N];//能共用c 的个数
    static int pre_co[] = new int[N];//能公用co 的个数
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        String s = sc.next();
        for(int i = 1 ; i <= n ;i++) c[i] = s.charAt(i-1);

        for(int i = 1 ; i <= n ; i++){
            if(c[i] == 'C'){
                pre_c[i] = 1;
            }
            pre_c[i] += pre_c[i-1];
            if(c[i] == 'O'){
                pre_co[i] = pre_c[i-1];
            }
            pre_co[i] += pre_co[i-1];
        }
        long res = 0L;
        for(int i = 3 ; i <= n ;i++){
            if(c[i] == 'W') res += pre_co[i];
        }
       // System.out.println(res);
    }

}


活动打卡代码 AcWing 1904. 奶牛慢跑

yoiy-sk
2天前
import java.util.*;
public class Main{
    static int N = 100010, INF = (int)2e9;
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] q = new int[N];
        for(int i = 0 ; i < n ; i++){
            int x = sc.nextInt();
            int v = sc.nextInt();
            q[i] = v;
        }
        //分为几段,每一段的最小值为小组队长
        int res = 0,vmin = INF;
        for (int i = n-1; i >=  0; i--) {
            if(q[i] <= vmin){
                res++;
                vmin = q[i];
            }
        }
        System.out.println(res);
    }
}



yoiy-sk
3天前
import java.util.*;

public class Main{
    static int n1,n2,m;
    static int N = 510, M = 100010;
    static int e[] = new int[M], ne[] = new int[M], h[] = new int[N],idx;
    static int match[] = new int[N];
    static boolean st[] = new boolean[N];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n1 = sc.nextInt() ; n2 = sc.nextInt();
        m = sc.nextInt();
        Arrays.fill(h,-1);
        while (m-->0){
            int a = sc.nextInt();
            int b = sc.nextInt();
            add(a,b);
        }

        int res = 0;
        for (int i = 1; i <= n1; i++) {
            Arrays.fill(st,false);
            if(find(i)) res++;
        }
        System.out.println(res);
    }
    static boolean find(int x){
        for (int i = h[x]; i != -1 ; i = ne[i]) {
            int j = e[i];
            if(!st[j]){
                st[j] = true;
                if(match[j] == 0 || find(match[j])){
                    match[j] = x;
                    return true;
                }
            }
        }
        return false;
    }
    static void add(int a, int b){
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}