回答編集履歴
2
入力文字列の最後が「,」の場合のバグを修正
answer
CHANGED
@@ -52,4 +52,37 @@
|
|
52
52
|
}
|
53
53
|
|
54
54
|
int main() { generate(0); }
|
55
|
+
```
|
56
|
+
**追記2**
|
57
|
+
最初のコードは、`A,B,C,` のように最後が `,` の場合を
|
58
|
+
入力謝りにしていなかったので、次のように訂正します。
|
59
|
+
```C++
|
60
|
+
#include <iostream>
|
61
|
+
#include <sstream> // istringstream
|
62
|
+
using namespace std;
|
63
|
+
|
64
|
+
const int n = 3; // n <= 26. 3 for A,B,C. 5 for A,B,C,D,E.
|
65
|
+
|
66
|
+
bool check(const char *s)
|
67
|
+
{
|
68
|
+
istringstream iss(s);
|
69
|
+
int cnt[26] = { 0 };
|
70
|
+
char c;
|
71
|
+
while (iss >> c && c >= 'A' && c < 'A'+n && !cnt[c-'A']++) {
|
72
|
+
if (!(iss >> c)) return true;
|
73
|
+
if (c != ',') break;
|
74
|
+
}
|
75
|
+
return false;
|
76
|
+
}
|
77
|
+
|
78
|
+
int main(int argc, char* argv[])
|
79
|
+
{
|
80
|
+
bool found = false;
|
81
|
+
if (argc == 2)
|
82
|
+
found = check(argv[1]);
|
83
|
+
if (found)
|
84
|
+
cout << "コマンドラインの引き数は : " << argv[1] << endl;
|
85
|
+
else
|
86
|
+
cout << "error: 入力誤りです"<< endl;
|
87
|
+
}
|
55
88
|
```
|
1
全パターン生成のコードを追加
answer
CHANGED
@@ -27,4 +27,29 @@
|
|
27
27
|
else
|
28
28
|
cout << "error: 入力誤りです"<< endl;
|
29
29
|
}
|
30
|
+
```
|
31
|
+
**追記**
|
32
|
+
全パターンを生成したいのなら、
|
33
|
+
```C++
|
34
|
+
#include <iostream>
|
35
|
+
using namespace std;
|
36
|
+
|
37
|
+
const int n = 3; // n <= 26. 3 for A,B,C. 5 for A,B,C,D,E.
|
38
|
+
char a[n], used[n];
|
39
|
+
|
40
|
+
void show(int k)
|
41
|
+
{
|
42
|
+
for (int i = 0; i < k; i++) cout << a[i] << ',';
|
43
|
+
cout << a[k] << '\n';
|
44
|
+
}
|
45
|
+
|
46
|
+
void generate(int i)
|
47
|
+
{
|
48
|
+
if (i == n) return;
|
49
|
+
for (int j = 0; j < n; j++)
|
50
|
+
if (!used[j])
|
51
|
+
used[j] = a[i] = 'A'+j, show(i), generate(i+1), used[j] = 0;
|
52
|
+
}
|
53
|
+
|
54
|
+
int main() { generate(0); }
|
30
55
|
```
|