// 程序面100题(38)-输出1到最大的N数位.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
char chead[]={'1','2','3','4','5','6','7','8','9'};
char ctail[]={'0','1','2','3','4','5','6','7','8','9'};// need n layers for loop
string currentstr[10];
void recuiseprint(int m,int n)//this is wrong, at least need n layers for loops
{
if (m==0)
{
for (int j=0;j<9;j++)
{
cout<<chead[j];
recuiseprint(m+1,n);
}
}
else
{
if (m==n)
{
cout<<" ";
}
else
{
for (int j=0;j<10;j++)
{
cout<<ctail[j];
recuiseprint(m+1,n);
}
}
}
}
bool addOne(char *bigint,int n)//0019 return value is false means overflow
{
bigint[n-1]+=1;
int j=n-1;
while(bigint[j]>'9')
{
bigint[j]='0';
j--;
if (j<0)
return false;
else
bigint[j]++;
}
return true;
}
void printbig(char *bigint,int n)
{
int j=0;
while(bigint[j]=='0')j++;
for (int i=j;i<n;i++)
{
cout<<bigint[i];
}
cout<<" ";
}
void recursive(char *bigint,int m,int n)//m
{
if (m==n)//here noticeable
{
printbig(bigint,n);
}
else
{
for (char j='0';j<='9';j++)
{
bigint[m]=j;
recursive(bigint,m+1,n);
//bigint[m]=j; this is not necessary why? because in every recursive bigint is reset by j,no need to recover
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int n;
cin>>n;
char *big=new char[n];
for (int i=0;i<n;i++)
{
big[i]='0';
}
while (addOne(big,n))
{
printbig(big,n);
}
cout<<endl<<"recursive"<<endl;
recursive(big,0,n);
system("pause");
return 0;
}
程序面100题(38)-输出1到最大的N数位
最新推荐文章于 2022-05-03 23:19:41 发布