2016 微软秋招(校招)在线笔试 题目1 : Farthest Point

题目1 : Farthest Point
时间限制:5000ms
单点时限:1000ms
内存限制:256MB
描述
Given a circle on a two-dimentional plane.

Output the integral point in or on the boundary of the circle which has the largest distance from the center.

输入
One line with three floats which are all accurate to three decimal places, indicating the coordinates of the center x, y and the radius r.

For 80% of the data: |x|,|y|<=1000, 1<=|r|<=1000

For 100% of the data: |x|,|y|<=100000, 1<=|r|<=100000

输出
One line with two integers separated by one space, indicating the answer.

If there are multiple answers, print the one with the largest x-coordinate.

If there are still multiple answers, print the one with the largest y-coordinate.

样例输入
1.000 1.000 5.000
样例输出
6 1


//未优化版本
import java.util.LinkedList;
import java.util.Scanner;

class Point{
    public long x;
    public long y;
    public Point(long X, long Y){
        this.x = X;
        this.y = Y;
    }
}
public class T1 {
    public static boolean isInCircle(long x, long y, double x0, double y0, double r){
        if(distance_2(x, y, x0, y0) <= r*r){
            return true;
        } else {
            return false;
        }
    }
    public static double distance_2(long x, long y, double x0, double y0){
        double deltaX = x - x0;
        double deltaY = y - y0;
        return deltaX * deltaX + deltaY * deltaY;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double x,y,r;
        x = input.nextDouble();
        y = input.nextDouble();
        r = input.nextDouble();
        long left = (int)(x - r), right = (int)(x + r), top = (int)(y + r), bottom = (int)(y - r);
        double maxDistance = 0;

        LinkedList<Point> points = new LinkedList<Point>();

        for(long i = left; i <= right; i++){
            if(isInCircle(i, top, x, y, r)){
                double curDistance = distance_2(i, top, x, y);
                if(curDistance >= maxDistance){
                    maxDistance = curDistance;
                    points.add(new Point(i, top));
                }
            }
        }
        for(long i = left; i <= right; i++){
            if(isInCircle(i, bottom, x, y, r)){
                double curDistance = distance_2(i, bottom, x, y);
                if(curDistance >= maxDistance){
                    maxDistance = curDistance;
                    points.add(new Point(i, bottom));
                }
            }
        }
        for(long i = bottom; i <= top; i++){
            if(isInCircle(left, i, x, y, r)){
                double curDistance = distance_2(left, i, x, y);
                if(curDistance >= maxDistance){
                    maxDistance = curDistance;
                    points.add(new Point(left, i));
                }
            }
        }

        for(long i = bottom; i <= top; i++){
            if(isInCircle(right, i, x, y, r)){
                double curDistance = distance_2(right, i, x, y);
                if(curDistance >= maxDistance){
                    maxDistance = curDistance;
                    points.add(new Point(right, i));
                }
            }
        }

        for(Point pt : points){
            if(distance_2(pt.x,pt.y, x, y) < maxDistance){
                points.remove(pt);
            }
        }

        long maxX = points.get(0).x;
        LinkedList<Point> resultPts = new LinkedList<Point>();
        for(Point pt : points){
            if(pt.x > maxX){
                maxX = pt.x;
            }
        }
        for(Point pt : points){
            if(pt.x == maxX){
                resultPts.add(pt);
            }
        }

        Point resultPt = resultPts.get(0);

        for(Point pt : resultPts){
            if(pt.y > resultPt.y){
                resultPt = pt;
            }
        }
        System.out.println(resultPt.x+" "+resultPt.y);
    }

}
当然可以!下面是一个使用Farthest Point Sampling (FPS)算法找出文件中距离最远的3张图像,并返回图像名的Python代码示例: ```python import os import numpy as np from PIL import Image def compute_distance(image1, image2): # 计算两张图像的距离,可以使用任意方法 # 这里以欧氏距离为例 return np.linalg.norm(image1 - image2) def farthest_point_sampling(images_dir, num_samples=3): # 获取目录下所有图像文件名 image_files = [file for file in os.listdir(images_dir) if file.endswith('.jpg') or file.endswith('.png')] # 随机选择第一个样本 sample_indices = [np.random.randint(len(image_files))] for _ in range(num_samples-1): max_distance = float('-inf') farthest_index = None # 遍历所有图像文件 for i, image_file in enumerate(image_files): if i not in sample_indices: # 加载图像并转换为灰度图 image_path = os.path.join(images_dir, image_file) image = np.array(Image.open(image_path).convert('L')) # 计算当前图像与已选择样本的距离之和 total_distance = 0 for index in sample_indices: sample_path = os.path.join(images_dir, image_files[index]) sample_image = np.array(Image.open(sample_path).convert('L')) total_distance += compute_distance(image, sample_image) # 更新最远距离和索引 if total_distance > max_distance: max_distance = total_distance farthest_index = i # 将最远的图像索引添加到样本列表中 sample_indices.append(farthest_index) # 返回最远的图像文件名 return [image_files[index] for index in sample_indices] # 示例使用 images_directory = 'path/to/images' farthest_images = farthest_point_sampling(images_directory, num_samples=3) print(farthest_images) ``` 你需要将 `images_directory` 替换为包含图像文件的实际路径。此代码将在指定目录中找到距离最远的3张图像,并返回它们的文件名。请确保在运行代码之前安装必要的依赖项(numpy、PIL)。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值