格子点の列挙
Perl がなかったので。力技。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | use strict;
my $PI = atan2(1, 1) * 4;
my $MAXR = int(sqrt(1000 / $PI) + sqrt(2) + 0.5);
my @res = ();
for (my $i = 0; $i <= $MAXR; $i++) {
for (my $j = 0; $j <= $MAXR; $j++) {
my $r = sqrt($i * $i + $j * $j);
push(@res, [$i, $j, $r, atan2($j, $i)]);
($i != 0) && push(@res, [-$i, $j, $r, atan2($j, -$i)]);
($j != 0) && push(@res, [$i, -$j, $r, atan2(-$j, $i) + 2 * $PI]);
($i * $j != 0) && push(@res, [-$i, -$j, $r, atan2(-$j, -$i) + 2 * $PI]);
}
}
foreach my $p (sort {($a->[2] <=> $b->[2]) || ($a->[3] <=> $b->[3])} @res) {
printf("%3d, %3d\n", splice(@{$p}, 0, 2));
}
|
Posted feedbacks - C++
円周をたどって。1000個目は -8, 16。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <vector>
#include <iostream>
using namespace std;
void lattice(int num) {
vector<pair<int, int>> v;
vector<pair<int, int>>::iterator ite;
int n = 1, r = 0, x_start = 0;
if (num <= 0) return;
cout << "0, 0" << endl;
while (n < num) {
if (r == x_start * x_start) ++x_start;
int x = x_start;
int y = 0;
int next_r = x * x;
while (x > 0) {
int d = x * x + y * y;
if (d <= r) {
++x; ++y;
} else {
if (d < next_r) {
v.clear();
v.push_back(pair<int, int>(x, y));
next_r = d;
} else if (d == next_r) {
v.push_back(pair<int, int>(x, y));
}
--x;
}
}
for (ite = v.begin(); ite != v.end() && n < num; ++ite, ++n)
cout << ite->first << ", " << ite->second << endl;
for (ite = v.begin(); ite != v.end() && n < num; ++ite, ++n)
cout << -ite->second << ", " << ite->first << endl;
for (ite = v.begin(); ite != v.end() && n < num; ++ite, ++n)
cout << -ite->first << ", " << -ite->second << endl;
for (ite = v.begin(); ite != v.end() && n < num; ++ite, ++n)
cout << ite->second << ", " << -ite->first << endl;
v.clear();
r = next_r;
}
}
int main(void) {
lattice(1000);
return 0;
}
|




かも
#3421()
Rating0/2=0.00
同じ距離の点はどういう順番でも構いませんが、可能であればX軸に一番近い第一象限の点から原点を中心として反時計回りの順に列挙してください。 列挙の方法は、1行に一つの点の、X,Y座標を出力することとします。
サンプル出力
最低でも1000件まで列挙できることを確認してください。 また「反時計回り」の条件も満たしている場合は、1000番目の頂点が何かも併せて答えてください。
このお題はかもさんの投稿を元にしています。ご協力ありがとうございました。
1 reply [ reply ]