公式
B - Doors in the Center 解説
by
B - Doors in the Center 解説
by
kyopro_friends
初心者の方へ
- プログラミングの学習を始めたばかりで何から手をつけるべきかわからない方は、まずは practice contest の問題A「Welcome to AtCoder」をお試しください。言語ごとに解答例が掲載されています。
- また、プログラミングコンテストの問題に慣れていない方は、 AtCoder Beginners Selection の問題をいくつか試すことをおすすめします。
- C++入門 AtCoder Programming Guide for beginners (APG4b) は、競技プログラミングのための C++ 入門用コンテンツです。
- Python入門 AtCoder Programming Guide for beginners (APG4bPython) は、競技プログラミングのための Python 入門用コンテンツです。
\(N\) が偶数のとき
答えとなる文字列は ----==----
のように =
を中央に \(2\) 個、-
をその左右に合わせて \(N-2\) 個含みます。-
を =
の左右に均等に分け、
-
を \(\frac{N-2}{2}\) 個=
を \(2\) 個-
を \(\frac{N-2}{2}\) 個
をこの順につなげた文字列が答えとなります。
\(N\) が奇数のとき
答えとなる文字列は ----=----
のように =
を中央に \(1\) 個、-
をその左右に合わせて \(N-1\) 個含みます。-
を =
の左右に均等に分け、
-
を \(\frac{N-1}{2}\) 個=
を \(1\) 個-
を \(\frac{N-1}{2}\) 個
をこの順につなげた文字列が答えとなります。
実装
実装例(Python)
N=int(input())
if N%2==0:
c = (N-2)//2
print('-'*c + '='*2 + '-'*c)
else:
c = (N-1)//2
print('-'*c + '=' + '-'*c)
実装例(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;
}
}
投稿日時:
最終更新: