Official
A - 321-like Checker Editorial 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.
Approach 1: decompose \(N\) into digits
One can decompose \(N\) into digits as follows.
- First, let \(D=[]\).
- While \(N>0\), repeat the following.
- Push to \(D\) the remainder when \(N\) is divided by \(10\).
- Then, replace \(N\) with the quotient when is \(N\) divided by \(10\).
- Finally, reverse \(D\).
For example, when \(N=321\) the procedure goes as follows.
- Start with \(D = []\).
- Since \(321>0\), push \(1\) to the tail of \(D\), and replace \(N\) with \(32\). Now, \(D=[1]\).
- Since \(32>0\), push \(2\) to the tail of \(D\), and replace \(N\) with \(3\). Now, \(D=[1,2]\).
- Since \(3>0\), push \(3\) to the tail of \(D\), and replace \(N\) with \(0\). Now, \(D=[1,2,3]\).
- Now that \(N=0\), terminate the loop.
- Finally, reverse \(D\) to obtain \(D=[3,2,1]\).
With this \(D\), one can check the condition in the problem statement using a loop to solve the problem.
To check the condition, it is good idea to deform the condition
Yes
if (\(i\)-th digit) \(>\) (\((i+1)\)-th digit) for all \(i\)
into
No
if (\(i\)-th digit) \(\le\) (\((i+1)\)-th digit) for all \(i\).
Sample code (Python):
N = int(input())
D = []
while N>0:
D.append(N%10)
N //= 10
D.reverse()
for i in range(1,len(D)):
if D[i-1]<=D[i]:
print("No")
exit()
print("Yes")
Approach 2: receive \(N\) as a string
If one can somehow receive \(N\) as a string, then its \(i\)-th character is the \(i\)-th significant digit of \(N\).
Then one can use that string to check the condition, where \(N\) does no longer need to be decomposed into digits.
Sample code (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
for(int i=1;i<s.size();i++){
if(s[i-1]<=s[i]){
cout << "No\n";
return 0;
}
}
cout << "Yes\n";
return 0;
}
posted:
last update: