1.1. ์ ๊ธ๋ง ๋ฐฉ์ (JugglingAction) ¶
- Leonardong์ด ์์ฑํ ์ฝ๋
- ์ด 4์๊ฐ 8๋ถ ๊ฑธ๋ฆผ
~cpp
#include <iostream>
#include <string>
using namespace std;
string roll( string str,int n, int i );
int getGcd(int aN, int aM);
void main()
{
string str = "abcdefg";
int n = str.length();
int i;
cout << "current : " << str << endl
<< "shitf amount? : ";
cin >> i;
cout << "after shifting : " << roll(str, n, i) << endl;
}
string roll( string str, int n, int i )
{
if ( i % n != 0 )
i %= n;
int gcd = getGcd(n, i);
char temp;
// for ( int count = 0 ; count * gcd < i ; count ++ )
for ( int count = 0 ; count < i ; count += gcd)
for ( int tag = 0 ; tag < gcd ; tag++ ){
temp = str[tag];
for ( int base = tag ; base + gcd < n ; base += gcd)
str[base] = str[base + gcd];
str[base] = temp;
}
return str;
}
int getGcd(int aN, int aM)
{
//assert aN > aM
int remain;
do{
remain = aN % aM;
aN = aM;
aM = remain;
}while( remain != 0 );
return aN;
}
- ์์ฑํ ํ
์ ์ฅ๊ณต๊ฐ์ ํ๋๋ผ๋ ์ค์ด๋ ค๊ณ ๋ณ์๋ฅผ ์ต๋ํ ์ ์ฐ๋ ค๊ณ ๋ ธ๋ ฅํ๋ค. ์์ด ๋ณต์กํด์ก๋ค. ๊ฐ๋จํ ์์๋ณ์๋ ์จ์ค์ผ ๊ฒ ๋ค.
์ํํธ๋ฅผ ์ผ๋ฐํ์์ผ์ ์๊ฐํ๊ณ ๋ฌธ์ ์ ์ ๊ทผํ๋ค. ํ์ง๋ง ํ๋ฆฌ์ง ์์๋ค. ์ฑ ์ ๋ค์ ์ฝ๊ณ , ๊ทธ๋ฆผ์ ๋ด์ ๋ฌด์์ ์ ๋ชป ์ดํดํ๋์ง ์ดํ๋ค. ํ์ง๋ง ์๋ชป ์ดํดํ ๋ถ๋ถ์ ์์๋ค. ์๊ฐ์ด ์ง๋๊ณ , ๋ฌธ์ ๋ฅผ ๋ค์ ์ฝ์ผ๋ฉด ํํธ๋ฅผ ์ป์ง ์์๊น ํ๋ ์๊ฐ์ ๋ฌธ์ ๋ฅผ ์ฝ์๋ค. ๋ฌธ์ ์์ ์ต๋๊ณต์ฝ์๋ผ๋ ๋ง์ ์ ๊ฒฝ์ฐ์ง ์์๋ค๋ ์ ์ ๋ฐ๊ฒฌํ๋ค. ์ต๋๊ณต์ฝ์๋ฅผ ์ด์ฉํด์ ๊ฒฐ๊ตญ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
์ ๋๋ ๋ฐฉ์์ ๋งค๋ฌ๋ฆฌ๋ค ๋ณด๋ ์ฌ์ฌ ๋ต๋ตํ๊ณ ์ง์ฆ์ด ๋ฌ๋ค. ๋ญ๊ฐ ์๋๋ค๋ ์๊ฐ์ด ์๊พธ ๋ค์๋ค.
ํฌ๊ฒ๋ ๊ฐ์ ์๊ณ ๋ฆฌ์ฆ๋ ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ์ ๊ทผ ํ ์ ์๋ค.
๋งํ๋ค๋ ๋๋์ด ๋ค๋ฉด, ๋ฌธ์ ๋ฅผ ๋ค์ ์ดํดํด๋ณธ๋ค. HowToSolveIt์์ ๋์๋ ๋ฐ์ ๋ฅผ ์ค์ค๋ก ํด๋ณธ๋ค. ์ด๋ฒ์๋ ๋นผ๋จน๊ณ ๋์ด๊ฐ ์กฐ๊ฑด์ด์๋๊ฐ?๋ผ๋ ๋ฐ์ ๋ฅผ ๋นจ๋ฆฌ ํ๋๋ผ๋ฉด ํด๊ฒฐ ํ ์ ์์์ ๊ฒ์ด๋ค.
1.2. ์๋ค์ง๊ธฐ ๋ฐฉ์ ¶
- Leonardong๊ณผ ํฉ์ฌ์ ์ PairProgramming
- 30๋ถ ๊ฑธ๋ฆผ
~cpp #include <iostream> #include <string> using namespace std; void reverse(string & str, int start, int end); void main() { string str = "abcdefgh"; int n = str.size(); int i = 3; //shift amount int start, end; start = 0; end = i-1; reverse( str, start, end); start = i; end = n-1; reverse( str, start, end); start = 0; end = n-1; reverse( str, start, end); cout << str << endl; } void reverse(string & str, int front, int rear) { char temp; while( front < rear ){ temp = str[front]; str[front] = str[rear]; str[rear] = temp; front++; rear--; } }
1.3. ์๊ณ ๋ฆฌ์ฆ2 ¶
๋ฏธํด๊ฒฐ
32m์์
32m์์
~cpp
#include <iostream>
#include <string>
using namespace std;
void swap(string & str, int start1, int end1, int start2, int end2){
if ( end1 - start1 == end2 - start2){
char temp;
int limit = end1 - start1 + 1;
for ( int i = 0 ; i < limit ; i++){
temp = str[start1];
str[start1] = str[start2];
str[start2] = temp;
start1++;
start2++;
}
}
}
int main()
{
string str= "abcdefg";
int n = str.size();
int i = 3; // abc defg
swap(str,0,i-1, n-i, n-1);
// swap(str, 0, i-1, n - i, n-1);
// swap(str, 0, i-1, i, n-i);
cout << str << endl;
return 0;
}
2. ํ๊ธฐ ¶
ํฉ์ฌ์
๋ค์ ์ฌ์ ๋ฅผ ๊ฐ์ง๊ณ PairProgramming ์ ํ๋ค. ํผ์ ํ ๋๋ณด๋ค ๋ฌธ์ ํ์
์ด ์ ๋์๊ณ ๋ญ๊ฐ ํ ํธ์ธ ๋๋์ด ๋ค์๋ค. ์์ฌ์ด ์ ์ด๋ผ๋ฉด ๋ฌธ์ ํด๊ฒฐ์ ๋๋ฌด ๋งค๋ฌ๋ ค์ ๋ฆฌํํ ๋ง์ ๊ฒ์๋ฆฌํ ๊ฒฐ๊ณผ ์ฐ์ํ ํ๋ก๊ทธ๋จ์ ์์ฑํ์ง ๋ชปํ๋ค. ๋ ๊ทธ๋ ๋ฏ์ด ์ญํ ์ ๋ง๋ ๋ณ์๋ช
๋ถ์ด๊ธฐ๋ ์ด๋ ต๋ค. ์งง์์ง๋ง ํฅ๋ฏธ์ง์งํ ์๊ฐ์ด์๋ค.










