回答編集履歴

1

追記

2020/07/22 06:08

投稿

episteme
episteme

スコア16612

test CHANGED
@@ -29,3 +29,227 @@
29
29
 
30
30
 
31
31
  これだけやれば、距離が近い市3位までを cities[0],cities[1],cities[2] に求めたことになります。
32
+
33
+
34
+
35
+ [追記] 15分で書いたやつ:
36
+
37
+ ```C
38
+
39
+ #define _CRT_SECURE_NO_WARNINGS /* disable warning only for VC++ */
40
+
41
+ #include <stdio.h>
42
+
43
+ #include <stdio.h>
44
+
45
+ #include <stdlib.h>
46
+
47
+ #include <math.h>
48
+
49
+ #include <string.h>
50
+
51
+
52
+
53
+ typedef struct {
54
+
55
+ char name[32];
56
+
57
+ double lat;
58
+
59
+ double lng;
60
+
61
+ } City;
62
+
63
+
64
+
65
+ // ファイル:path を読み、City列を *pcities に求める
66
+
67
+ // 都市の数を返す
68
+
69
+ int build_cities(const char* path, City** pcities) {
70
+
71
+ int n;
72
+
73
+ City* c;
74
+
75
+ FILE* fp = fopen(path,"r");
76
+
77
+ if ( fp == NULL ) return 0;
78
+
79
+ fscanf(fp, "%d", &n);
80
+
81
+ c = (City*)malloc(sizeof(City)*n);
82
+
83
+ if ( c != NULL ) {
84
+
85
+ int i;
86
+
87
+ *pcities = c;
88
+
89
+ for ( i = 0; i < n; ++i ) {
90
+
91
+ fscanf(fp, "%s %lf %lf", c[i].name, &c[i].lat, &c[i].lng);
92
+
93
+ }
94
+
95
+ }
96
+
97
+ fclose(fp);
98
+
99
+ return n;
100
+
101
+ }
102
+
103
+
104
+
105
+ void print_city(const City* city) {
106
+
107
+ printf("%s %lf %lf\n", city->name, city->lat, city->lng);
108
+
109
+ }
110
+
111
+
112
+
113
+ void print_cities(const City* cities, int ncities) {
114
+
115
+ int i;
116
+
117
+ for (i = 0; i < ncities; ++i) {
118
+
119
+ print_city(&cities[i]);
120
+
121
+ }
122
+
123
+ }
124
+
125
+
126
+
127
+ // cities[begin]~cities[ncities-1] の中からtargetに最も近い cities[n] を求め n を返す。
128
+
129
+ // 2都市間の距離は fdistance により算出する
130
+
131
+ int nearest_city(const City* cities, int ncities, int begin,
132
+
133
+ const City* target, double (*fdistance)(const City*, const City*) ) {
134
+
135
+ int i;
136
+
137
+ int nearest = 0;
138
+
139
+ double nearest_distance = 0.0;
140
+
141
+ for ( i = begin; i < ncities; ++i ) {
142
+
143
+ double distance = fdistance(&cities[i], target);
144
+
145
+ if ( i == begin || nearest_distance > distance ) {
146
+
147
+ nearest = i;
148
+
149
+ nearest_distance = distance;
150
+
151
+ }
152
+
153
+ }
154
+
155
+ return nearest;
156
+
157
+ }
158
+
159
+
160
+
161
+ // 都市 a と b を交換する
162
+
163
+ void swap_city(City* a, City* b) {
164
+
165
+ City tmp = *a;
166
+
167
+ *a = *b;
168
+
169
+ *b = tmp;
170
+
171
+ }
172
+
173
+
174
+
175
+ // (なんちゃって)マンハッタン距離
176
+
177
+ double manhattan_distance(const City* a, const City* b) {
178
+
179
+ return fabs(a->lng - b->lng) + fabs(a->lng - b->lng);
180
+
181
+ }
182
+
183
+
184
+
185
+ int main(int argc, char* argv[]) {
186
+
187
+ City target;
188
+
189
+ City* cities;
190
+
191
+ int ncities;
192
+
193
+ int n;
194
+
195
+ /*
196
+
197
+ const char* path = argv[1];
198
+
199
+ strcpy(target.name, argv[2]);
200
+
201
+ target.lat = atof(argv[3]);
202
+
203
+ target.lng = atof(argv[4]);
204
+
205
+ */
206
+
207
+ const char* path = "data/coord.dat";
208
+
209
+ strcpy(target.name, "八王子市");
210
+
211
+ target.lat = 35.66657;
212
+
213
+ target.lng = 139.3161;
214
+
215
+
216
+
217
+ ncities = build_cities(path, &cities);
218
+
219
+ //print_cities(cities, ncities);
220
+
221
+
222
+
223
+ // nearest 3
224
+
225
+ n = nearest_city(cities, ncities, 0, &target, manhattan_distance);
226
+
227
+ swap_city(&cities[0], &cities[n]);
228
+
229
+ n = nearest_city(cities, ncities, 1, &target, manhattan_distance);
230
+
231
+ swap_city(&cities[1], &cities[n]);
232
+
233
+ n = nearest_city(cities, ncities, 2, &target, manhattan_distance);
234
+
235
+ swap_city(&cities[2], &cities[n]);
236
+
237
+
238
+
239
+ printf("nearest 3 cities from: ");
240
+
241
+ print_city(&target);
242
+
243
+ puts("---------------");
244
+
245
+ print_cities(cities, 3);
246
+
247
+
248
+
249
+ free(cities);
250
+
251
+ return 0;
252
+
253
+ }
254
+
255
+ ```