比如

n = 2

那么从1一直输出到99

分析

直接输出,遇到大数时肯定有问题,比如n=100,存储100位的数据类型不存在。

可以利用数组来存储大数,比如n=100,可以开辟个数组 char a[101]

思路一

模拟现实中的技术方式,逢九进一

参考代码

#include <iostream>
#include <cstring>
using namespace std; bool minuxOne(char *a, int *end_index, int size)
{
if((*end_index == size - && a[size - ] == '') || *end_index < || *end_index >= size)
return false;
int tmp = size - ;
if(a[size - ] != '')
--a[size -];
else
{
a[size - ] = '';
while()
{
if(a[tmp] == '')
{
a[tmp] = '';
--tmp;
}
else
{
--a[tmp];
break;
}
}
if(a[*end_index] == '')
++*end_index;
}
return true;
} bool printNum(const int size)
{
if(size < )
return false;
char *a = new char[size];
memset(a, '', size);
int end_index = ;
while()
{
if(end_index == size - && a[size - ] == '')
break;
for(int i = ; i < size; ++i)
{
if(end_index > i && a[i] == '' )
continue;
cout << a[i];
}
cout << "\t";
if(!minuxOne(a, &end_index, size))
break;
}
delete []a;
return true;
} int main()
{
int size = ;
printNum(size);
}

结果

思路二

本质可以看作是全排列,只是前边的0别输出

#include <iostream>
using namespace std; bool print1ToMaxN(const int n);
bool print1ToMaxNRecursion(char *a, int size, int index);
bool printNum(char *a, int size);
int main()
{
int size = ;
print1ToMaxN(size);
} bool print1ToMaxN(const int size)
{
if(size <= )
return false;
char *a = new char[size];
for(int i = ; i <= ; ++i)
{
a[] = i + '';
print1ToMaxNRecursion(a, size, );
}
return true;
} bool print1ToMaxNRecursion(char *a, int size, int index)
{
if(size <= || index < )
return false;
if(index == size)
{
printNum(a, size);
return true;
}
for(int i = ; i <= ; ++i)
{
a[index] = i + '';
print1ToMaxNRecursion(a, size, index + );
}
} bool printNum(char *a, int size)
{
if(size <= )
return false;
bool IsPre0 = true;
for(int i = ; i < size; ++i)
{
if(IsPre0 && a[i] != '')
IsPre0 = false;
if(!IsPre0)
cout << a[i];
}
cout << "\t";
}

结果

分析

1. 输出问题:一开始为0不要输出,这需要输出时判断下

2. 递归全排列的本质原理:每一位字符0~0都有可能,在方位本位的时候递归遍历下一位。

最新文章

  1. C#学习
  2. RTMP流媒体播放过程
  3. android去掉滑动到顶部和底部的阴影
  4. git 证书错误
  5. Java中request请求之 - 带文件上传的form表单
  6. http请求的referer属性
  7. Extjs4.2纯前台导出Excel总结
  8. VC/MFC 下 递归遍历目录下的所有子目录及文件
  9. scrollview上面的图片不上下滑动
  10. CodeChef Little Elephant and Mouses [DP]
  11. UI设计师给的px尺寸单位,安卓如何换算成dp?
  12. mysql 备份数据语句
  13. ansible的安装部署及简单应用
  14. 注解图Annotation
  15. js区别手机和电脑打开网页
  16. Python六大开源框架对比:Web2py略胜一筹(转)
  17. linux、内核源码、内核编译与配置、内核模块开发、内核启动流程(转)
  18. python3----requests
  19. 第23课 #error和#line使用分析
  20. 【MySQL解惑笔记】忘记MySQL数据库密码

热门文章

  1. 团队作业php
  2. Could not load file or assembly &#39;Microsoft.Office.Interop.Word, Version=14.0
  3. MVC缓存技术
  4. shell中的比较语句
  5. winform 如何控制输入法
  6. Load hlsl
  7. Nodejs Express 4.X 中文API 4--- Router篇
  8. PE文件结构详解
  9. Goolg Chrome 插件开发--Hello world
  10. 将HTMLCollection/NodeList/伪数组转换成数组