公式

B - Doors in the Center 解説 by en_translator


For beginners

If \(N\) is even

The sought string looks like ----==----, containing two =’s at the middle, and \((N-2)\) -’s to their left and right. Distributing -’s evenly on the both sides of =, the answer is the concatenation of

  • \(\frac{N-2}{2}\) copies of -,
  • \(2\) copies of =, and
  • \(\frac{N-2}{2}\) copies of -.

If \(N\) is odd

The sought string looks like ----=----, containing one = at the middle, and \((N-1)\) -’s to its left and right. Distributing -’s evenly on the both sides of =, the answer is the concatenation of

  • \(\frac{N-1}{2}\) copies of -,
  • \(1\) copies of =,
  • \(\frac{N-1}{2}\) copies of -.

Implementation

Sample code (Python)

N=int(input())

if N%2==0:
  c = (N-2)//2
  print('-'*c + '='*2 + '-'*c)
else:
  c = (N-1)//2
  print('-'*c + '=' + '-'*c)

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  cin >> n;

  if(n%2 == 0){
    int c = (n-2)/2;
    cout << string(c,'-') + "==" + string(c,'-') << endl;
  }else{
    int c = (n-1)/2;
    cout << string(c,'-') + "=" + string(c,'-') << endl;
  }
}

投稿日時:
最終更新: