找出多组分辨率中最接近目标值的一组

java 复制代码
package com.test;

import java.util.ArrayList;
import java.util.List;

public class Test {
	
	public static void main(String[] args) {
		
		// 目标分辨率
		int targetWidth = 640;
		int targetHeight = 480;
		
		//String str = "3264x2448,3264x1836,2560x1920,3264x1472,3200x1440,2304x1728,2560x1440,1920x1920,2048x1536,2304x1296,1920x1440,2400x1080,1920x1080";
		String str = "2560x1920,3264x1472,3200x1440,2304x1728,2560x1440,1920x1920,2048x1536,2304x1296,1920x1440,2400x1080,2304x1040,1920x1080,1632x1224,1600x1200,1440x1080,1280x960,1088x1088,1600x720,1280x720,960x540,800x600,800x480,720x480,352x288,320x240,176x144";
		
		String[] ary1 = str.split(",");
		List<String> list = new ArrayList<String>();
		
		for (int i = 0; i < ary1.length; i++) {
			String sss = ary1[i];
			list.add(sss);
		}
		
		//获取和目标值最接近的一组分辨率
		String s = getBestResolution(list,targetWidth,targetHeight);
		
		// 输出最接近目标分辨率的数据
		System.out.println("最接近目标分辨率的数据为:"+s);
		
	}

	/**
	 * 	获取和目标值最接近的一组分辨率
	 * @param list 分辨率集合,例如:[960x540,800x600,800x480,720x480,352x288,320x240,176x144]
	 * @param targetWidth 目标分辨率宽,例如:640
	 * @param targetHeight 目标分辨率高,例如:480
	 * @return 和目标值最接近的一组分辨率,例如:720x480
	 */
	public static String getBestResolution(List<String>list,int targetWidth,int targetHeight) {
		// 数据集合
		List<Resolution> resolutions = new ArrayList<Resolution>();
		
		for (int i = 0; i < list.size(); i++) {
			String sss = list.get(i);
			String[] ary2 = sss.split("x");
			Resolution resolution = new Resolution(Integer.parseInt(ary2[0]), Integer.parseInt(ary2[1]));
			resolutions.add(resolution);
		}
		
		// 初始化最小差距为最大值
		int minDifference = Integer.MAX_VALUE;
		Resolution closestResolution = null;

		// 遍历数据集合,找到最接近目标分辨率的数据
		for (Resolution resolution : resolutions) {
			int difference = Math.abs(resolution.width - targetWidth) + Math.abs(resolution.height - targetHeight);
			if (difference < minDifference) {
				minDifference = difference;
				closestResolution = resolution;
			}
		}
		String res = String.format("%dx%d",closestResolution.width,closestResolution.height);
		return res;
	}

	// 分辨率类
	static class Resolution {
		int width;
		int height;

		public Resolution(int width, int height) {
			this.width = width;
			this.height = height;
		}
	}
}

输出结果

java 复制代码
最接近目标分辨率的数据为:720x480
相关推荐
字节源流2 小时前
关于maven的依赖下不下来的问题
java·maven
pjx9872 小时前
服务间的“握手”:OpenFeign声明式调用与客户端负载均衡
java·运维·spring·负载均衡
prinrf('千寻)3 小时前
MyBatis-Plus 的 updateById 方法不更新 null 值属性的问题
java·开发语言·mybatis
老华带你飞3 小时前
实习记录小程序|基于SSM+Vue的实习记录小程序设计与实现(源码+数据库+文档)
java·数据库·spring boot·小程序·论文·毕设·实习记录小程序
在未来等你3 小时前
互联网大厂Java求职面试:AI与大模型应用集成及云原生挑战
java·微服务·ai·kubernetes·大模型·embedding·spring ai
源码技术栈4 小时前
SaaS基于云计算、大数据的Java云HIS平台信息化系统源码
java·大数据·云计算·云his·his系统·云医院·区域his
编程、小哥哥4 小时前
互联网大厂Java面试:从Spring Boot到微服务架构的技术深挖
java·spring boot·redis·微服务·prometheus·面试技巧
揽你·入怀4 小时前
数据结构:ArrayList简单实现与常见操作实例详解
java·开发语言
shandianchengzi4 小时前
【记录】Windows|竖屏怎么调整分辨率使横竖双屏互动鼠标丝滑
windows·计算机外设·显示器·鼠标·分辨率·双屏
okok__TXF4 小时前
SpringBoot3+AI
java·人工智能·spring