2019.6.24-2019.6.28 (training data structure) 3. Array coding solve the problem

2019.6.24-2019.6.28 (training data structure) books: "Data Structure Project Training Course" Zhao Jun Zhe Dai Wenhua

Table 5.5 Array and generalized expansion project training

An integer between elements array [0..N-1], is stored in the 0 ~ N-1 (1 <N <= 10), and A [i]: has an array A! = A [j], i! = J.

For example: When N = 6, there are A = (4,3,0,5,1,2). At this time, encoding of array A is defined as follows:

A [0] code: 0.

A [i] encoding wei: in a ratio of A [0], A [1], ..., A [i-1] A [i] is a small value number (i = 1,2, ... N-1).

Encoding the array A is B = (0,0,0,3,1,2).

Requirements are as follows:

To give the array A, using code A Solution C.

Given the coding array A, the original data A is obtained.

#include<iostream>
#include<cstdio>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;

int a[12],b[12];

void swap(int * x,int * y){
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void fun(int N){
    srand(time(NULL));
    int i;
    for(i=0; i<N; ++i)
        a[i]=i;
    for(i=N-1; i>=1; --i) 
        swap(&a[i], &a[rand()%i]);
    printf("产生的随机数组是:\n");
    for(i=0; i<N; ++i)
        printf("%d ",a[i]);
    cout<<endl;
}

int main(){
    int n,m;
    printf("请输入n:\n");
    cin>>n;
    fun(n);
    for(int i=0;i<n;i++){
        int count=0;
        for(int j=0;j<i;j++){
            if(a[j]<a[i]){
                count++;
            }
        }
        b[i]=count;
    }
    printf("数组的编码分别是:\n");
    for(int i=0;i<n;i++){
        cout<<b[i]<<" ";
    }
    
    //解码 
    int d[n],f[n];
    int flag=0;
    //printf("\n请输入数组的编码:\n");
    /*for(int i=0;i<n;i++){
        cin>>m;
        c[i]=m;
    }*/
    for(int i=0;i<n;i++){
        d[i]=i;
    }
    for(int i=n-1;i>=0;i--){
        int count=0;
        for(int j=0 ; J <n-; J ++ ) {
             IF (D [J] == - . 1 ) { 
                COUNT - ; 
            }     
            the else  IF (B [I] == COUNT) { 
                F [In Flag] = D [J]; 
                In Flag + + ; 
                D [J] = - . 1 ;
                 BREAK ; 
            } 
            COUNT ++ ; 
        } 
    } 
    the printf ( " \ n-requested array codec is: \ n- " );
     for ( int I = FLAG- . 1;i>=0;i--){
        cout<<f[i]<<" ";
    }
    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/zhying99/p/11083822.html