回答編集履歴
4
バブルソートでは無い
test
CHANGED
@@ -96,9 +96,9 @@
|
|
96
96
|
|
97
97
|
|
98
98
|
|
99
|
-
//
|
99
|
+
//ソート
|
100
|
-
|
100
|
+
|
101
|
-
void
|
101
|
+
void sortDiagonal(Diagonal *d){
|
102
102
|
|
103
103
|
int size=getDiagonalLength(d);
|
104
104
|
|
@@ -230,7 +230,7 @@
|
|
230
230
|
|
231
231
|
d.or=0;
|
232
232
|
|
233
|
-
|
233
|
+
sortDiagonal(&d);
|
234
234
|
|
235
235
|
|
236
236
|
|
@@ -240,7 +240,7 @@
|
|
240
240
|
|
241
241
|
d.or=0;
|
242
242
|
|
243
|
-
|
243
|
+
sortDiagonal(&d);
|
244
244
|
|
245
245
|
|
246
246
|
|
@@ -248,7 +248,7 @@
|
|
248
248
|
|
249
249
|
d.or=i;
|
250
250
|
|
251
|
-
|
251
|
+
sortDiagonal(&d);
|
252
252
|
|
253
253
|
}
|
254
254
|
|
3
位置ずらし
test
CHANGED
@@ -28,9 +28,9 @@
|
|
28
28
|
|
29
29
|
Matrix *allocMatrix(int n) {
|
30
30
|
|
31
|
-
Matrix *m
|
31
|
+
Matrix *m=malloc(sizeof(Matrix)+sizeof(int)*n*n);
|
32
|
-
|
32
|
+
|
33
|
-
m->n
|
33
|
+
m->n=n;
|
34
34
|
|
35
35
|
return m;
|
36
36
|
|
@@ -44,13 +44,13 @@
|
|
44
44
|
|
45
45
|
int getMatrix(Matrix *m, int c, int r) {
|
46
46
|
|
47
|
-
return m->a[m->n
|
47
|
+
return m->a[m->n*r+c];
|
48
48
|
|
49
49
|
}
|
50
50
|
|
51
51
|
void setMatrix(Matrix *m, int c, int r, int v) {
|
52
52
|
|
53
|
-
m->a[m->n
|
53
|
+
m->a[m->n*r+c]=v;
|
54
54
|
|
55
55
|
}
|
56
56
|
|
@@ -60,7 +60,7 @@
|
|
60
60
|
|
61
61
|
Matrix *m;
|
62
62
|
|
63
|
-
int oc,
|
63
|
+
int oc,or; //開始位置オフセット(どちらかは0)
|
64
64
|
|
65
65
|
} Diagonal;
|
66
66
|
|
@@ -68,29 +68,29 @@
|
|
68
68
|
|
69
69
|
int getDiagonal(Diagonal *d, int i) {
|
70
70
|
|
71
|
-
return getMatrix(d->m,
|
71
|
+
return getMatrix(d->m,d->oc+i,d->or+i);
|
72
72
|
|
73
73
|
}
|
74
74
|
|
75
75
|
void setDiagonal(Diagonal *d, int i, int v) {
|
76
76
|
|
77
|
-
setMatrix(d->m,
|
77
|
+
setMatrix(d->m,d->oc+i,d->or+i,v);
|
78
78
|
|
79
79
|
}
|
80
80
|
|
81
81
|
int getDiagonalLength(Diagonal *d) {
|
82
82
|
|
83
|
-
return d->m->n
|
83
|
+
return d->m->n-(d->oc+d->or);
|
84
84
|
|
85
85
|
}
|
86
86
|
|
87
87
|
void swapDiagonal(Diagonal *d, int i, int j) {
|
88
88
|
|
89
|
-
int t
|
89
|
+
int t=getDiagonal(d,i);
|
90
|
-
|
90
|
+
|
91
|
-
setDiagonal(d,
|
91
|
+
setDiagonal(d,i,getDiagonal(d,j));
|
92
|
-
|
92
|
+
|
93
|
-
setDiagonal(d,
|
93
|
+
setDiagonal(d,j,t);
|
94
94
|
|
95
95
|
}
|
96
96
|
|
@@ -168,17 +168,17 @@
|
|
168
168
|
|
169
169
|
|
170
170
|
|
171
|
-
Matrix *m
|
171
|
+
Matrix *m=allocMatrix(n);
|
172
172
|
|
173
173
|
|
174
174
|
|
175
175
|
printf("%d×%d行列A(i,j)の要素を入力してください。\n",n,n); fflush(stdout);
|
176
176
|
|
177
|
-
for(int i=0;
|
177
|
+
for(int i=0;i<n*n;i++){
|
178
178
|
|
179
179
|
printf("A(%2d, %2d) = ",i/n+1,i%n+1); fflush(stdout);
|
180
180
|
|
181
|
-
scanf("%d",
|
181
|
+
scanf("%d",&m->a[i]);
|
182
182
|
|
183
183
|
}
|
184
184
|
|
@@ -190,15 +190,15 @@
|
|
190
190
|
|
191
191
|
Matrix *testMatrix() {
|
192
192
|
|
193
|
-
int n
|
193
|
+
int n=3;
|
194
|
-
|
194
|
+
|
195
|
-
Matrix *m
|
195
|
+
Matrix *m=allocMatrix(n);
|
196
196
|
|
197
197
|
|
198
198
|
|
199
199
|
int sample[]={1,5,3,4,6,2,4,8,2};
|
200
200
|
|
201
|
-
for(int i=0;i<n*n;i++)
|
201
|
+
for(int i=0;i<n*n;i++)m->a[i]=sample[i];
|
202
202
|
|
203
203
|
|
204
204
|
|
@@ -210,41 +210,43 @@
|
|
210
210
|
|
211
211
|
int main (void){
|
212
212
|
|
213
|
-
Matrix *m
|
213
|
+
Matrix *m=inputMatrix();
|
214
|
-
|
214
|
+
|
215
|
-
//Matrix *m
|
215
|
+
//Matrix *m=testMatrix();
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
printMatrix(m,"before sort:\n");//ソート前の行列を出力
|
216
220
|
|
217
221
|
|
218
222
|
|
219
223
|
Diagonal d;
|
220
224
|
|
221
|
-
d.m
|
225
|
+
d.m=m;
|
222
|
-
|
226
|
+
|
227
|
+
|
228
|
+
|
223
|
-
d.oc
|
229
|
+
d.oc=0;
|
224
|
-
|
230
|
+
|
225
|
-
d.or
|
231
|
+
d.or=0;
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
printMatrix(m, "before sort:\n");//ソート前の行列を出力
|
230
|
-
|
231
|
-
|
232
232
|
|
233
233
|
Bsort(&d);
|
234
234
|
|
235
|
+
|
236
|
+
|
235
|
-
for(int i=1;
|
237
|
+
for(int i=1;i<m->n-1;i++){
|
236
|
-
|
238
|
+
|
237
|
-
d.oc
|
239
|
+
d.oc=i;
|
238
|
-
|
240
|
+
|
239
|
-
d.or
|
241
|
+
d.or=0;
|
240
242
|
|
241
243
|
Bsort(&d);
|
242
244
|
|
243
245
|
|
244
246
|
|
245
|
-
d.oc
|
247
|
+
d.oc=0;
|
246
|
-
|
248
|
+
|
247
|
-
d.or
|
249
|
+
d.or=i;
|
248
250
|
|
249
251
|
Bsort(&d);
|
250
252
|
|
@@ -252,7 +254,7 @@
|
|
252
254
|
|
253
255
|
|
254
256
|
|
255
|
-
printMatrix(m,
|
257
|
+
printMatrix(m,"ans\n");//ソート後の行列を出力
|
256
258
|
|
257
259
|
|
258
260
|
|
2
コード改良
test
CHANGED
@@ -78,15 +78,9 @@
|
|
78
78
|
|
79
79
|
}
|
80
80
|
|
81
|
-
int max(int a, int b) {
|
82
|
-
|
83
|
-
return a > b ? a : b;
|
84
|
-
|
85
|
-
}
|
86
|
-
|
87
81
|
int getDiagonalLength(Diagonal *d) {
|
88
82
|
|
89
|
-
return d->m->n -
|
83
|
+
return d->m->n - (d->oc + d->or);
|
90
84
|
|
91
85
|
}
|
92
86
|
|
1
コード追加
test
CHANGED
@@ -1 +1,319 @@
|
|
1
1
|
折角(?)ソートも書かれているので、行列内を斜めにアクセスできるアクセサを作れば、メインのループは一本で済むのではないでしょうか。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
```c
|
6
|
+
|
7
|
+
#include <stdio.h>
|
8
|
+
|
9
|
+
#include <stdlib.h>
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
#define MIN 0
|
14
|
+
|
15
|
+
#define MAX 100
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
typedef struct {
|
20
|
+
|
21
|
+
int n;
|
22
|
+
|
23
|
+
int a[0];
|
24
|
+
|
25
|
+
} Matrix;
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
Matrix *allocMatrix(int n) {
|
30
|
+
|
31
|
+
Matrix *m = malloc(sizeof(Matrix)+sizeof(int)*n*n);
|
32
|
+
|
33
|
+
m->n = n;
|
34
|
+
|
35
|
+
return m;
|
36
|
+
|
37
|
+
}
|
38
|
+
|
39
|
+
void freeMatrix(Matrix *m) {
|
40
|
+
|
41
|
+
free(m);
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
int getMatrix(Matrix *m, int c, int r) {
|
46
|
+
|
47
|
+
return m->a[m->n * r + c];
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
void setMatrix(Matrix *m, int c, int r, int v) {
|
52
|
+
|
53
|
+
m->a[m->n * r + c] = v;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
typedef struct {
|
60
|
+
|
61
|
+
Matrix *m;
|
62
|
+
|
63
|
+
int oc, or; //開始位置オフセット(どちらかは0)
|
64
|
+
|
65
|
+
} Diagonal;
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
int getDiagonal(Diagonal *d, int i) {
|
70
|
+
|
71
|
+
return getMatrix(d->m, d->oc+i, d->or+i);
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
void setDiagonal(Diagonal *d, int i, int v) {
|
76
|
+
|
77
|
+
setMatrix(d->m, d->oc+i, d->or+i, v);
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
int max(int a, int b) {
|
82
|
+
|
83
|
+
return a > b ? a : b;
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
int getDiagonalLength(Diagonal *d) {
|
88
|
+
|
89
|
+
return d->m->n - max(d->oc, d->or);
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
void swapDiagonal(Diagonal *d, int i, int j) {
|
94
|
+
|
95
|
+
int t = getDiagonal(d, i);
|
96
|
+
|
97
|
+
setDiagonal(d, i, getDiagonal(d, j));
|
98
|
+
|
99
|
+
setDiagonal(d, j, t);
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
//バブルソート
|
106
|
+
|
107
|
+
void Bsort(Diagonal *d){
|
108
|
+
|
109
|
+
int size=getDiagonalLength(d);
|
110
|
+
|
111
|
+
for(int i=0;i<size;i++){
|
112
|
+
|
113
|
+
for(int j=i+1;j<size;j++){
|
114
|
+
|
115
|
+
if(getDiagonal(d,i)>getDiagonal(d,j)){
|
116
|
+
|
117
|
+
swapDiagonal(d,i,j);
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
//配列を出力する関数
|
130
|
+
|
131
|
+
void printMatrix(Matrix *m, char *header){
|
132
|
+
|
133
|
+
if(header!=NULL)printf(header);
|
134
|
+
|
135
|
+
for(int r=0;r<m->n;r++){
|
136
|
+
|
137
|
+
for(int c=0;c<m->n;c++)printf("%4d",getMatrix(m,c,r));
|
138
|
+
|
139
|
+
printf("\n");
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
//値をインプットする関数
|
148
|
+
|
149
|
+
int input(int min, int max){
|
150
|
+
|
151
|
+
int result;
|
152
|
+
|
153
|
+
while(1){
|
154
|
+
|
155
|
+
scanf("%d",&result);
|
156
|
+
|
157
|
+
if(min<=result&&result<=max) break;
|
158
|
+
|
159
|
+
printf("入力された値は無効です\n");
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
return result;
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
Matrix *inputMatrix() {
|
170
|
+
|
171
|
+
printf("正方行列の列の数を入力してください。\n"); fflush(stdout);
|
172
|
+
|
173
|
+
int n=input(MIN,MAX);
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
Matrix *m = allocMatrix(n);
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
printf("%d×%d行列A(i,j)の要素を入力してください。\n",n,n); fflush(stdout);
|
182
|
+
|
183
|
+
for(int i=0; i<n*n; i++){
|
184
|
+
|
185
|
+
printf("A(%2d, %2d) = ",i/n+1,i%n+1); fflush(stdout);
|
186
|
+
|
187
|
+
scanf("%d", &m->a[i]);
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
return m;
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
Matrix *testMatrix() {
|
198
|
+
|
199
|
+
int n = 3;
|
200
|
+
|
201
|
+
Matrix *m = allocMatrix(n);
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
int sample[]={1,5,3,4,6,2,4,8,2};
|
206
|
+
|
207
|
+
for(int i=0;i<n*n;i++) m->a[i]=sample[i];
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
return m;
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
int main (void){
|
218
|
+
|
219
|
+
Matrix *m = inputMatrix();
|
220
|
+
|
221
|
+
//Matrix *m = testMatrix();
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
Diagonal d;
|
226
|
+
|
227
|
+
d.m = m;
|
228
|
+
|
229
|
+
d.oc = 0;
|
230
|
+
|
231
|
+
d.or = 0;
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
printMatrix(m, "before sort:\n");//ソート前の行列を出力
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
Bsort(&d);
|
240
|
+
|
241
|
+
for(int i=1; i<m->n-1; i++){
|
242
|
+
|
243
|
+
d.oc = i;
|
244
|
+
|
245
|
+
d.or = 0;
|
246
|
+
|
247
|
+
Bsort(&d);
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
d.oc = 0;
|
252
|
+
|
253
|
+
d.or = i;
|
254
|
+
|
255
|
+
Bsort(&d);
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
printMatrix(m, "ans\n");//ソート後の行列を出力
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
freeMatrix(m);
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
return 0;
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
```
|
274
|
+
|
275
|
+
実行結果
|
276
|
+
|
277
|
+
```plain
|
278
|
+
|
279
|
+
正方行列の列の数を入力してください。
|
280
|
+
|
281
|
+
3
|
282
|
+
|
283
|
+
3×3行列A(i,j)の要素を入力してください。
|
284
|
+
|
285
|
+
A( 1, 1) = 9
|
286
|
+
|
287
|
+
A( 1, 2) = 8
|
288
|
+
|
289
|
+
A( 1, 3) = 7
|
290
|
+
|
291
|
+
A( 2, 1) = 6
|
292
|
+
|
293
|
+
A( 2, 2) = 5
|
294
|
+
|
295
|
+
A( 2, 3) = 4
|
296
|
+
|
297
|
+
A( 3, 1) = 3
|
298
|
+
|
299
|
+
A( 3, 2) = 2
|
300
|
+
|
301
|
+
A( 3, 3) = 1
|
302
|
+
|
303
|
+
before sort:
|
304
|
+
|
305
|
+
9 8 7
|
306
|
+
|
307
|
+
6 5 4
|
308
|
+
|
309
|
+
3 2 1
|
310
|
+
|
311
|
+
ans
|
312
|
+
|
313
|
+
1 4 7
|
314
|
+
|
315
|
+
2 5 8
|
316
|
+
|
317
|
+
3 6 9
|
318
|
+
|
319
|
+
```
|