본문 바로가기
프로그램/C++ 1000제

C++ 48제] vector 사용, 문제와 같이 출력하도록 하시오.

by 건티 2022. 6. 17.
728x90

출처 : 반크_세계유산 석굴암

 

문제]

1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25

 

참고풀이]

#include <iostream>
#include <vector>
#include <iomanip> //setw()
using namespace std;

int main()
{
   //2차원 vector를 선언하고 초기값으로 0을 셋팅한다. 
   vector<vector<int> > v(5,vector<int>(5,0));
   int r,c;//행열변수 
   int n;//증가값변수
   int sw;//열위치 변환변수 
   int i;//반복변수

   //vector에 증가값을 넣는다.
   sw=1;
   c=-1;
   n=0;
   for(r=0;r<5;r++) 
   {
      for(i=1;i<=5;i++)
      {
         c+=sw;
         v[r][c]=++n;
      }
      c+=sw;
      sw*=-1;
   }

   //결과출력 
   for(r=0;r<5;r++)
   {
      for(c=0;c<5;c++)
         cout << setw(3) << v[r][c];
      cout << endl;
   }
   return 0;
}

 

참고풀이 결과]

 

 

 

대한민국의 아름다운 영토, 독도의 여름

 

반응형

댓글