您的位置:首页 > 职场人生

面试题12 打印1到最大的N位数

2016-04-04 17:26 417 查看
题目描述:

给定一个数字N,打印从1到最大的N位数。

#include<iostream>
using namespace std;

void Print(int k, int n, int x[]) {
if (n == 0)
{
cerr << "error" << endl;
exit(0);
}
else
{
if (k >= n)
{
int flag = 0;
for (int i = 0; i < k; i++)
{
if (x[i] == 0 && flag == 0) {
continue;
}
else {
flag = 1;
cout << x[i];
}
}
if (flag != 0)
{
cout << endl;
}
}
else
{
for (int i = 0; i <= 9; i++) {
x[k] = i;
Print(k + 1, n, x);
}
}
}
}
int main()
{
int n;
cin >> n;
int *x = new int
;
for (int i = 0; i < n; i++)
x[i] = 0;
Print(0, n, x);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: