整数字典序问题解答【原创】
2010-04-26 10:30
260 查看
给出一个整数n,求n以内的所有整数的排列,按字典序,而且要求给定一个序列,自动求出下一个序列。源码如下: #include<stdio.h> #include<stdlib.h> /* * @data 2001.04.21 * @author Fu Fengrui * */ /*user interface*/ void myGui(); /*use to process the innormal*/ void error(); /*exit*/ void proExit(); /*operate the file*/ void operateFile(); /*read from the screen*/ void operateRead(); /*compute the objctive array*/ int computeObj(int *obj,int *auxi,int n); /*compute the auxiliary array*/ int computeAuxi(int *obj,int *auxi,int n); /*increase the auxiliary array*/ int increaseAuxi(int *auxi, int n); /*initial the two array*/ int initObj(int *obj, int n); int initAuxi(int *auxi, int n); int main(){ int choice = 0;/*default read in the file*/ myGui(); scanf("%d",&choice); switch(choice){ case 1: { operateFile(); break; } case 2: { operateRead(); break; } case 3: { proExit(); break;} default: { error(); break; } } printf("press any key to continue!/n"); getchar(); getchar(); return 0; } /*my gui*/ void myGui(){ printf("*******************************/n"); printf("/n"); printf("**** Arrange Problem ******/n"); printf("/n"); printf("**** By Fu Fengrui******/n"); printf("/n"); printf("*******************************/n"); printf("/n"); printf("enter your choice:/n"); printf("1.output/input from the file/n"); printf("2.get the next array/n"); printf("3.exit/n"); } /*error func*/ void error(){ printf("error! please re-execute the program!/n"); } /*exit */ void proExit(){ printf("exit the program! Thank you!/n"); } /*opreate the file*/ void operateFile(){ /*printf("operate file/n");*/ int *obj = 0, *auxi = 0;/*ini*/ int n = 0; int i = 0; int index = 0; FILE *fin; FILE *fout; if((fin = fopen("./input.txt","r"))==NULL){ printf("can not open the file ./input.txt"); return; } if((fout = fopen("./output.txt","w")) == NULL){ printf("can not open the file ./output.txt"); return; }/*point to the file*/ n = fgetc(fin) - 48;/*read from the file and convert to int*/ printf("the number of n : %d/n",n); obj = (int *)malloc(n * sizeof(int)); auxi = (int *)malloc((n-1) * sizeof(int)); initObj( obj, n); initAuxi( auxi, n); do{ computeObj(obj, auxi, n); for(i = 0; i < n; i++){ fputc(obj[i] + 48, fout);/*convert the char to int and save in the file*/ fputc(',', fout); }/*how to put it to the file in a effciency way, i don't know*/ /*fwrite(obj, sizeof(int), n, fout);*/ fputc('/n',fout); initObj(obj, n);/*re initialize*/ index = increaseAuxi(auxi,n);/*identify*/ }while(index); printf("the result has been saved in the file named output.txt/n"); printf("please look up it/n"); fclose(fin); fclose(fout); } /*operate the next*/ void operateRead(){ /*printf("operate read/n");*/ int *obj, *auxi; int i = 0; int n = 0; char c = ' '; printf("input the number of n and press enter to confirm: /n"); scanf("%d", &n); if(n < 1 || n > 9){ printf("the value of n is in reccret /n"); return; } obj = (int *)malloc(n * sizeof(int)); auxi = (int *)malloc((n-1) * sizeof(int)); printf("input the arrays and split with blank: /n"); for(i = 0; i < n; i++){ scanf("%d", &obj[i]); getchar(); } printf("the input is over, now loading...... /n"); do{ initAuxi(auxi, n); computeAuxi(obj, auxi, n); increaseAuxi(auxi, n); computeObj(obj, auxi, n); printf("the next result is: /n"); for(i = 0; i < n - 1; i++){ printf("%d,", obj[i]); } printf("%d", obj[i]); printf("/n"); printf("press 'y' to continue or press any key to exit./n"); c = getchar(); getchar();/*to get the Enter key*/ }while( c == 'y'); } /*use to initialize */ int initObj(int *obj, int n){ int i = 0; for(i = 0; i < n; i++){ obj[i] = 0; } return 1; } int initAuxi(int *auxi, int n){ int i = 0; for(i = 0; i < n-1; i++){ auxi[i] = 0; } return 1; } /*use to compute the object array */ int computeObj(int *obj, int *auxi, int n){ int i = 0, j = 0; int x = 0; int last = 0; for(i = 0; i < n; i++){ last += (i+1); } for(i = 0; i < n-1; i++){ x = auxi[i] + 1; for(j = 0; j < i; j++){ if(obj[j] <= x) x++; } obj[i] = x; last = last -x; }/*except the last byte*/ obj[n-1] = last; return 1; } /*use to compute the auxiliry*/ int computeAuxi(int *obj, int *auxi, int n){ int i = 0, j = 0; int x = 0; for(i = 0; i < n; i++){ x = obj[i] - 1; for(j = 0; j < i; j++){ if(x > auxi[j]) x--; } auxi[i] = x; } return 1; } /*use to increase the auxiliry */ int increaseAuxi(int *auxi, int n){ int i = 0; for(i = n-2; i >= 0; i--){ if((auxi[i] + 1) > n - i -1){ auxi[i] = 0; continue; } else{ auxi[i] += 1; break; } } if(i < 0) return 0; return 1; } |
相关文章推荐
- 关于Installshield里一些常见问题的解答(二)--此篇非原创,只是集成了一些网友的解答
- [原创]关于《精通QT4编程》第一个例子无法编译的问题的解答
- [原创]关于《精通QT4编程》第一个例子无法编译的问题的解答
- 关于Installshield里一些常见问题的解答(二)--此篇非原创,只是集成了一些网友的解答
- 作译者常见问题解答(一):关于技术图书原创
- SQLSERVER应用问题解答13例(一)
- 【解答】关于内核中没开MMU之前的虚拟地址物理地址转换问题
- CString转成String和函数f(x)=x+4/x的问题解答
- 有点意思的C/C++问题及解答:21-25
- 网站建设中域名常见问题解答
- 您能看出这个Double Check里的问题吗?(解答)
- [原创]解决virtualbox及一些qt应用程序字体难看的问题
- 【原创】Windows Mobile上C#发送短信失败问题...Help!!!
- SEO中11个最常见的问题解答
- .Net线程问题解答
- 安装maven2 for eclipse,以及安装后的问题解答
- 原创:经验分享:微信小程序外包接单常见问题及流程
- Zbar识别汉字乱码问题,求大神解答
- PHP安装攻略:常见问题解答(一)
- 网闸——物理隔离网闸常见技术问题解答