• ## Distance

千次阅读 2018-04-22 18:42:45
1191: Distance时间限制: 1 Sec 内存限制: 32 MB题目描述There is a battle field. It is a square with the side length 100 miles, and unfortunately we have two comrades who get hurt still in the battle ...
1191: Distance时间限制: 1 Sec  内存限制: 32 MB题目描述There is a battle field. It is a square with the side length 100 miles, and unfortunately we have two comrades who get hurt still in the battle field. They are in different positions. You have to save them. Now I give you the positions of them, and you should choose a straight way and drive a car to get them. Of course you should cross the battle field, since it is dangerous, you want to leave it as quickly as you can!输入There are many test cases. Each test case contains four floating number, indicating the two comrades' positions (x1,y1), (x2,y2).Proceed to the end of file.输出you should output the mileage which you drive in the battle field. The result should be accurate up to 2 decimals.样例输入1.0 2.0 3.0 4.0
15.0 23.0 46.5 7.0样例输出140.01
67.61提示The battle field is a square local at (0,0),(0,100),(100,0),(100,100).解题思路首先计算两点所确定的直线l，算出直线方程。计算出直线在x=0上的截距a和在x=100上的截距b，通过比较他们与0和100的关系可确定直线所处的位置，进而可利用勾股定理求出l在正方形内部的长度。#include <stdio.h>
#include <math.h>
int main()
{
double x1, x2, y1, y2, x, y, s, k, a, b;
while (~scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2))
{
if (x1 == x2 || y1 == y2)
{
printf("100.00\n");
continue;
}
k = (y1 - y2) / (x1 - x2);                          /*算出直线的斜率*/
a = y1 - k * x1;                                    /*算出直线在直线x=0上的截距a*/
b = y1 - k * (x1 - 100);                            /*算出直线在直线x=100上的截距b*/
if (a >= 100)
{
if (b < 0)
{
x = x1 - (y1 - 100) / k;
y = (x1 - y1 / k) - x;
s = sqrt(10000 + y * y);
printf("%.2f\n", s);
}
else if (b < 100)
{
x = 100 - (x1 - (y1 - 100) / k);
y = 100 - b;
s = sqrt(x * x + y * y);
printf("%.2f\n", s);
}
else printf("0.00\n");
}
else if (a >= 0)
{
if (b >= 100)
{
x = x1 - (y1 - 100) / k;
y = 100 - a;
s = sqrt(x * x + y * y);
printf("%.2f\n", s);
}
else if (b >= 0)
{
x = 100;
y = fabs(a - b);
s = sqrt(x * x + y * y);
printf("%.2f\n", s);
}
else
{
x = x1 - y1 / k;
y = a;
s = sqrt(x * x + y * y);
printf("%.2f\n", s);
}
}
else
{
if (b >= 100)
{
x = x1 - (y1 - 100) / k;
y = x - (x1 - y1 / k);
s = sqrt(10000 + y * y);
printf("%.2f\n", s);
}
else if (b > 0)
{
x = 100 - (x1 - y1 / k);
y = b;
s = sqrt(x * x + y * y);
printf("%.2f\n", s);
}
else printf("0.00\n");
}
}
return 0;
}
展开全文
• distance = a1.distance(a2) row.append(str(round(distance, precision))) tableVals.append(row) #Produce table table = pylab.table(rowLabels = rowLabels, colLabels = columnLabels, cellText = ...
# quote from 'introduction to computation and programming
# using Python, revised, MIT press
import pylab

def minkowskiDist(v1, v2, p):
"""Assumes v1 and v2 are equal-length arrays of numbers
Returns Minkowski distance of order p between v1 and v2"""
dist = 0.0
for i in range(len(v1)):
dist += abs(v1[i] - v2[i])**p
return dist**(1.0/p)

class Animal(object):
def __init__(self, name, features):
"""Assumes name a string; features a list of numbers"""
self.name = name
self.features = pylab.array(features)

def getName(self):
return self.name

def getFeatures(self):
return self.features

def distance(self, other):
"""Assumes other an animal
Returns the Euclidean distance between feature vectors
of self and other"""
return minkowskiDist(self.getFeatures(),
other.getFeatures(), 2)

def compareAnimals(animals, precision):
"""Assumes animals is a list of animals, precision an int >= 0
Builds a table of Euclidean distance between each animal"""
#Get labels for columns and rows
columnLabels = []
for a in animals:
columnLabels.append(a.getName())
rowLabels = columnLabels[:]
tableVals = []
#Get distances between pairs of animals
#For each row
for a1 in animals:
row = []
#For each column
for a2 in animals:
if a1 == a2:
row.append('--')
else:
distance = a1.distance(a2)
row.append(str(round(distance, precision)))
tableVals.append(row)
#Produce table
table = pylab.table(rowLabels = rowLabels,
colLabels = columnLabels,
cellText = tableVals,
cellLoc = 'center',
loc = 'center',
colWidths = [0.2]*len(animals))
table.scale(1, 2.5)
pylab.axis('off') #Don't display x and y-axes
pylab.savefig('distances')

rattlesnake = Animal('rattlesnake', [1,1,1,1,0])
boa = Animal('boa\nconstrictor', [0,1,0,1,0])
dartFrog = Animal('dart frog', [1,0,1,0,5])
alligator = Animal('alligator', [1,1,0,1,4])
animals = [rattlesnake, boa, dartFrog, alligator]
compareAnimals(animals, 3)
展开全文
• <div><p>There is no distance in Orange that can be used for distance calculations between columns in data with discrete attributes. <p>Implement Hamming distance in distance.py and add it to the ...
• L1distance和L2distance区别： L1distance即曼哈顿，即城市距离，各维度距离之和 L2distance即欧式距离，两点间直线距离 参考cs231n课程的一幅图，图上方形和圆形上的点都有相同距离。
L1distance和L2distance区别：
L1distance即曼哈顿，即城市距离，各维度距离之和
L2distance即欧式距离，两点间直线距离
参考cs231n课程的一幅图，图上方形和圆形上的点都有相同距离。


展开全文
• It removes the stepper <code>step_distance</code> parameter and introduces a new <code>rotation_distance</code> parameter. <p>I'm proposing two major phases for this change. The first phase would...
• (From wikipedia) For binary strings a and b the Hamming distance is equal to the number of ones in a XOR b. For calculating Hamming distance between two strings a and b, they must have equal length. ...
• Hamming Distance The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Examp...
Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, calculate the Hamming distance.

Example

Input: x = 1, y = 4
Output: 2
Explanation:
1   (0 0 0 1)
4   (0 1 0 0)
↑       ↑
The above arrows point to positions where the corresponding bits are different.

Solution
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
return bin(x^y)[2::].count('1')



展开全文
• 今天写个程序，要用到一个字符串与另一个字符串之间的距离(两个字符串对应位的字母不同的个数)，于是自己写了个distance函数：int distance(const char *p1,const char *p2);但是结果跟自己想的不一样，但是如果把...
• <div><p>Change the return type of <code>distance</code> and <code>comparable_distance</code> free functions. In more detail: - <code>distance</code> returns <code>distance_result<...>::type...
• <p>Currently it looks like rust-geo calculates distance with Euclidean distance, so it's in degrees. That can be useful, but is not typical for geo math: instead, Haversine distance or Vincenty ...
• The conic coordinates of a point p lying on the surface of the cone are two numbers: the first, d, is the distance from the tip of the cone to p and the second, A , is the angle in degrees between ...
• Pearson Distance: where: 1. is the covariance 2. is the standard deviation of 3. is the standard deviation of pearson distance: Whe...
• Distance Field Global illumination http://timhobsonue4.snappages.com/distance-field-global-illumination ...
• Given a tree, calculate the average distance between two vertices in the tree. For example, the average distance between two vertices in the following tree is (d01 + d02 + d03 + d04 + d12 +d13 +d14 +...
• 本篇内容将讲述Edit Distance（编辑距离），具体包括4个方面： Defining Minimum Edit Distance Computing Minimum Edit Distance Backtrace for Computing Alignments Weighted Minimum Edit Distance Definition ...
• Edit Distance (Levenshtein Distance)Find the edit distance between given two words s1 and s2.The disntace is the minimum number of single-character edits required to change one word into the other....
• Distance computations - Cosine distance - 余弦距离 Distance computations (scipy.spatial.distance) https://docs.scipy.org/doc/scipy/reference/spatial.distance.html computation [kɒmpjʊ'teɪʃ(ə)n]：n...
• distance 表示一个cpu访问其他cpu的距离，一般用一个数字表示，例如： 需要调用numa_set_distance 来设定一个一唯数组来表示 void __init numa_set_distance(int from, int to, int distance) {  numa_...
• 这个项目只有一类距离。...distance = Distance . new distance . kilometers = 10 distance . miles #=> 6.2137119... 笔记 MILLIMETER_IN_EACH字典是自省的要点。 您必须添加单位的单字，方法将为您元编程定义。
• Plagiarism is a form of academic dishonesty. To fight with such misconducts, plagiarism ... The key is that given two documents, how to judge their similarity, or in other words, document distance?
• Levenshtein distanceLevenshtein distance Levenshtein distance 简介 算法 java实现 Levenshtein distance 简介 Levenshtein distance，最短编辑距离，计算2个字符串的最短距离，即变换次数， 规则为： 1.修改一...
• In information theory, Linguistics and computer science, the Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between ...
• Py之distancedistance的简介、安装、使用方法之详细攻略 目录 distance的简介 distance的安装 distance的使用方法 1、编辑距离、汉明距离、sorensen相似系数、jaccard系数、ifast_comp distance的...
• What it actually do, is replaced most checks, which involves distance measurement to check is actor closer/further than some specific distance, to use distance squared instead which increase ...

...