您的位置:首页 > 其它

整数字典序问题解答【原创】

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;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: