蓝桥算法训练营—普及组—2023年2.9日

P1781 宇宙总统

https://www.luogu.com.cn/problem/P1781

字符串比较

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
    
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) throws Exception{
    	
    	int n = Integer.parseInt(in.readLine());
    	int idx = -1;
    	String ans = "0";
    	for (int i = 1; i <= n; i++) {
    		String ss = in.readLine();
    		int len = ss.length(), m = ans.length();
    		if (len > m) {
    			idx = i;
    			ans = ss;
    		} else if (len < m) {
    			continue;
    		} else if (ss.compareTo(ans) > 0){
    			idx = i;
    			ans = ss;			
    		}
    		
    	}
    	out.println(idx);
    	out.println(ans);
    	
    	out.flush();
    	in.close();
    }  
}

P1223 排队接水

https://www.luogu.com.cn/problem/P1223

贪心

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
    
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
    public static void main(String[] args) throws Exception{
    	
    	int n = Integer.parseInt(in.readLine().trim());
    	int[][] arr = new int[n][2];
    	String[] ss = in.readLine().split(" ");
    	for (int i = 0; i < n; i++) {
    		arr[i][0] = i + 1;
    		arr[i][1] = Integer.parseInt(ss[i]);
    	}
    	Arrays.parallelSort(arr, (a, b) -> (a[1] - b[1]));
    	double tot = 0, cur = 0;
    	for (int i = 0; i < n; i++) {
    		out.print(arr[i][0] + " ");
    		tot += cur;
    		cur += arr[i][1];
    	}
    	out.printf("\n%.2f", tot / n);

    	
    	out.flush();
    	in.close();
    }  
}

P3817 小A的糖果

https://www.luogu.com.cn/problem/P3817

贪心

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
    
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
    public static void main(String[] args) throws Exception{
    	
    	String[] nx = in.readLine().split(" ");
    	int n = Integer.parseInt(nx[0]), x = Integer.parseInt(nx[1]);
    	int[] arr = new int[n];
    	String[] ss = in.readLine().split(" ");
    	long ans = 0;
    	for (int i = 0; i < n; i++) {
    		arr[i] = Integer.parseInt(ss[i]);
    		if (i > 0 && arr[i] + arr[i - 1] > x) {
    			ans += arr[i] - x + arr[i - 1];
    			arr[i] = x - arr[i - 1];
    		}
    	}
    	
    	out.println(ans);
    	
    	out.flush();
    	in.close();
    }  
}

P1007 独木桥

https://www.luogu.com.cn/problem/P1007

贪心

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
    
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
    public static void main(String[] args) throws Exception{
    	
    	int l = Integer.parseInt(in.readLine()), n = Integer.parseInt(in.readLine());
    	int mn = 0, mx = 0;
    	String[] ss = null;
    	if (n > 0) ss = in.readLine().split(" ");
    	for (int i = 0; i < n; i++) {
    		int t = Integer.parseInt(ss[i]);
    		mn = Math.max(mn, Math.min(t, l + 1 - t));
    		mx = Math.max(mx, Math.max(t, l + 1 - t));
    	}
    	out.println(mn + " " + mx);
    	
    	out.flush();
    	in.close();
    }  
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

⠀One0ne

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值