回答編集履歴
3
test
CHANGED
@@ -1,107 +1,50 @@
|
|
1
|
-
```
|
1
|
+
```c
|
2
|
-
|
3
2
|
#include <stdio.h>
|
4
3
|
|
5
|
-
|
6
|
-
|
7
4
|
typedef struct { int n, d; } frac;
|
8
|
-
|
9
5
|
typedef frac (*Op)(frac, frac);
|
10
6
|
|
11
|
-
|
12
|
-
|
13
7
|
frac add(frac a, frac b) { return (frac){ a.n*b.d + b.n*a.d, a.d*b.d }; }
|
14
|
-
|
15
8
|
frac sub(frac a, frac b) { return (frac){ a.n*b.d - b.n*a.d, a.d*b.d }; }
|
16
|
-
|
17
9
|
frac mul(frac a, frac b) { return (frac){ a.n*b.n, a.d*b.d }; }
|
18
|
-
|
19
10
|
frac div(frac a, frac b) { return (frac){ a.n*b.d, a.d*b.n }; }
|
20
11
|
|
21
|
-
|
22
|
-
|
23
12
|
Op o[] = { add, sub, mul, div };
|
24
|
-
|
25
13
|
char* op_str[] = { "+", "-", "×", "÷" };
|
26
|
-
|
27
|
-
|
28
14
|
|
29
15
|
int is10(frac x) { return x.n == x.d * 10; }
|
30
16
|
|
31
|
-
|
32
|
-
|
33
17
|
int main()
|
34
|
-
|
35
18
|
{
|
36
|
-
|
37
19
|
char str[256];
|
38
|
-
|
39
20
|
printf("4桁の数字を入力してください。\n");
|
40
|
-
|
41
|
-
scanf_s("%s", str,256);
|
21
|
+
scanf("%255s", str); // scanf_s("%s", str, 256);
|
42
|
-
|
43
22
|
printf("入力値は「%s」です。\n", str);
|
44
23
|
|
45
|
-
|
46
|
-
|
47
24
|
frac a = { str[0] - '0', 1 };
|
48
|
-
|
49
25
|
frac b = { str[1] - '0', 1 };
|
50
|
-
|
51
26
|
frac c = { str[2] - '0', 1 };
|
52
|
-
|
53
27
|
frac d = { str[3] - '0', 1 };
|
54
28
|
|
29
|
+
for (int i = 0; i < 4; i++)
|
30
|
+
for (int j = 0; j < 4; j++)
|
31
|
+
for (int k = 0; k < 4; k++) {
|
32
|
+
if (is10(o[j](o[i](a, b), o[k](c, d)))) // (a+b)+(c+d)
|
33
|
+
printf("(%c %s %c) %s (%c %s %c)\n", str[0], op_str[i],
|
34
|
+
str[1], op_str[j], str[2], op_str[k], str[3]);
|
35
|
+
if (is10(o[k](o[j](o[i](a, b), c),d))) // {(a+b)+c}+d
|
36
|
+
printf("{(%c %s %c) %s %c} %s %c\n", str[0], op_str[i],
|
37
|
+
str[1], op_str[j], str[2], op_str[k], str[3]);
|
38
|
+
if (is10(o[i](a, o[j](b, o[k](c, d))))) // a+{b+(c+d)}
|
39
|
+
printf("%c %s {%c %s (%c %s %c)}\n", str[0], op_str[i],
|
40
|
+
str[1], op_str[j], str[2], op_str[k], str[3]);
|
41
|
+
if (is10(o[k](o[i](a, o[j](b, c)), d))) // {a+(b+c)}+d
|
42
|
+
printf("{%c %s (%c %s %c)} %s %c\n", str[0], op_str[i],
|
43
|
+
str[1], op_str[j], str[2], op_str[k], str[3]);
|
44
|
+
if (is10(o[i](a, o[k](o[j](b, c), d)))) // a+{(b+c)+d}
|
45
|
+
printf("%c %s {(%c %s %c) %s %c}\n", str[0], op_str[i],
|
46
|
+
str[1], op_str[j], str[2], op_str[k], str[3]);
|
47
|
+
}
|
48
|
+
}
|
49
|
+
```
|
55
50
|
|
56
|
-
|
57
|
-
for (int i = 0; i < 4; i++)
|
58
|
-
|
59
|
-
for (int j = 0; j < 4; j++)
|
60
|
-
|
61
|
-
for (int k = 0; k < 4; k++) {
|
62
|
-
|
63
|
-
// (a+b)+(c+d)
|
64
|
-
|
65
|
-
if (is10(o[j](o[i](a, b), o[k](c, d))))
|
66
|
-
|
67
|
-
printf("(%c %s %c) %s (%c %s %c)\n", str[0], op_str[i],
|
68
|
-
|
69
|
-
str[1], op_str[j], str[2], op_str[k], str[3]);
|
70
|
-
|
71
|
-
// {(a+b)+c}+d
|
72
|
-
|
73
|
-
if (is10(o[k](o[j](o[i](a, b), c),d)))
|
74
|
-
|
75
|
-
printf("{(%c %s %c) %s %c} %s %c\n", str[0], op_str[i],
|
76
|
-
|
77
|
-
str[1], op_str[j], str[2], op_str[k], str[3]);
|
78
|
-
|
79
|
-
// a+{b+(c+d)}
|
80
|
-
|
81
|
-
if (is10(o[i](a, o[j](b, o[k](c, d)))))
|
82
|
-
|
83
|
-
printf("%c %s {%c %s (%c %s %c)}\n", str[0], op_str[i],
|
84
|
-
|
85
|
-
str[1], op_str[j], str[2], op_str[k], str[3]);
|
86
|
-
|
87
|
-
// {a+(b+c)}+d
|
88
|
-
|
89
|
-
if (is10(o[k](o[i](a, o[j](b, c)), d)))
|
90
|
-
|
91
|
-
printf("{%c %s (%c %s %c)} %s %c\n", str[0], op_str[i],
|
92
|
-
|
93
|
-
str[1], op_str[j], str[2], op_str[k], str[3]);
|
94
|
-
|
95
|
-
// a+{(b+c)+d}
|
96
|
-
|
97
|
-
if (is10(o[i](a, o[k](o[j](b, c), d))))
|
98
|
-
|
99
|
-
printf("%c %s {(%c %s %c) %s %c}\n", str[0], op_str[i],
|
100
|
-
|
101
|
-
str[1], op_str[j], str[2], op_str[k], str[3]);
|
102
|
-
|
103
|
-
}
|
104
|
-
|
105
|
-
}
|
106
|
-
|
107
|
-
```
|
2
コードの修正
test
CHANGED
@@ -58,7 +58,7 @@
|
|
58
58
|
|
59
59
|
for (int j = 0; j < 4; j++)
|
60
60
|
|
61
|
-
for (int k = 0; k < 4; k++)
|
61
|
+
for (int k = 0; k < 4; k++) {
|
62
62
|
|
63
63
|
// (a+b)+(c+d)
|
64
64
|
|
@@ -100,6 +100,8 @@
|
|
100
100
|
|
101
101
|
str[1], op_str[j], str[2], op_str[k], str[3]);
|
102
102
|
|
103
|
+
}
|
104
|
+
|
103
105
|
}
|
104
106
|
|
105
107
|
```
|
1
コードの修正
test
CHANGED
@@ -90,7 +90,7 @@
|
|
90
90
|
|
91
91
|
printf("{%c %s (%c %s %c)} %s %c\n", str[0], op_str[i],
|
92
92
|
|
93
|
-
str[1], op_str[j], str[
|
93
|
+
str[1], op_str[j], str[2], op_str[k], str[3]);
|
94
94
|
|
95
95
|
// a+{(b+c)+d}
|
96
96
|
|