公式
B - Doors in the Center 解説 by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
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;
}
}
投稿日時:
最終更新: