A - 123233 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.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
Solution 1: count how many times each digit occurs
There are many ways to extract the digits of \(N\), but the following algorithm is the most basic:
While \(N>0\), repeat the following:
- Record the remainder when \(N\) is divided by \(10\). Then, divide \(N\) by \(10\), rounding down the fractional part.
Then, the recorded numbers are the digits of \(N\).
The remainder when \(380\) is divided by \(10\) is \(0\), and dividing \(380\) by \(10\) and rounding down the fractional part yields \(38\).
The remainder when \(38\) is divided by \(10\) is \(8\), and dividing \(38\) by \(10\) and rounding down the fractional part yields \(3\).
The remainder when \(3\) is divided by \(10\) is \(3\), and dividing \(3\) by \(10\) and rounding down the fractional part yields \(0\).
This way, the digits of \(380\) have been obtained: \(3, 8\), and \(0\).
This process can be concisely implemented using a while statement.
After finding the digits, count the occurrences of each number to check if the condition in the problem statement is satisfied. It is easy to implement in the manner of packet sort.
Sample code (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> mem(10,0);
while(n>0){
mem[n%10]++;
n/=10;
}
if(mem[1]==1 && mem[2]==2 && mem[3]==3){
cout << "Yes\n";
}
else{
cout << "No\n";
}
return 0;
}
Solution 2: sort the digit to check if it matches with 122333
The answer is Yes if and only if sorting the digits of \(N\) yields \(122333\).
Thus, the problem can be also answered by, after receiving the \(N\) as a string or converting the received integer \(N\) into a string, sort the characters in the string in ascending order, and checking if it coincides with \(122333\).
Sample code 1 (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
string s=to_string(n);
sort(s.begin(),s.end());
if(s=="122333"){cout << "Yes\n";}
else{cout << "No\n";}
return 0;
}
Sample code 2 (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
sort(s.begin(),s.end());
if(s=="122333"){cout << "Yes\n";}
else{cout << "No\n";}
return 0;
}
posted:
last update: