在这里,我们将看到一个问题,我们有N个和基数B。我们的任务是计算基数B的所有N位数字,而没有任何前导0。因此,如果N为2,B为2,将有四个数字00、01、10、11。因此,对于本节,只有两个数字有效。它们是10、11,没有前导0。
如果基数为B,则从0到B – 1不同的数字。因此,可以生成B个N个不同的N位数字值(包括前导0)。如果我们忽略它的第一个数字是0m,则有B N-1数字。因此,无前导0的N个数字总数为B N – B N-1
Begin total := BN with_zero := BN-1 return BN – BN-1End
#include <iostream>
#include <cmath>
using namespace std;
int countNDigitNum(int N, int B) {
int total = pow(B, N);
int with_zero = pow(B, N - 1);
return total - with_zero;
}
int main() {
int N = 5;
int B = 8;
cout << "Number of values: " << countNDigitNum(N, B);
}输出结果
Number of values: 28672