add: 220411-CN [cpp]

This commit is contained in:
eat-swap 2022-04-11 11:46:43 +08:00
parent ba2e393b13
commit caa5f4662c
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 36 additions and 1 deletions

35
cpp/2204/220411-CN.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <cstdio>
/**
* 357. Count Numbers with Unique Digits
* Given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10^n.
*/
class Solution {
public:
static int countNumbersWithUniqueDigits(int n) {
static const int ans[] = {1, 10, 91, 739, 5275, 32491, 168571, 712891, 2345851};
return ans[n];
}
};
bool isUnique(int x) {
int c[10]{};
for (; x; x /= 10)
if (c[x % 10]++)
return false;
return true;
}
int main() {
int ans = 0;
for (int i = 0, j = 1; i < 9; ++i, j *= 10) {
for (int k = j / 10; k < j; ++k) {
if (isUnique(k)) {
++ans;
}
}
std::printf("%d(%d) -> %d\n", i, j, ans);
}
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2204)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220410-CN.cpp)
ADD_EXECUTABLE(2204 220411-CN.cpp)