AtCoderで入茶を目指して勉強しています。
勉強を継続するために投稿を始めました。
もともとアカウントを作成していましたが、今年の4月から本格的に勉強を始めました。
一応自分用に解法を書いていますが雑です、自分で読み返して困ったら修正します。
私のアカウント
解いた問題
本日解いた問題
B - Uneven Numbers
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
ll n;
cin >> n;
ll res = 0;
for(int i = 1; i <= n; i++){
string s = to_string(i);
ll x = s.size();
if(x%2==1) res++;
}
cout << res << endl;
}
解法
N
以下で桁数が奇数になるものを全て数える。
B - 105
B - 105
解答
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
ll n;
cin >> n;
ll res = 0;
for(int i = 1; i <= n; i++){
ll divi = 0;
if(i%2==0)continue;
for(int j = 1; j <= i; j++){
if(i%j == 0) divi++;
}
if(divi==8) res++;
}
cout << res << endl;
}
解法
1からN
までの奇数において約数が何個になるか全探索を行い、出力する。
B - K-th Common Divisor
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
ll a, b, k;
cin >> a >> b >> k;
vec cnt(0);
ll mmax = max(a, b);
for(int i = 1; i <= mmax; i++){
if(a%i==0 && b%i==0) cnt.push_back(i);
}
ll s = cnt.size();
cout << cnt[s - k] << endl;
}
解法
A
、B
どちらも割り切れる正整数を入れるための配列を用意する。その後全探索を行い配列に入れていき、K
番目に大きいものを出力する。
D - Lucky PIN
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
ll n;
cin >> n;
string s;
cin >> s;
ll res = 0;
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
for(int k = 0; k < 10; k++){
ll cnt = 0;
bool jud1=false,jud2=false,jud3= false;
while(cnt < n){
if(s[cnt] == i + '0') {
jud1 = true;
cnt++;
break;
}
cnt++;
}
while(cnt < n){
if(s[cnt] == j + '0') {
jud2 = true;
cnt++;
break;
}
cnt++;
}
while(cnt < n){
if(s[cnt] == k + '0') {
jud3 = true;
cnt++;
break;
}
cnt++;
}
if(jud1&&jud2&&jud3) res++;
}
}
}
cout << res << endl;
}
解法
暗証番号は000
から999
までなので1桁ずつ考え、全探索を行う。暗証番号の最初の数字がS
の要素に含まれていたら次の要素も含まれているか、のように探索を行うことで結果が求まる。