C++ 中 {0, 1, 2, 3, 4, 5} 中的第 n 个数字

在本教程中,我们将编写一个程序来查找具有给定数字的第 n 个数字。

由数字组成的数字是

0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25, 等等,

我们可以使用前 6 位数字组成上述序列。让我们看一个数字形成的例子。

输入

1 * 10 + 0 = 10
1 * 10 + 1 = 11
1 * 10 + 2 = 12
1 * 10 + 3 = 13
1 * 10 + 4 = 14
1 * 10 + 5 = 15

同样,申请号码2、3、4、5。你会得到所有的号码。返回序列中的第 n 个数字。

示例

让我们看看代码。

#include <bits/stdc++.h>
using namespace std;
int findNthNumber(int n) {
   vector<int> numbers;
   for (int i = 0; i < 6; i++) {
      numbers.push_back(i);
   }
   for (int i = 0; i <= n / 6; i++) {
      for (int j = 0; j < 6; j++) {
         if ((numbers[i] * 10) != 0) {
            numbers.push_back(numbers[i] * 10 + numbers[j]);
         }
      }
   }
   return numbers[n - 1];
}
int main() {
   int n = 7;
   cout << findNthNumber(n) << endl;
   return 0;
}
输出结果

如果你运行上面的代码,那么你会得到下面的结果。

10

结论

如果您对本教程有任何疑问,请在评论部分提及。