程序面100题(38)-输出1到最大的N数位

// 程序面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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值