回答編集履歴
3
重複除去とソートの話を追記
answer
CHANGED
@@ -73,4 +73,37 @@
|
|
73
73
|
if( CheckResult1[i] || CheckResult2[i] ){ printf( "%d ", i+1 ); }
|
74
74
|
}
|
75
75
|
printf( "\n" );
|
76
|
+
```
|
77
|
+
|
78
|
+
---
|
79
|
+
|
80
|
+
値の種類が少ないことを利用すれば,「重複を除去して昇順に」の話も同じ考えでやれる.
|
81
|
+
|
82
|
+
```C++
|
83
|
+
#define ARRAY_SIZE 10
|
84
|
+
|
85
|
+
int main(void)
|
86
|
+
{
|
87
|
+
//元の配列
|
88
|
+
const int SrcArray[ARRAY_SIZE] = { 5, 8, 4, 3, 7, 9, 7, 9, 5, 1 };
|
89
|
+
//結果配列用
|
90
|
+
int ResultArraySize = 0;
|
91
|
+
int ResultArray[ARRAY_SIZE] = { 0,0,0,0,0, 0,0,0,0,0 };
|
92
|
+
|
93
|
+
//1~10の各値が元の配列に存在するか調べる
|
94
|
+
unsigned char CheckResult[10] = { 0,0,0,0,0, 0,0,0,0,0 };
|
95
|
+
for( int i=0; i<ARRAY_SIZE; ++i ){ CheckResult[ SrcArray[i]-1 ] = 1; }
|
96
|
+
//調べた結果から,結果配列を作る
|
97
|
+
for( int i=0; i<10; ++i )
|
98
|
+
{
|
99
|
+
if( CheckResult[i] ){ ResultArray[ ResultArraySize++ ] = i+1; }
|
100
|
+
}
|
101
|
+
|
102
|
+
//結果表示
|
103
|
+
printf( "重複を取り除いてソートした結果(%d 個になった)\n", ResultArraySize );
|
104
|
+
for( int i=0; i<ResultArraySize; ++i ){ printf( "%d ", ResultArray[i] ); }
|
105
|
+
printf( "\n" );
|
106
|
+
|
107
|
+
return 0;
|
108
|
+
}
|
76
109
|
```
|
2
bitがどうのを使わないパターンを追加
answer
CHANGED
@@ -46,4 +46,31 @@
|
|
46
46
|
|
47
47
|
return 0;
|
48
48
|
}
|
49
|
+
```
|
50
|
+
|
51
|
+
「bitがどうの」的な話が嫌なら,
|
52
|
+
配列1の調査結果と配列2の調査結果を別個に持てばいい.
|
53
|
+
調査結果データの表現方法が異なるだけで,やってることは一緒.
|
54
|
+
|
55
|
+
```C++
|
56
|
+
//調べる処理
|
57
|
+
unsigned char CheckResult1[10] = { 0,0,0,0,0, 0,0,0,0,0 };
|
58
|
+
unsigned char CheckResult2[10] = { 0,0,0,0,0, 0,0,0,0,0 };
|
59
|
+
for( int i=0; i<ARRAY1_SIZE; ++i ){ CheckResult1[ Array1[i]-1 ] = 1; }
|
60
|
+
for( int i=0; i<ARRAY2_SIZE; ++i ){ CheckResult2[ Array2[i]-1 ] = 1; }
|
61
|
+
|
62
|
+
//結果表示
|
63
|
+
printf( "共通 : " );
|
64
|
+
for( int i=0; i<10; ++i )
|
65
|
+
{
|
66
|
+
if( CheckResult1[i] && CheckResult2[i] ){ printf( "%d ", i+1 ); }
|
67
|
+
}
|
68
|
+
printf( "\n" );
|
69
|
+
|
70
|
+
printf( "どっちかにある : " );
|
71
|
+
for( int i=0; i<10; ++i )
|
72
|
+
{
|
73
|
+
if( CheckResult1[i] || CheckResult2[i] ){ printf( "%d ", i+1 ); }
|
74
|
+
}
|
75
|
+
printf( "\n" );
|
49
76
|
```
|
1
コード例追記
answer
CHANGED
@@ -8,4 +8,42 @@
|
|
8
8
|
//各 CheckFlag[x] はbitフラグの組み合わせとして,
|
9
9
|
//配列1にある場合は.0x01 を,配列2にある場合は0x02 をORする形で結果を作るとか,そんな感じで.
|
10
10
|
unsigned char CheckFlag[10] = { 0,0,0,0,0, 0,0,0,0,0 };
|
11
|
+
```
|
12
|
+
|
13
|
+
---
|
14
|
+
|
15
|
+
こんな感じで.
|
16
|
+
|
17
|
+
```C++
|
18
|
+
#define ARRAY1_SIZE 10
|
19
|
+
#define ARRAY2_SIZE 10
|
20
|
+
|
21
|
+
int main(void)
|
22
|
+
{
|
23
|
+
//配列1と配列2
|
24
|
+
const int Array1[ARRAY1_SIZE] = { 5, 8, 4, 3, 7, 9, 7, 9, 5, 1 };
|
25
|
+
const int Array2[ARRAY2_SIZE] = { 7, 6, 1, 5, 9, 9, 10, 8, 4, 3 };
|
26
|
+
|
27
|
+
//調べる処理
|
28
|
+
unsigned char CheckFlag[10] = { 0,0,0,0,0, 0,0,0,0,0 };
|
29
|
+
for( int i=0; i<ARRAY1_SIZE; ++i ){ CheckFlag[ Array1[i]-1 ] |= 0x01; }
|
30
|
+
for( int i=0; i<ARRAY2_SIZE; ++i ){ CheckFlag[ Array2[i]-1 ] |= 0x02; }
|
31
|
+
|
32
|
+
//結果表示
|
33
|
+
printf( "共通 : " );
|
34
|
+
for( int i=0; i<10; ++i )
|
35
|
+
{
|
36
|
+
if( CheckFlag[i]==0x03 ){ printf( "%d ", i+1 ); }
|
37
|
+
}
|
38
|
+
printf( "\n" );
|
39
|
+
|
40
|
+
printf( "どっちかにある : " );
|
41
|
+
for( int i=0; i<10; ++i )
|
42
|
+
{
|
43
|
+
if( CheckFlag[i] ){ printf( "%d ", i+1 ); }
|
44
|
+
}
|
45
|
+
printf( "\n" );
|
46
|
+
|
47
|
+
return 0;
|
48
|
+
}
|
11
49
|
```
|