LoginSignup
0
0

More than 5 years have passed since last update.

NxNの行列を時計回りに90度回転させるプログラム

Last updated at Posted at 2019-01-02

ソースコード

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
  int N;
  cin>>N;
  vector<string> matrix(N);
  int i;
  for(i=0;i<N;i++){
    cin>>matrix[i];
  }
  int layer;
  for(layer=0;layer<N/2;++layer){
    int first=layer;
    int last=N-1-layer;
    for(i=first;i<last;i++){
      int j=last-i+first;
      int tmp=matrix[first][i];
      matrix[first][i]=matrix[j][first];
      matrix[j][first]=matrix[last][j];
      matrix[last][j]=matrix[i][last];
      matrix[i][last]=tmp;
    }
  }
  for(i=0;i<N;i++){
    cout<<matrix[i]<<endl;

  }
  return 0;
}

入力例

4
1111
2222
3333
4444

出力例

4321
4321
4321
4321
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0