回答編集履歴
2
0を含む場合に出力しない制御の追加
test
CHANGED
@@ -6,6 +6,8 @@
|
|
6
6
|
|
7
7
|
※なので、不適当なプログラムを記載している部分があるかもしれません。
|
8
8
|
|
9
|
+
※コメントを受けて修正しました。
|
10
|
+
|
9
11
|
|
10
12
|
|
11
13
|
```C++
|
@@ -16,6 +18,10 @@
|
|
16
18
|
|
17
19
|
#include <string>
|
18
20
|
|
21
|
+
#include <sstream>
|
22
|
+
|
23
|
+
#include <iomanip>
|
24
|
+
|
19
25
|
using namespace std;
|
20
26
|
|
21
27
|
|
@@ -114,7 +120,11 @@
|
|
114
120
|
|
115
121
|
// 出力対象の数字を文字列に変換
|
116
122
|
|
123
|
+
std::ostringstream sout;
|
124
|
+
|
125
|
+
sout << std::setfill('0') << std::setw(5) << i;
|
126
|
+
|
117
|
-
string str = st
|
127
|
+
std::string str = sout.str();
|
118
128
|
|
119
129
|
|
120
130
|
|
@@ -122,7 +132,7 @@
|
|
122
132
|
|
123
133
|
for (int j = 0; j < (int) str.size(); j++) {
|
124
134
|
|
125
|
-
if (ctoi(str[j]) > max) {
|
135
|
+
if (ctoi(str[j]) > max || ctoi(str[j]) == 0) {
|
126
136
|
|
127
137
|
printFlag = false;
|
128
138
|
|
@@ -140,13 +150,13 @@
|
|
140
150
|
|
141
151
|
cout << "[";
|
142
152
|
|
143
|
-
int length =
|
153
|
+
int length = str.length();
|
144
154
|
|
145
155
|
for (int j = 0; j < length; j++) {
|
146
156
|
|
147
157
|
cout << str[j];
|
148
158
|
|
149
|
-
if (length !=(
|
159
|
+
if (length != (j + 1)) {
|
150
160
|
|
151
161
|
cout << ", ";
|
152
162
|
|
1
回答に対するコメントを受けて、再度プログラムを書き直しました。
test
CHANGED
@@ -1,3 +1,205 @@
|
|
1
|
+
ご質問の内容を正確に把握せずに回答してしまいました。大変申し訳ございません。
|
2
|
+
|
3
|
+
再帰処理を使用しないとすると以下のような処理が妥当なのではないかと思いました。
|
4
|
+
|
5
|
+
※但し私C++は書いたことがないので、javaをイメージして記載してしまいました。
|
6
|
+
|
7
|
+
※なので、不適当なプログラムを記載している部分があるかもしれません。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
```C++
|
12
|
+
|
13
|
+
#include <iostream>
|
14
|
+
|
15
|
+
#include <stdexcept>
|
16
|
+
|
17
|
+
#include <string>
|
18
|
+
|
19
|
+
using namespace std;
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
// char文字を int型 に変換(数字以外が来た場合は -1 を返却)
|
24
|
+
|
25
|
+
int ctoi(char c) {
|
26
|
+
|
27
|
+
switch (c) {
|
28
|
+
|
29
|
+
case '0':
|
30
|
+
|
31
|
+
return 0;
|
32
|
+
|
33
|
+
case '1':
|
34
|
+
|
35
|
+
return 1;
|
36
|
+
|
37
|
+
case '2':
|
38
|
+
|
39
|
+
return 2;
|
40
|
+
|
41
|
+
case '3':
|
42
|
+
|
43
|
+
return 3;
|
44
|
+
|
45
|
+
case '4':
|
46
|
+
|
47
|
+
return 4;
|
48
|
+
|
49
|
+
case '5':
|
50
|
+
|
51
|
+
return 5;
|
52
|
+
|
53
|
+
case '6':
|
54
|
+
|
55
|
+
return 6;
|
56
|
+
|
57
|
+
case '7':
|
58
|
+
|
59
|
+
return 7;
|
60
|
+
|
61
|
+
case '8':
|
62
|
+
|
63
|
+
return 8;
|
64
|
+
|
65
|
+
case '9':
|
66
|
+
|
67
|
+
return 9;
|
68
|
+
|
69
|
+
default:
|
70
|
+
|
71
|
+
return -1;
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
// len 文字数の数字を カンマ区切りで出力
|
80
|
+
|
81
|
+
// 但し max 以上の数字が入っている場合は出力を実施しない
|
82
|
+
|
83
|
+
// 出力例: [2,2,2,2,2]
|
84
|
+
|
85
|
+
void printNums(int len, int max) {
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
// 出力最大値を取得
|
90
|
+
|
91
|
+
string maxValue = "";
|
92
|
+
|
93
|
+
for (int i = 0; i < len; i++) {
|
94
|
+
|
95
|
+
maxValue = maxValue + std::to_string(max);
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
int maxvalue = std::stoi(maxValue);
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// 0 から 出力最大値まで走査
|
104
|
+
|
105
|
+
for (int i = 1; i <= maxvalue; i++) {
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
// 出力対象フラグ初期化
|
110
|
+
|
111
|
+
bool printFlag = true;
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
// 出力対象の数字を文字列に変換
|
116
|
+
|
117
|
+
string str = std::to_string(i);
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// 出力対象に max 以上の数値が含まれている場合は出力処理を実施しない
|
122
|
+
|
123
|
+
for (int j = 0; j < (int) str.size(); j++) {
|
124
|
+
|
125
|
+
if (ctoi(str[j]) > max) {
|
126
|
+
|
127
|
+
printFlag = false;
|
128
|
+
|
129
|
+
break;
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
// 出力対象の場合は出力を実施
|
138
|
+
|
139
|
+
if (printFlag) {
|
140
|
+
|
141
|
+
cout << "[";
|
142
|
+
|
143
|
+
int length = str.length();
|
144
|
+
|
145
|
+
for (int j = 0; j < length; j++) {
|
146
|
+
|
147
|
+
cout << str[j];
|
148
|
+
|
149
|
+
if (length !=( j + 1)) {
|
150
|
+
|
151
|
+
cout << ", ";
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
cout << "]" << endl;
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
// メイン処理
|
172
|
+
|
173
|
+
int main() {
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
// 5桁で全ての数字が2以下のものを出力
|
178
|
+
|
179
|
+
printNums(5, 2);
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
// 結果返却
|
184
|
+
|
185
|
+
return 0;
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
```
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
※以下当初の回答
|
198
|
+
|
199
|
+
/*******************************************************************/
|
200
|
+
|
201
|
+
|
202
|
+
|
1
203
|
単純に for文 を4重に回すのではダメなのですか?
|
2
204
|
|
3
205
|
|