๋ฌธ์ ๋ ์ฌ๊ธฐ๋ฅผ ํด๋ฆญ
์ฝ๋๋ก ๋ฐ๋ก ์ด๋์ ์ฌ๊ธฐ๋ฅผ ํด๋ฆญํ์ธ์
ํ์ด
๐ ๋ฌธ์ ์ ๋ชฉ : ์ธ๋ก ์ฝ๊ธฐ
๐ ์นดํ
๊ณ ๋ฆฌ : ๋ฌธ์์ด, ์ ๋ํน
์ ๋ํน : ํด๋น ๋ฌธ์ ๋ฅผ ํ์ดํ๊ธฐ ์ํด ๊ณ ์๋ ํน์ํ ์๊ณ ๋ฆฌ์ฆ ์์ด, ํด๋น ๋ฌธ์ ๋ง์ ์ํด ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํด์ผ ํ๋ ๋ฌธ์ ์ ์ข
๋ฅ. ๊ทธ๋ฅ ์ฌ๋ฐ๋ ๋ฌธ์ ํ์ด ์ ๋์ด๋ค.
๋ด๊ฐ ๋ฐฐ์ด ๊ฒ
ํ๊ณ ์ฐพ์๋ณด๋ char 2์ฐจ์ ๋ฐฐ์ด๋ก ํ๋ฉด ๋๋ค.
char str[5][MAX] = { NULL, };
for(int i=0;i<5;i++) cin >> str[i];
ํฌ์ธํฐ ํ์
์ผ๋ก ์๊ฐํด๋ณด๋ฉด, C์์๋ ๊ฐ๋ฅํ ํ์ด์ธ๋ฐ C++์ด๋ผ๋ ์ด์ ๋ก deque๋ฅผ ์ฌ์ฉํด ๋นํจ์จ์ ์ธ ๋ฉ๋ชจ๋ฆฌ ๋ญ๋น๋ฅผ ์ผ์ผ์ผฐ๋ค๋ ์ ์ด ์์ฝ๋ค.
์ ์ ํ์ด๋ steffen-lee๋์ ๋ฌธ์ ํ์ด์ธ ๋ฏ. ์ ์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
#include<iostream>
using namespace std;
const int Max = 15;
int main() {
char str[5][Max] = { NULL, };//NULL๋ก ์ด๊ธฐํ
for (int i = 0; i < 5; ++i)
cin >> str[i];
for (int j = 0; j < Max; ++j) {
for (int i = 0; i < 5; ++i) {
if (str[i][j] == NULL)//NULL์ด ์์ผ๋ฉด ๊ฑด๋๋
continue;
cout << str[i][j];
}
}
return 0;
}
๋ณด๋ฉด ๊ตณ์ด โ*โ ๋ฑ, ๊ณต๋ฐฑ์ ํํํ๋ ๋ค๋ฅธ ์๋ฅผ ์ฝ์
ํ์ง ์๊ณ ๋ฐฐ์ด ํฌ๊ธฐ๋ฅผ ์ก๊ณ , ๊ทธ ์์ ์
๋ ฅ๋๋ string์ ์ฝ์
ํ ํ, ๋ง์ฝ ํด๋น ๋ถ๋ถ์ด NULL์ด๋ผ๋ฉด ์ฌ์ฉํ์ง ์์๋ค. NULL์ธ ๋ถ๋ถ์ ์ ๊ทผํ๋ ๋ฐ์์ด ๋ ๊น๋ฑ ์ค์ํ๋ค๊ฐ ์๋ฌ๊ฐ ๋ ๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ ์์ฃผ ์จ๋จน์ง ์์์๋๋ฐ, ๊ตณ์ด.. ์กฐ์ฌ์ ํด์ผ ํ ๊น? ๊ท๋ชจ๊ฐ ํฐ ํ๋ก๊ทธ๋๋ฐ์ด๋ฉด ๋ชฐ๋ผ๋, ๋๋ ๋ชจ๋ฅด๋ ๊ณณ์์ NULL์ ์ฐธ์กฐํด์ ์๋ฌ๊ฐ ๋ ๋ฆฌ๋ ์๊ณ โฆ
์๋ฌดํผ๊ฐ์ ๋ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
์ฝ๋
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
int main()
{
deque<string> charArr;
int maxL = 0;
for (int h = 0; h < 5; h++)
{
string tmp;
cin >> tmp;
charArr.push_back(tmp);
maxL = max(maxL, int(tmp.length()));
}
for (int h = 0; h < 5; h++)
{
string tmp = charArr.front();
charArr.pop_front();
int L = tmp.length();
tmp.append(maxL - L, '*');
charArr.push_back(tmp);
}
for(int l=0;l<maxL;l++)
for (int h = 0; h < 5; h++)
{
string tmp = charArr.front();
charArr.pop_front();
if (tmp[l] != '*') cout << tmp[l];
charArr.push_back(tmp);
}
return 0;
}