利用递归的先天条件排列组合
#include "stdafx.h"
#include <iostream>
using namespace std;
void printBigInt(const char* c) {
int i = 0;
while (c[i] == '0') {
++i;
}
// not print zero
if (i == strlen(c)) {
return;
}
printf("%s\n", c + i);
}
void reSolver(char* c, int index, int len) {
if (len == index) {
printBigInt(c);
return;
}
for (int i = 0; i < 10; ++i) {
c[index] = i + '0';
reSolver(c, index + 1, len);
}
return;
}
int main() {
int n = 0;
cin >> n;
if(n <= 0) return;
char* c = new char[n + 1];
c[n] = '\0';
reSolver(c, 0, n);
}
n 层 for 循环
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void printBigInt(const vector<char>& c) {
int i = 0;
while (c[i] == '0') {
++i;
}
// not print zero
if (i == c.size()) {
return;
}
cout << string(c.begin() + i, c.end()) << endl;
}
int main() {
int n = 0;
cin >> n;
if (n <= 0) return 0;
vector<char> c(n + 1, '0');
c[n] = '\0';
while (k >= 0) {
printBigInt(c);
int k = n - 1;
while (k >= 0) {
++c[k];
if (c[k] <= '9') {
break;
}
else {
c[k] = '0';
--k;
}
}
}
}