笛卡尔邻居距离【难度:2级】:
答案1:
using System;
using System.Collections.Generic;
using System.Linq;
public class Kata
{
public static double[] cartesianNeighborsDistance(int x, int y, int range)
{
var set = new HashSet<double>();
for (int i = -range; i <= range; i++)
{
for (int j = -range; j <= range; j++)
{
if (!(i == 0 && j == 0))
set.Add(Math.Round(Math.Sqrt(i * i + j * j), 10));
}
}
return set.ToArray();
}
}
答案2:
using System;
using System.Linq;
using System.Collections.Generic;
public class Kata
{
public static double[] cartesianNeighborsDistance(int x, int y, int range)
{
List<double> temp = new List<double>();
for(int i = 1; i <= range; i++)
{
temp.Add(i);
for(int j = i; j >= 1; j--)
{
double check = Math.Round(Math.Sqrt(Math.Pow(i, 2) + Math.Pow(j, 2)), 10);
temp.Add(check);
}
}
return temp.Distinct().ToArray();
}
}
答案3:
using System;
using System.Linq;
using System.Collections.Generic;
public class Kata {
public static double[] cartesianNeighborsDistance(int x, int y, int range){
List<double>Distances=new List<double>();
for(int i=0;i<=range;i++)
for(int j=0;j<=range;j++)
if(!(i==0 && j==0))
Distances.Add(distance(x,y,x+i,y+j));
return Distances.Distinct().ToArray();
}
public static double distance(int x1,int y1,int x2,int y2)
{
return Math.Round(Math.Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)),10);
}
}
答案4:
using System;
using System.Linq;
using System.Collections.Generic;
public class Kata {
public static double[] cartesianNeighborsDistance(int x, int y, int range){
List<double> items = new List<double>();
range++;
for (int i = 0; i < range; i++)
{
double ii = Convert.ToDouble(i);
for (int j = 0; j < range; j++)
{
double jj = Convert.ToDouble(j);
items.Add(Math.Sqrt(Math.Pow(i,2) + Math.Pow(j,2)));
}
}
items.Remove(0);
double[] coords = items.Distinct().ToArray();
return coords;
}
}
答案5:
using System;
using System.Linq;
using System.Collections.Generic;
public class Kata
{
public static double[] cartesianNeighborsDistance(int x, int y, int range)
{
List<double> results = new List<double>();
for (int iy = y + 1; iy <= y + range; iy++)
{
for (int ix = x; ix <= x + range; ix++)
results.Add( Math.Sqrt( Math.Pow(ix - x, 2) + Math.Pow(iy - y, 2) ) );
}
return results.Distinct().ToArray();
}
}
答案6:
using System;
using System.Collections.Generic;
public class Kata
{
public static double[] cartesianNeighborsDistance(int x, int y, int range)
{
List<double> results = new List<double>();
double distance;
for (int i = range; i > 0; i--) {
for (int j = i; j >= 0; j--) {
distance = Math.Round(Math.Sqrt(j*j + i*i),10);
if (!results.Contains(distance)) { results.Add(distance); }
}
}
return results.ToArray();
}
}
答案7:
using System;
using System.Linq;
using System.Collections.Generic;
public class Kata {
public static double[] cartesianNeighborsDistance(int x, int y, int range){
List<double> distances = new List<double>();
for(int x0 = x - range; x0 <= x + range; x0++)
{
for(int y0 = y - range; y0 <= y; y0++)
{
double distance = getDistance(x0, y0, x, y);
if(!distances.Contains(distance) && distance > 0)
{
distances.Add(distance);
}
}
}
return distances.ToArray();
}
private static double getDistance(int x1, int y1, int x2, int y2)
{
return Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));
}
}
答案8:
using System;
using System.Collections.Generic;
public class Kata {
public static double[] cartesianNeighborsDistance(int x, int y, int range){
List<double[]> _list = new List<double[]>();
List<double> _list2 = new List<double>();
List<double> _list3 = new List<double>();
for (int i = 0; i < range; i++)
{
for (int j = 0; j < range; j++)
{
_list.Add(new double[] { i, j });
}
}
foreach (var item in _list)
{
_list2.Add(Math.Round(Math.Pow(Math.Pow(range - item[0], 2) + Math.Pow(range - item[1], 2), 0.5),10));
}
for (int i = 0; i < range; i++)
{
_list2.Add(range - i);
}
foreach (var item in _list2)
{
foreach (var el in _list3)
{
if (item == el)
{
_list3.Remove(el);
break;
}
}
_list3.Add(item);
}
double[] _answers = new double[_list3.Count];
for (int i = 0; i < _answers.Length; i++)
{
_answers[i] = _list3[i];
}
return _answers;
}
}
答案9:
using System;
using System.Collections.Generic;
using System.Linq;
public class Kata
{
public static double[] cartesianNeighborsDistance(int x, int y, int range)
{
return CreateCoordinates(x, y, range)
.Select(c => Math.Round(Math.Sqrt((c[0] - x) * (c[0] - x) + (c[1] - y) * (c[1] - y)), 10))
.Distinct().ToArray();
}
private static IEnumerable<double[]> CreateCoordinates(int x, int y, int range)
{
for (int cx = x - range; cx <= x + range; cx++)
{
for (int cy = y - range; cy <= y + range; cy++)
{
if (!(cx == x && cy == y))
yield return new[] { (double)cx, cy };
}
}
}
}
答案10:
using System;
using System.Collections.Generic;
using System.Linq;
public class Kata {
public static double[] cartesianNeighborsDistance(int x, int y, int range){
var result = new List<double>();
for (double i = x - range; i <= x + range; i++)
{
for (double j = y - range; j <= y + range; j++)
{
if (!(i == x && j == y))
{
double distace = Math.Sqrt(Math.Pow(x - i, 2) + Math.Pow(y - j, 2));
result.Add(distace);
}
}
}
return result.Distinct().ToArray();
}
}