hiro1729 競プロ

競プロの解説などを書きます。

ABC328-B 解説

B - 11/11

月と日の組み合わせを全探索します。月と日の組み合わせが正しい条件は、月と日の全ての数字が等しいことなので、setに突っ込みます。

Python

N = int(input())
D = list(map(int, input().split()))
cnt = 0
for i in range(N):
    for j in range(1, D[i] + 1):
        if len(set(str(i + 1) + str(j))) == 1:
            cnt += 1
print(cnt)

C++

#include <iostream>
#include <set>
using namespace std;

int main() {
    int N;
    cin >> N;
    int cnt = 0;
    for (int i = 1; i <= N; i++) {
        int D;
        cin >> D;
        for (int j = 1; j <= D; j++) {
            set<int> s;
            int m = i, d = j;
            while (m) {
                s.insert(m % 10);
                m /= 10;
            }
            while (d) {
                s.insert(d % 10);
                d /= 10;
            }
            if (s.size() == 1) {
                cnt++;
            }
        }
    }
    cout << cnt << '\n';
}