题目:输入数字n,按顺序输出从1最大的n位10进制数。比如输入3,则输出1、2、3一直到最大的3位数即999
//输出n位的数,用这种方法可以防止出现越界 #include <iostream> #include <string> using namespace std; void printDecimal(int n) { if(n <= 0) return; string str = "1"; while(str.size() <= n) { cout<<str << " "; int i = str.size() - 1; while(i>= 0 && str.at(i) == '9') { str[i] = '0'; i--; } if(i == -1) str = "1" + str; else str[i] = str[i] + 1; } } int main() { printDecimal(3); return 0; }