栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于java语言写题的总结1.0

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于java语言写题的总结1.0

import java.io.*;

public class Rook{
	
	

	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		while(t-- > 0) {
			int n = Integer.parseInt(br.readLine());
			int smallest = Integer.MAX_VALUE;
			int costSmallest = 0;
			int largest = Integer.MIN_VALUE;
			int costLargest = 0;
			int longest = Integer.MIN_VALUE;
			int costLongest = 0;
			while(n-- > 0) {
				String[] input = br.readLine().split(" ");
				
				if(Integer.parseInt(input[0]) < smallest) {
					smallest = Integer.parseInt(input[0]);
					costSmallest = Integer.parseInt(input[2]);
				} else if(Integer.parseInt(input[0]) == smallest) {
					costSmallest = Math.min(costSmallest, Integer.parseInt(input[2]));
				}
				
				if(Integer.parseInt(input[1]) > largest) {
					largest = Integer.parseInt(input[1]);
					costLargest = Integer.parseInt(input[2]);
				} else if(Integer.parseInt(input[1]) == largest) {
					costLargest = Math.min(costLargest, Integer.parseInt(input[2]));
				}
				
				if(longest < (Integer.parseInt(input[1]) - Integer.parseInt(input[0]) + 1)) {
					longest = Integer.parseInt(input[1]) - Integer.parseInt(input[0]) + 1;
					costLongest = Integer.parseInt(input[2]);
				} else if(longest == (Integer.parseInt(input[1]) - Integer.parseInt(input[0]) + 1)) {
					costLongest = Math.min(costLongest, Integer.parseInt(input[2]));
				}
				
				int answer = costLargest + costSmallest;
				if(longest == largest - smallest + 1) {
					answer = Math.min(answer, costLongest);
				}
				
				System.out.println(answer);
			}
		}
	}

}

这是最近codeforces里比赛的一道题,在比赛中由于一直时间超限,导致没完成,上述的代码来自一位外国的大佬,作为以后做题的模板
总结:
1.在写题的过程中能不用for就不用。
2.输入数据大时,BufferedReader是一个好选择,注意要抓取IOEXCEPTION
3.向大佬学习

 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/697215.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号