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

回答編集履歴

3

2022/01/12 06:27

投稿

kazuma-s
kazuma-s

スコア8222

answer CHANGED
@@ -1,54 +1,49 @@
1
- ```C
2
- #include <stdio.h>
3
-
4
- typedef struct { int n, d; } frac;
5
- typedef frac (*Op)(frac, frac);
6
-
7
- frac add(frac a, frac b) { return (frac){ a.n*b.d + b.n*a.d, a.d*b.d }; }
8
- frac sub(frac a, frac b) { return (frac){ a.n*b.d - b.n*a.d, a.d*b.d }; }
9
- frac mul(frac a, frac b) { return (frac){ a.n*b.n, a.d*b.d }; }
10
- frac div(frac a, frac b) { return (frac){ a.n*b.d, a.d*b.n }; }
11
-
12
- Op o[] = { add, sub, mul, div };
13
- char* op_str[] = { "+", "-", "×", "÷" };
14
-
15
- int is10(frac x) { return x.n == x.d * 10; }
16
-
17
- int main()
18
- {
19
- char str[256];
20
- printf("4桁の数字を入力してください。\n");
21
- scanf_s("%s", str,256);
22
- printf("入力値は「%s」です。\n", str);
23
-
24
- frac a = { str[0] - '0', 1 };
25
- frac b = { str[1] - '0', 1 };
26
- frac c = { str[2] - '0', 1 };
27
- frac d = { str[3] - '0', 1 };
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
- // (a+b)+(c+d)
33
- if (is10(o[j](o[i](a, b), o[k](c, d))))
34
- printf("(%c %s %c) %s (%c %s %c)\n", str[0], op_str[i],
35
- str[1], op_str[j], str[2], op_str[k], str[3]);
36
- // {(a+b)+c}+d
37
- if (is10(o[k](o[j](o[i](a, b), c),d)))
38
- printf("{(%c %s %c) %s %c} %s %c\n", str[0], op_str[i],
39
- str[1], op_str[j], str[2], op_str[k], str[3]);
40
- // a+{b+(c+d)}
41
- if (is10(o[i](a, o[j](b, o[k](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
- // {a+(b+c)}+d
45
- if (is10(o[k](o[i](a, o[j](b, c)), d)))
46
- printf("{%c %s (%c %s %c)} %s %c\n", str[0], op_str[i],
47
- str[1], op_str[j], str[2], op_str[k], str[3]);
48
- // a+{(b+c)+d}
49
- if (is10(o[i](a, o[k](o[j](b, c), d))))
50
- printf("%c %s {(%c %s %c) %s %c}\n", str[0], op_str[i],
51
- str[1], op_str[j], str[2], op_str[k], str[3]);
52
- }
53
- }
54
- ```
1
+ ```c
2
+ #include <stdio.h>
3
+
4
+ typedef struct { int n, d; } frac;
5
+ typedef frac (*Op)(frac, frac);
6
+
7
+ frac add(frac a, frac b) { return (frac){ a.n*b.d + b.n*a.d, a.d*b.d }; }
8
+ frac sub(frac a, frac b) { return (frac){ a.n*b.d - b.n*a.d, a.d*b.d }; }
9
+ frac mul(frac a, frac b) { return (frac){ a.n*b.n, a.d*b.d }; }
10
+ frac div(frac a, frac b) { return (frac){ a.n*b.d, a.d*b.n }; }
11
+
12
+ Op o[] = { add, sub, mul, div };
13
+ char* op_str[] = { "+", "-", "×", "÷" };
14
+
15
+ int is10(frac x) { return x.n == x.d * 10; }
16
+
17
+ int main()
18
+ {
19
+ char str[256];
20
+ printf("4桁の数字を入力してください。\n");
21
+ scanf("%255s", str); // scanf_s("%s", str, 256);
22
+ printf("入力値は「%s」です。\n", str);
23
+
24
+ frac a = { str[0] - '0', 1 };
25
+ frac b = { str[1] - '0', 1 };
26
+ frac c = { str[2] - '0', 1 };
27
+ frac d = { str[3] - '0', 1 };
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
+ ```

2

コードの修正

2022/01/10 08:43

投稿

kazuma-s
kazuma-s

スコア8222

answer CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
  for (int i = 0; i < 4; i++)
30
30
  for (int j = 0; j < 4; j++)
31
- for (int k = 0; k < 4; k++)
31
+ for (int k = 0; k < 4; k++) {
32
32
  // (a+b)+(c+d)
33
33
  if (is10(o[j](o[i](a, b), o[k](c, d))))
34
34
  printf("(%c %s %c) %s (%c %s %c)\n", str[0], op_str[i],
@@ -49,5 +49,6 @@
49
49
  if (is10(o[i](a, o[k](o[j](b, c), d))))
50
50
  printf("%c %s {(%c %s %c) %s %c}\n", str[0], op_str[i],
51
51
  str[1], op_str[j], str[2], op_str[k], str[3]);
52
+ }
52
53
  }
53
54
  ```

1

コードの修正

2022/01/10 08:43

投稿

kazuma-s
kazuma-s

スコア8222

answer CHANGED
@@ -44,7 +44,7 @@
44
44
  // {a+(b+c)}+d
45
45
  if (is10(o[k](o[i](a, o[j](b, c)), d)))
46
46
  printf("{%c %s (%c %s %c)} %s %c\n", str[0], op_str[i],
47
- str[1], op_str[j], str[1], op_str[k], str[3]);
47
+ str[1], op_str[j], str[2], op_str[k], str[3]);
48
48
  // a+{(b+c)+d}
49
49
  if (is10(o[i](a, o[k](o[j](b, c), d))))
50
50
  printf("%c %s {(%c %s %c) %s %c}\n", str[0], op_str[i],