質問編集履歴
1
行番号を削除しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,161 +12,159 @@
|
|
12
12
|
|
13
13
|
### 該当のソースコード
|
14
14
|
|
15
|
-
|
15
|
+
#include <stdio.h>
|
16
16
|
|
17
|
-
02:
|
18
17
|
|
19
|
-
03:#define MAXDATE 10000
|
20
18
|
|
21
|
-
0
|
19
|
+
#define MAXDATE 10000
|
22
20
|
|
23
|
-
05:#define _YES 1
|
24
21
|
|
25
|
-
06:#define _NO 0
|
26
22
|
|
27
|
-
|
23
|
+
#define _YES 1
|
28
24
|
|
29
|
-
|
25
|
+
#define _NO 0
|
30
26
|
|
31
|
-
09:{
|
32
27
|
|
33
|
-
10: int n, i, j, k, x;
|
34
28
|
|
35
|
-
|
29
|
+
int main(void)
|
36
30
|
|
37
|
-
|
31
|
+
{
|
38
32
|
|
39
|
-
|
33
|
+
int n, i, j, k, x;
|
40
34
|
|
41
|
-
|
35
|
+
int v[MAXDATE];
|
42
36
|
|
43
|
-
|
37
|
+
int highid, lowid, midid;
|
44
38
|
|
45
|
-
|
39
|
+
int tmp;
|
46
40
|
|
47
|
-
|
41
|
+
char filename[20];
|
48
42
|
|
49
|
-
|
43
|
+
FILE *fin;
|
50
44
|
|
51
|
-
19: scanf("%s", filename);
|
52
45
|
|
53
|
-
20:
|
54
46
|
|
55
|
-
|
47
|
+
puts("ファイル名を入力してください。");//データの読み込み
|
56
48
|
|
57
|
-
|
49
|
+
printf("file name =");
|
58
50
|
|
59
|
-
|
51
|
+
scanf("%s", filename);
|
60
52
|
|
61
|
-
24: }
|
62
53
|
|
63
|
-
25:
|
64
54
|
|
65
|
-
|
55
|
+
if ((fin = fopen(filename, "r"))==NULL) {
|
66
56
|
|
67
|
-
|
57
|
+
printf("ファイルを開くことが出来ません。\n");
|
68
58
|
|
69
|
-
|
59
|
+
return 1;
|
70
60
|
|
71
|
-
|
61
|
+
}
|
72
62
|
|
73
|
-
30: for (i = 0; i < n; i++) {
|
74
63
|
|
75
|
-
31: fscanf(fin, "%d", &v[i]);
|
76
64
|
|
77
|
-
|
65
|
+
n = 0;
|
78
66
|
|
79
|
-
|
67
|
+
while (fscanf(fin, "%d", &v[n])==1) n++;
|
80
68
|
|
81
|
-
|
69
|
+
fclose(fin);
|
82
70
|
|
83
|
-
35: j = i;
|
84
71
|
|
85
|
-
36: for (k = i + 1; k < n; k++) {
|
86
72
|
|
87
|
-
|
73
|
+
for (i = 0; i < n; i++) {
|
88
74
|
|
89
|
-
|
75
|
+
fscanf(fin, "%d", &v[i]);
|
90
76
|
|
91
|
-
|
77
|
+
}
|
92
78
|
|
93
|
-
|
79
|
+
|
94
80
|
|
95
|
-
|
81
|
+
for (i = 0; i < n - 1; i++) { //選択ソートによるデータの並び替え
|
96
82
|
|
97
|
-
|
83
|
+
j = i;
|
98
84
|
|
99
|
-
|
85
|
+
for (k = i + 1; k < n; k++) {
|
100
86
|
|
101
|
-
|
87
|
+
if (v[j] > v[k]) {
|
102
88
|
|
103
|
-
|
89
|
+
j = k;
|
104
90
|
|
105
|
-
|
91
|
+
}
|
106
92
|
|
107
|
-
|
93
|
+
}
|
108
94
|
|
109
|
-
|
95
|
+
tmp = v[j];
|
110
96
|
|
111
|
-
|
97
|
+
v[j] = v[i];
|
112
98
|
|
113
|
-
|
99
|
+
v[i] = tmp;
|
114
100
|
|
115
|
-
|
101
|
+
}
|
116
102
|
|
117
|
-
52:
|
118
103
|
|
119
|
-
53: while (lowid <= highid) {
|
120
104
|
|
121
|
-
|
105
|
+
while (scanf("%d", &x)==1) { //二分探索による探索
|
122
106
|
|
123
|
-
55: if (v[midid] == x) {
|
124
107
|
|
125
|
-
56: found = _YES;
|
126
108
|
|
127
|
-
|
109
|
+
highid = n - 1;
|
128
110
|
|
129
|
-
|
111
|
+
lowid = 0;
|
130
112
|
|
131
|
-
59: else if (v[midid] < x) {
|
132
113
|
|
133
|
-
60: lowid = midid + 1;
|
134
114
|
|
135
|
-
|
115
|
+
int found = _NO;
|
136
116
|
|
137
|
-
|
117
|
+
while (lowid <= highid) {
|
138
118
|
|
139
|
-
|
119
|
+
midid = (lowid + highid) / 2;
|
140
120
|
|
141
|
-
|
121
|
+
if (v[midid] == x) {
|
142
122
|
|
143
|
-
|
123
|
+
found = _YES;
|
144
124
|
|
145
|
-
|
125
|
+
break;
|
146
126
|
|
147
|
-
|
127
|
+
}
|
148
128
|
|
149
|
-
|
129
|
+
else if (v[midid] < x) {
|
150
130
|
|
151
|
-
|
131
|
+
lowid = midid + 1;
|
152
132
|
|
153
|
-
|
133
|
+
}
|
154
134
|
|
155
|
-
|
135
|
+
else {
|
156
136
|
|
157
|
-
|
137
|
+
highid = midid - 1;
|
158
138
|
|
159
|
-
|
139
|
+
}
|
160
140
|
|
161
|
-
|
141
|
+
}
|
162
142
|
|
163
|
-
75: printf("さようなら。\n");
|
164
143
|
|
165
|
-
76:
|
166
144
|
|
167
|
-
|
145
|
+
if (found == _YES) {
|
168
146
|
|
147
|
+
printf("%dはv[%d]と一致しました。\n", x, midid);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
169
|
-
|
151
|
+
else {
|
152
|
+
|
153
|
+
printf("%dはデータから見つかりませんでした。\n", x);
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
printf("さようなら。\n");
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
return 0;
|
166
|
+
|
167
|
+
}
|
170
168
|
|
171
169
|
|
172
170
|
|