teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2020/07/22 06:08

投稿

episteme
episteme

スコア16612

answer CHANGED
@@ -13,4 +13,116 @@
13
13
  cities[2]~cities[市の数-1] の中から 八王子市(35.67,139.32)に最も近い
14
14
  cities[x]を見つけ、cities[2]と交換する
15
15
 
16
- これだけやれば、距離が近い市3位までを cities[0],cities[1],cities[2] に求めたことになります。
16
+ これだけやれば、距離が近い市3位までを cities[0],cities[1],cities[2] に求めたことになります。
17
+
18
+ [追記] 15分で書いたやつ:
19
+ ```C
20
+ #define _CRT_SECURE_NO_WARNINGS /* disable warning only for VC++ */
21
+ #include <stdio.h>
22
+ #include <stdio.h>
23
+ #include <stdlib.h>
24
+ #include <math.h>
25
+ #include <string.h>
26
+
27
+ typedef struct {
28
+ char name[32];
29
+ double lat;
30
+ double lng;
31
+ } City;
32
+
33
+ // ファイル:path を読み、City列を *pcities に求める
34
+ // 都市の数を返す
35
+ int build_cities(const char* path, City** pcities) {
36
+ int n;
37
+ City* c;
38
+ FILE* fp = fopen(path,"r");
39
+ if ( fp == NULL ) return 0;
40
+ fscanf(fp, "%d", &n);
41
+ c = (City*)malloc(sizeof(City)*n);
42
+ if ( c != NULL ) {
43
+ int i;
44
+ *pcities = c;
45
+ for ( i = 0; i < n; ++i ) {
46
+ fscanf(fp, "%s %lf %lf", c[i].name, &c[i].lat, &c[i].lng);
47
+ }
48
+ }
49
+ fclose(fp);
50
+ return n;
51
+ }
52
+
53
+ void print_city(const City* city) {
54
+ printf("%s %lf %lf\n", city->name, city->lat, city->lng);
55
+ }
56
+
57
+ void print_cities(const City* cities, int ncities) {
58
+ int i;
59
+ for (i = 0; i < ncities; ++i) {
60
+ print_city(&cities[i]);
61
+ }
62
+ }
63
+
64
+ // cities[begin]~cities[ncities-1] の中からtargetに最も近い cities[n] を求め n を返す。
65
+ // 2都市間の距離は fdistance により算出する
66
+ int nearest_city(const City* cities, int ncities, int begin,
67
+ const City* target, double (*fdistance)(const City*, const City*) ) {
68
+ int i;
69
+ int nearest = 0;
70
+ double nearest_distance = 0.0;
71
+ for ( i = begin; i < ncities; ++i ) {
72
+ double distance = fdistance(&cities[i], target);
73
+ if ( i == begin || nearest_distance > distance ) {
74
+ nearest = i;
75
+ nearest_distance = distance;
76
+ }
77
+ }
78
+ return nearest;
79
+ }
80
+
81
+ // 都市 a と b を交換する
82
+ void swap_city(City* a, City* b) {
83
+ City tmp = *a;
84
+ *a = *b;
85
+ *b = tmp;
86
+ }
87
+
88
+ // (なんちゃって)マンハッタン距離
89
+ double manhattan_distance(const City* a, const City* b) {
90
+ return fabs(a->lng - b->lng) + fabs(a->lng - b->lng);
91
+ }
92
+
93
+ int main(int argc, char* argv[]) {
94
+ City target;
95
+ City* cities;
96
+ int ncities;
97
+ int n;
98
+ /*
99
+ const char* path = argv[1];
100
+ strcpy(target.name, argv[2]);
101
+ target.lat = atof(argv[3]);
102
+ target.lng = atof(argv[4]);
103
+ */
104
+ const char* path = "data/coord.dat";
105
+ strcpy(target.name, "八王子市");
106
+ target.lat = 35.66657;
107
+ target.lng = 139.3161;
108
+
109
+ ncities = build_cities(path, &cities);
110
+ //print_cities(cities, ncities);
111
+
112
+ // nearest 3
113
+ n = nearest_city(cities, ncities, 0, &target, manhattan_distance);
114
+ swap_city(&cities[0], &cities[n]);
115
+ n = nearest_city(cities, ncities, 1, &target, manhattan_distance);
116
+ swap_city(&cities[1], &cities[n]);
117
+ n = nearest_city(cities, ncities, 2, &target, manhattan_distance);
118
+ swap_city(&cities[2], &cities[n]);
119
+
120
+ printf("nearest 3 cities from: ");
121
+ print_city(&target);
122
+ puts("---------------");
123
+ print_cities(cities, 3);
124
+
125
+ free(cities);
126
+ return 0;
127
+ }
128
+ ```