回答編集履歴
1
修正
answer
CHANGED
|
@@ -1,29 +1,25 @@
|
|
|
1
|
-
...こんなの?
|
|
2
1
|
```C++
|
|
3
|
-
#include<iostream>
|
|
4
|
-
bool has_duplicate_digits(int x)
|
|
2
|
+
bool has_duplicate_digits(int x) {
|
|
5
|
-
|
|
6
|
-
int
|
|
3
|
+
int counts[10] = {0};
|
|
7
|
-
int x;
|
|
8
|
-
std::cout << "整数を入力してください";
|
|
9
|
-
std::cin >> x;
|
|
10
|
-
|
|
4
|
+
// 各数の出現頻度をcountsに求め、
|
|
11
|
-
|
|
5
|
+
while ( x != 0 ) {
|
|
12
|
-
|
|
6
|
+
counts[x % 10]++;
|
|
7
|
+
x /= 10;
|
|
13
|
-
|
|
8
|
+
}
|
|
9
|
+
// counts 内に2以上のものがあれば重複あり
|
|
14
|
-
|
|
10
|
+
for ( int i = 0; i < 10; ++i ) {
|
|
15
|
-
|
|
11
|
+
if ( counts[i] > 1 ) return true;
|
|
16
|
-
|
|
12
|
+
}
|
|
17
|
-
|
|
13
|
+
return false;
|
|
18
14
|
}
|
|
19
|
-
|
|
15
|
+
```
|
|
16
|
+
あるいは
|
|
17
|
+
```C++
|
|
20
18
|
#include <string>
|
|
21
|
-
#include <
|
|
19
|
+
#include <set>
|
|
22
20
|
|
|
23
21
|
bool has_duplicate_digits(int x) {
|
|
24
|
-
|
|
22
|
+
std::string digits = std::to_string(x);
|
|
25
|
-
std::sort(digits.begin(),digits.end());
|
|
26
|
-
return digits.size() !=
|
|
27
|
-
|
|
23
|
+
return std::set<char>(digits.begin(), digits.end()).size() != digits.size();
|
|
28
24
|
}
|
|
29
25
|
```
|