A - A 解説 by en_translator
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” (https://atcoder.jp/contests/abs).
There are various approaches. Please refer to the implementation examples below. Presented later in this article are curated showcase of the sample code among these.
- Replace characters one by one: Sample code (C++)
- Print the answer one character by one: Sample code (C++) / Sample code (Python)
- Construct another answer string: Sample code (C++) / Sample code (Python)
- (Advanced) Use the standard library’s string/sequence operations: Sample code (C++, replace_if) / Sample code (Python, regular expression)
(C++) Replace characters one by one
#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
for (int i = 0; i < S.size(); i++)
if (S[i] != 'A')
S[i] = '.';
/*
One canse also use a range-based for statement as follows:
for (char& c : S)
if (c != 'A')
c = '.';
*/
cout << S << endl;
}
(Python) Print the answer one by one
S = input()
for c in S:
# One can also write print("A" if c == "A" else ".", end="")
if c == "A":
print("A", end="")
else:
print(".", end="")
(C++) Construct another answer string
#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
string T = "";
for (char c : S)
// One can also write T += c == 'A' ? 'A' : '.'
if (c == 'A')
T += 'A';
else
T += '.';
cout << T << endl;
}
(Python) Construct another answer string
S = input()
# Combine the list comprehension syntax and join
print("".join("A" if c == "A" else "." for c in S))
投稿日時:
最終更新: