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

给出一个整数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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值