在Go语言中,可以使用以下代码来比较两个尺寸的相似程度:

package main

import (
	"fmt"
	"math"
)

type Size struct {
	Width  int
	Height int
}

func main() {
	size1 := Size{
		Width:  200,
		Height: 400,
	}

	size2 := Size{
		Width:  300,
		Height: 400,
	}

	size3 := Size{
		Width:  200,
		Height: 300,
	}

	distance1 := math.Sqrt(math.Pow(float64(size1.Width-size2.Width), 2) + math.Pow(float64(size1.Height-size2.Height), 2))
	distance2 := math.Sqrt(math.Pow(float64(size1.Width-size3.Width), 2) + math.Pow(float64(size1.Height-size3.Height), 2))

	if distance1 < distance2 {
		fmt.Println("尺寸1与尺寸2最相近")
	} else if distance2 < distance1 {
		fmt.Println("尺寸1与尺寸3最相近")
	} else {
		fmt.Println("尺寸1与尺寸2、尺寸3相同距离")
	}
}

上述代码定义了一个Size结构体,包含宽度和高度两个字段。然后,分别创建了三个Size结构体实例,表示三种尺寸。

使用欧几里得距离公式计算了尺寸1与尺寸2、尺寸3之间的距离(差异)。然后,比较距离大小,输出最相近的尺寸。

运行以上代码,输出结果为:"尺寸1与尺寸3最相近"。表示尺寸1与尺寸3的差异最小,最相近

go语言 写出如何比较宽200高400 与宽300高400 宽200高300 哪个尺寸最相近

原文地址: https://www.cveoy.top/t/topic/hNSR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录