如果基数是b,那么有0到b-1个不同的数字。所以可以生成b^n个不同的n位数(包括前导0)。如果我们忽略第一个数字0,那么有b^(n-1)个数字。所以没有前导0的总共n位数是b^n - b^(n-1)
算法countndigitnum(n, b)begin total := bn with_zero := bn-1 return bn – bn-1end
example的中文翻译为:示例#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
以上就是所有可能的n位数和基数b,但不包括前导零的数字的详细内容。