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

質問編集履歴

3

ミス

2016/09/27 17:45

投稿

mentos109
mentos109

スコア28

title CHANGED
File without changes
body CHANGED
@@ -53,7 +53,6 @@
53
53
  num[i] = num[i] % 10;
54
54
  }
55
55
  }
56
- cout << " " << num1_digit << " ";
57
56
  }
58
57
 
59
58
  void Factorial(int number, int* num) {

2

記述ミス

2016/09/27 17:45

投稿

mentos109
mentos109

スコア28

title CHANGED
File without changes
body CHANGED
@@ -3,15 +3,15 @@
3
3
  どこがおかしいのでしょうか?
4
4
  内容としては100の階乗を求めるプログラムです。
5
5
  91の段階で結果がおかしくなります。
6
+ →検算したところ、9を掛けた時点でおかしくなるようです。
6
7
 
7
8
  ###該当のソースコード
8
- ```C++
9
- #include <iostream>
9
+ ```C++#include <iostream>
10
10
  #include <iomanip>
11
11
  #include <string>
12
12
  using namespace std;
13
13
 
14
- #define DIGIT_BUF 165
14
+ #define DIGIT_BUF 1000
15
15
 
16
16
  void ToArray(int number, int* num)
17
17
  {
@@ -24,42 +24,46 @@
24
24
  }
25
25
  }
26
26
 
27
+ int Digit(int* num)
28
+ {
29
+ int count = DIGIT_BUF - 1;
30
+ while (1) {
31
+ if (num[count] != 0)
32
+ return count;
33
+ count--;
34
+ }
35
+ }
36
+
37
+
27
38
  void ArrayMul(int* num1, int* num2, int* num)
28
39
  {
40
+ int num1_digit = Digit(num1);
41
+ int num2_digit = Digit(num2);
29
42
  for (int i = 0; i < DIGIT_BUF; i++) {
30
43
  num[i] = 0;
31
44
  }
32
- for (int i = 0; i < DIGIT_BUF; i++) {
45
+ for (int i = 0; i < num1_digit + 1; i++) {
33
- for (int j = 0; j < DIGIT_BUF; j++) {
46
+ for (int j = 0; j < num2_digit + 1; j++) {
34
47
  num[j + i] += num1[i] * num2[j];
35
48
  }
36
49
  }
37
- for (int i = 0; i < DIGIT_BUF; i++) {
50
+ for (int i = 0; i < Digit(num) + 1; i++) {
38
51
  if (num[i] / 10) {
39
52
  num[i + 1] += num[i] / 10;
40
53
  num[i] = num[i] % 10;
41
54
  }
42
55
  }
56
+ cout << " " << num1_digit << " ";
43
57
  }
44
58
 
45
- int Digit(int* num)
46
- {
47
- int count = DIGIT_BUF-1;
48
- while (1) {
49
- if (num[count] != 0)
50
- return count;
51
- count--;
52
- }
53
- }
54
-
55
59
  void Factorial(int number, int* num) {
56
60
  int hoge1[DIGIT_BUF] = {}, hoge2[DIGIT_BUF] = {}, hoge3[DIGIT_BUF] = {};
57
61
  ToArray(number, hoge3);
58
- for (number; number; number--) {
62
+ for (number; number > 1; number--) {
59
63
  memcpy(hoge1, hoge3, sizeof(int) * DIGIT_BUF);
60
64
  ToArray(number - 1, hoge2);
61
- ArrayMul(hoge1, hoge2, hoge3);
65
+ ArrayMul(hoge1, hoge2, hoge3);
62
- int count = digit(hoge3);
66
+ int count = Digit(hoge3);
63
67
  cout << number - 1 << " ";
64
68
  while (count >= 0) {
65
69
  cout << hoge3[count];
@@ -72,9 +76,8 @@
72
76
  int main()
73
77
  {
74
78
  int result[DIGIT_BUF] = {};
75
- int hoge;
76
79
 
77
- factorial(100, result);
80
+ Factorial(100, result);
78
81
 
79
82
  }
80
83
  ```

1

誤字

2016/09/27 17:45

投稿

mentos109
mentos109

スコア28

title CHANGED
File without changes
body CHANGED
@@ -42,7 +42,7 @@
42
42
  }
43
43
  }
44
44
 
45
- int digit(int* num)
45
+ int Digit(int* num)
46
46
  {
47
47
  int count = DIGIT_BUF-1;
48
48
  while (1) {
@@ -52,7 +52,7 @@
52
52
  }
53
53
  }
54
54
 
55
- void factorial(int number, int* num) {
55
+ void Factorial(int number, int* num) {
56
56
  int hoge1[DIGIT_BUF] = {}, hoge2[DIGIT_BUF] = {}, hoge3[DIGIT_BUF] = {};
57
57
  ToArray(number, hoge3);
58
58
  for (number; number; number--) {