A - Hell, World! 解説 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).
This problem can be solved by removing the \(X\)-th character of HelloWorld.
There are various ways to remove the \(X\)-th character of a string; for example, one can combine a for statement and an if statement to implement the following:
- Let \(S=\)
HelloWorld, and \(T\) be an empty string. - While \(i=1,2,\ldots,10\), do the following:
- If \(i\neq X\), concatenate \(S_i\) to the end of \(T\).
- Now \(T\) is the sought answer.
By constructing \(T\) this way, only the \(X\)-th character of \(S\) is skipped, yielding the string obtained by removing only the \(X\)-th character of HelloWorld.
Alternatively, for example in C++, one can remove the \(x\)-th character of a string \(S\) with S.erase(S.begin() + x) (notice one must use 0-based indexing). Such a method also correctly solves the problem.
x = int(input()) - 1
s = "HelloWorld"
t = ""
for i in range(10):
if i != x:
t += s[i]
print(t)
#include <bits/stdc++.h>
using namespace std;
int main() {
int x;
cin >> x;
x--;
string s = "HelloWorld";
s.erase(s.begin() + x);
cout << s << endl;
return 0;
}
投稿日時:
最終更新: