user-0 提问: 题虽小，不简单，遍历指定库阶乘输出。大家做做看 ^ _ ^

回复:
user: user-1
content: #include <iostream> using namespace std; int main() { 	char a[9]={'a','b','c','1','2','3','!','@','#'}; 	int stack[9]; 	int n,top; 	cin>>n; 	for(int len=1;len<=n;len++) 	{ 		top=0; 		do 		{ 			if(top<len) 				stack[top++]=0; 			else 				stack[top-1]++; 			while(stack[top-1]>=9) 				if(--top>0) stack[top-1]++; 			if(top==len) 			{ 				for(int i=0;i<len;i++) 					printf("%c",a[stack[i]]); 				printf(","); 			} 		}while(top>0); 		printf("\n"); 	} 	system("pause"); }

user: user-1
content: #include <iostream> using namespace std; int main() { 	char a[9]={'a','b','c','1','2','3','!','@','#'}; 	int stack[9]; 	int n,top; 	cin>>n; 	for(int len=1;len<=n;len++) 	{ 		top=0; 		do 		{ 			if(top<len) 				stack[top++]=0; 			else 				stack[top-1]++; 			while(stack[top-1]>=9) 				if(--top>0) stack[top-1]++; 			if(top==len) 			{ 				for(int i=0;i<len;i++) 					printf("%c",a[stack[i]]); 				printf(","); 			} 		}while(top>0); 		printf("\n"); 	} 	system("pause"); }

user: user-2
content: 请简单考虑一下，下面的代码输出什么？ for (int i = 0; i < 100; i++)     printf("%02d\n", i); 是的，它输出10的2次方个数字，这些数字是10进制表示，输出的数字的宽度是2 有没有注意到这些带颜色的数字？它就是我的解决方法的关键。  LZ其实在输出固定宽度的9进制数，只不过不是用012345678来表示digit，而是用abc123!@#来表示digit。  # include <stdio.h> # include <string.h> # include <math.h>  void reverse(char * string) {     int l = strlen(string);     int i;     char tmp;     for (i = 0; i < l / 2; i++)     {         tmp = string[i];         string[i] = string[l - 1 - i];         string[l - 1 - i] = tmp;     } }  char * format_to_base(int n, int base, int width, char a[]) {     static char buffer[100] = "";     int i = 0;     int q, r;     memset(buffer, 0, 100);     do {         q = n / base;         r = n % base;         buffer[i++] = a[r];         n = q;     } while (q != 0);     while (i < width)         buffer[i++] = a[0];     reverse(buffer);     return buffer; }  void output_line(int n) {     char a[] = "abc123!@#";     int max = pow(9, n);     int width = n;     int i;     for (i = 0; i < max; i++)     {         printf("%s", format_to_base(i, 9, width, a));         if (i != max - 1)             putchar(',');     }     putchar('\n'); }  int main() {     int n;     int i;     printf("input n (0 < n < 9): ");     scanf("%d", &n);     for (i = 1; i <= n; i++)         output_line(i);      return 0; }

user: user-0
content: 打算到周五结贴，还有新思路的朋友表吝啬呀，哈哈

user: user-3
content: 引用 17 楼 zhao4zhong1 的回复:15楼似乎借鉴的不对啊。嗯，貌似有些偏

user: user-4
content: 引用 19 楼 zxh707wk 的回复:该算法非递归输出 #include <stdio.h> #include <string.h> #include <malloc.h>  #define str "abc123!@#"  void putstr(int num[],int len) { 	int i; 	for(i=0;i<len;i++) 	{ 		printf("%c",str[num[i]]); 	} }  void fun(int n) { 	int i; 	int len=strlen(str); 	int* num=(int*)malloc(sizeof(int)*n); 	for(i=0;i<n;i++) 	{ 		num[i]=0; 	} 	for(;num[0]<len;) 	{ 		putstr(num,n); 		num[n-1]++; 		for(i=n-1;i>0;i--) 		{ 			if(num[i]>=len) 			{ 				num[i-1]++; 				num[i]=0; 			} 		} 		if(num[0]<len)printf(","); 	} }  int main() { 	int n; 	scanf("%d",&n); 	fun(n); 	return 0; }  http://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and

user: user-5
content: 该算法非递归输出 #include <stdio.h> #include <string.h> #include <malloc.h>  #define str "abc123!@#"  void putstr(int num[],int len) { 	int i; 	for(i=0;i<len;i++) 	{ 		printf("%c",str[num[i]]); 	} }  void fun(int n) { 	int i; 	int len=strlen(str); 	int* num=(int*)malloc(sizeof(int)*n); 	for(i=0;i<n;i++) 	{ 		num[i]=0; 	} 	for(;num[0]<len;) 	{ 		putstr(num,n); 		num[n-1]++; 		for(i=n-1;i>0;i--) 		{ 			if(num[i]>=len) 			{ 				num[i-1]++; 				num[i]=0; 			} 		} 		if(num[0]<len)printf(","); 	} }  int main() { 	int n; 	scanf("%d",&n); 	fun(n); 	return 0; }

user: user-6
content: 理解成一个M进制N位的全部表达。然后不断的++，再打印。

user: user-4
content: 15楼似乎借鉴的不对啊。

user: user-0
content: 赵老师出马，不仅是一个顶两

user: user-0
content: 学到的东西越来越多

user: user-4
content: 链接给出了将任何递归程序改写为非递归实现的方法和步骤。

user: user-5
content: 引用 20 楼 zhao4zhong1 的回复:[quote=引用 19 楼 zxh707wk 的回复:] 该算法非递归输出 #include <stdio.h> #include <string.h> #include <malloc.h>  #define str "abc123!@#"  void putstr(int num[],int len) { 	int i; 	for(i=0;i<len;i++) 	{ 		printf("%c",str[num[i]]); 	} }  void fun(int n) { 	int i; 	int len=strlen(str); 	int* num=(int*)malloc(sizeof(int)*n); 	for(i=0;i<n;i++) 	{ 		num[i]=0; 	} 	for(;num[0]<len;) 	{ 		putstr(num,n); 		num[n-1]++; 		for(i=n-1;i>0;i--) 		{ 			if(num[i]>=len) 			{ 				num[i-1]++; 				num[i]=0; 			} 		} 		if(num[0]<len)printf(","); 	} }  int main() { 	int n; 	scanf("%d",&n); 	fun(n); 	return 0; }  http://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and [/quote] 链接页面的内容看不懂。。。

user: user-4
content: “给定一个小点的输入，完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门！ 递归函数关注以下几个因素 ·退出条件 ·参数有哪些 ·返回值是什么 ·局部变量有哪些 ·全局变量有哪些 ·何时输出 ·会不会导致堆栈溢出

user: user-4
content: 难者不会，会者不难。 赵4出品，必属精品！  //给定一个数组a[9]={'a','b','c','1','2','3','!','@',"#"}， //要求程序启动后提示输入整数n(0<n<9),根据输入整数进行输出， //输出规则为：从一行一字符到n行n字符遍历数组中所有字符，比如： //n=1,则输出： //a,b,c,1,2,3,!,@,# //n=2.则如下输出： //a,b,c,1,2,3,!,@,# //aa,ab,ac,a1,a2,a3,a!,a@,a#,ba,bb,bc,b1,b2,b3,b!,b@,b#,ca,cb,cc,c1,c2,c3,c!,c@,c#,1a,1b,1c,11,12,13,1!,1@,1#,2a,2b,2c,21,22,23,2!,2@,2#,3a,3b,3c,31,32,33,3!,3@,3#,!a,!b,!c,!1,!2,!3,!!,!@,!#,@a,@b,@c,@1,@2,@3,@!,@@,@#,a,#b,#c,#1,#2,#3,#!,#@,## //…… #include <stdio.h> char a[9]={'a','b','c','1','2','3','!','@','#'}; char t[9]; void out(int m,int L) {     int i;     if (m==L) {         for (i=0;i<m;i++) printf("%c",t[i]);         for (i=0;i<m;i++) if ('#'!=t[i]) break;         if (i<m) printf(",");         return;     }     for (i=0;i<9;i++) {         t[L]=a[i];         out(m,L+1);     } } void out1_n(int n) {     int m;      for (m=1;m<=n;m++) {         out(m,0);         printf("\n");     } } int main() {     int n;      while (1) {         printf("Input n(0<n<9):");         fflush(stdout);         rewind(stdin);         if (1==scanf("%d",&n)) {             if (0<n && n<9) break;         }     }     out1_n(n);     return 0; } //Input n(0<n<9):1 //a,b,c,1,2,3,!,@,# // //Input n(0<n<9):2 //a,b,c,1,2,3,!,@,# //aa,ab,ac,a1,a2,a3,a!,a@,a#,ba,bb,bc,b1,b2,b3,b!,b@,b#,ca,cb,cc,c1,c2,c3,c!,c@,c#,1a,1b,1c,11,12,13,1!,1@,1#,2a,2b,2c,21,22,23,2!,2@,2#,3a,3b,3c,31,32,33,3!,3@,3#,!a,!b,!c,!1,!2,!3,!!,!@,!#,@a,@b,@c,@1,@2,@3,@!,@@,@#,#a,#b,#c,#1,#2,#3,#!,#@,## // //Input n(0<n<9):3 //a,b,c,1,2,3,!,@,# //aa,ab,ac,a1,a2,a3,a!,a@,a#,ba,bb,bc,b1,b2,b3,b!,b@,b#,ca,cb,cc,c1,c2,c3,c!,c@,c#,1a,1b,1c,11,12,13,1!,1@,1#,2a,2b,2c,21,22,23,2!,2@,2#,3a,3b,3c,31,32,33,3!,3@,3#,!a,!b,!c,!1,!2,!3,!!,!@,!#,@a,@b,@c,@1,@2,@3,@!,@@,@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1ca,1cb,1cc,1c1,1c2,1c3,1c!,1c@,1c#,11a,11b,11c,111,112,113,11!,11@,11#,12a,12b,12c,121,122,123,12!,12@,12#,13a,13b,13c,131,132,133,13!,13@,13#,1!a,1!b,1!c,1!1,1!2,1!3,1!!,1!@,1!#,1@a,1@b,1@c,1@1,1@2,1@3,1@!,1@@,1@#,1#a,1#b,1#c,1#1,1#2,1#3,1#!,1#@,1##,2aa,2ab,2ac,2a1,2a2,2a3,2a!,2a@,2a#,2ba,2bb,2bc,2b1,2b2,2b3,2b!,2b@,2b#,2ca,2cb,2cc,2c1,2c2,2c3,2c!,2c@,2c#,21a,21b,21c,211,212,213,21!,21@,21#,22a,22b,22c,221,222,223,22!,22@,22#,23a,23b,23c,231,232,233,23!,23@,23#,2!a,2!b,2!c,2!1,2!2,2!3,2!!,2!@,2!#,2@a,2@b,2@c,2@1,2@2,2@3,2@!,2@@,2@#,2#a,2#b,2#c,2#1,2#2,2#3,2#!,2#@,2##,3aa,3ab,3ac,3a1,3a2,3a3,3a!,3a@,3a#,3ba,3bb,3bc,3b1,3b2,3b3,3b!,3b@,3b#,3ca,3cb,3cc,3c1,3c2,3c3,3c!,3c@,3c#,31a,31b,31c,311,312,313,31!,31@,31#,32a,32b,32c,321,322,323,32!,32@,32#,33a,33b,33c,331,332,333,33!,33@,33#,3!a,3!b,3!c,3!1,3!2,3!3,3!!,3!@,3!#,3@a,3@b,3@c,3@1,3@2,3@3,3@!,3@@,3@#,3#a,3#b,3#c,3#1,3#2,3#3,3#!,3#@,3##,!aa,!ab,!ac,!a1,!a2,!a3,!a!,!a@,!a#,!ba,!bb,!bc,!b1,!b2,!b3,!b!,!b@,!b#,!ca,!cb,!cc,!c1,!c2,!c3,!c!,!c@,!c#,!1a,!1b,!1c,!11,!12,!13,!1!,!1@,!1#,!2a,!2b,!2c,!21,!22,!23,!2!,!2@,!2#,!3a,!3b,!3c,!31,!32,!33,!3!,!3@,!3#,!!a,!!b,!!c,!!1,!!2,!!3,!!!,!!@,!!#,!@a,!@b,!@c,!@1,!@2,!@3,!@!,!@@,!@#,!#a,!#b,!#c,!#1,!#2,!#3,!#!,!#@,!##,@aa,@ab,@ac,@a1,@a2,@a3,@a!,@a@,@a#,@ba,@bb,@bc,@b1,@b2,@b3,@b!,@b@,@b#,@ca,@cb,@cc,@c1,@c2,@c3,@c!,@c@,@c#,@1a,@1b,@1c,@11,@12,@13,@1!,@1@,@1#,@2a,@2b,@2c,@21,@22,@23,@2!,@2@,@2#,@3a,@3b,@3c,@31,@32,@33,@3!,@3@,@3#,@!a,@!b,@!c,@!1,@!2,@!3,@!!,@!@,@!#,@@a,@@b,@@c,@@1,@@2,@@3,@@!,@@@,@@#,@#a,@#b,@#c,@#1,@#2,@#3,@#!,@#@,@##,#aa,#ab,#ac,#a1,#a2,#a3,#a!,#a@,#a#,#ba,#bb,#bc,#b1,#b2,#b3,#b!,#b@,#b#,#ca,#cb,#cc,#c1,#c2,#c3,#c!,#c@,#c#,#1a,#1b,#1c,#11,#12,#13,#1!,#1@,#1#,#2a,#2b,#2c,#21,#22,#23,#2!,#2@,#2#,#3a,#3b,#3c,#31,#32,#33,#3!,#3@,#3#,#!a,#!b,#!c,#!1,#!2,#!3,#!!,#!@,#!#,#@a,#@b,#@c,#@1,#@2,#@3,#@!,#@@,#@#,##a,##b,##c,##1,##2,##3,##!,##@,### //

user: user-3
content: 嗯，借鉴赵老师处理逗号的思路，修改了下。 #include<iostream> #include<string> #include<fstream> using namespace std;  char a[9] = { 'a', 'b', 'c', '1', '2', '3', '!', '@', '#' }; char c[20];  void Circle(ostream& os, int times, int size){ 	if (times == 0){ 		for (int i = 0; i < size; ++i) 			os << c[i]; 		for (int i = 0; i < size; ++i){ 			if (c[i] != '#'){ 				os << ','; 				break; 			} 		} 		return; 	}  	for (int i = 0; i < 9; ++i){ 		c[size] = a[i]; 		Circle(os, times - 1, size + 1); 	} }  void Print(ostream& os, int times){ 	for (int i = 0; i < times; ++i){ 		Circle(os, i + 1, 0); 		os << endl; 	} }  int main(){ 	int times; 	cin >> times; 	/*ofstream out("Test.txt"); 	Print(out, times);*/ 	Print(cout, times); }

user: user-3
content: 如下改成文件流形式可以以文件来检查输出： #include<iostream> #include<string> #include<fstream> using namespace std;  char a[9] = { 'a', 'b', 'c', '1', '2', '3', '!', '@', '#' }; char temp[20];  void Circle(ostream& os,int times, char* c,int size){ 	if (times == 0){ 		for (int i = 0; i < size;++i) 			os << c[i] ; 		os << ','; 		return; 	}  	for (int i = 0; i < 9; ++i){ 		c[size] = a[i]; 		Circle(os,times - 1, c, size + 1); 	} }  void Print(ostream& os,int times){ 	for (int i = 0; i < times; ++i){ 		Circle(os, i + 1, temp, 0); 		os << endl; 	} }  int main(){ 	int times; 	cin >> times; 	ofstream out("Test.txt"); 	Print(out,times); } 嗯，n=4的时候没问题！

user: user-7
content: 犹豫数据不多，最笨的办法就是7楼的列举法，然后就是8楼的递归与循环了，坐等更牛的算法大神！

user: user-3
content: 貌似输入4的时候，控制台前面一部分就被覆盖掉了。 #include<iostream> using namespace std;  char a[9] = { 'a', 'b', 'c', '1', '2', '3', '!', '@', '#' }; char temp[20];  void Circle(int times, char* c,int size){ 	if (times == 0){ 		for (int i = 0; i < size;++i) 			cout << c[i] ; 		cout << ','; 		return; 	}  	for (int i = 0; i < 9; ++i){ 		c[size] = a[i]; 		Circle(times - 1, c, size + 1); 	} }  void Print(int times){ 	for (int i = 0; i < times; ++i){ 		Circle(i + 1, temp, 0); 		cout << endl; 	} }  int main(){ 	int times; 	cin >> times; 	Print(times); }

user: user-3
content: 引用 4 楼 polyard 的回复:[quote=引用 3 楼 Falleyes 的回复:] 最后一个逗号没有处理。#include<iostream> #include<string> using namespace std;  char a[9] = { 'a', 'b', 'c', '1', '2', '3', '!', '@', '#' };  void Print(int times,char c){ 	if (times == 1){ 		for (int i = 0; i < 9; ++i){ 			if (c) 				cout << c; 			cout << a[i] << ','; 		} 		return; 	}  	for (int i = 0; i < 9; ++i){ 		if (c) 			cout << c; 		Print(times - 1, a[i]); 	} }  int main(){ 	int times; 	cin >> times; 	Print(times,NULL); 	cout << endl; }     你题理解了一点，但就是关键的没看。你的程序是当times=2时，输出了相当于我题目中n=2输出中的下半部分，它还需要输出n=1的情况，也就是n=max时，输出要是max!的所有输出，而且是按照n=1,n=2...的顺序输出的。另外，你的程序当times=3时输出时错误的哦，加油，真没那么简单^_^[/quote]嗯，确实如此。按照n=1,n=2... 的顺序输出好改。times=3时出错了，看样子还要研究研究！