# 映射算法_反排序算法_自创

—————————2018-04-17更新————————

#### 最下面有更新

—————————以下原博客————————

14 7 8
5 57 32

7 8 14
5 32 57

14 映射 57
7 映射 5
8 映射 32

57 5 32

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 7;

void Function(int *a, int*b)
{
int c[maxn]; //备份
for(int i = 0; i < maxn; ++i) {
c[i] = a[i];
}
sort(a, a + maxn);
sort(b, b + maxn);
for(int i = 0; i < maxn; ++i) {
for(int j = 0; j < maxn; ++j) {
if(c[i] == a[j]) cout << b[j] << ' ';
}
}
}

int main()
{
int a[maxn] = {4, 34, 7, 3, 1, 23, -3};  //无重复
int b[maxn] = {334, 4, -1, 34, -45, 456, 71};

//想得到一个34, 456, 71, 4, -1, 334, -45序列
Function(a, b);
}

#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 7, MAX = 1000;
int a[maxn] = {4, 34, 7, 3, 1, 23, -3};  //无重复
int b[maxn] = {334, 4, -1, 34, -45, 456, 71};

bool cmp_a(int x, int y)
{
return a[x] < a[y];
}

bool cmp_b(int x, int y)
{
return b[x] < b[y];
}

void Function(int *a, int *b, int *a_a, int *b_b)
{
int a_map[MAX] = {};
sort(a_a, a_a + maxn, cmp_a);
sort(b_b, b_b + maxn, cmp_b);
for(int i = 0; i < maxn; ++i) {
a_map[a[a_a[i]]] = i;
}
for(int i = 0; i < maxn; ++i) {
cout << b[b_b[a_map[a[i]]]] << ' ';
}
}

int main()
{
int a_a[maxn], b_b[maxn];
for(int i = 0; i < maxn; ++i) {
a_a[b_b[i] = i] = i;
}

//想得到一个34, 456, 71, 4, -1, 334, -45序列
Function(a, b, a_a, b_b);
}

—————————以下2018-04-17更新————————

#### 第一种

#include <iostream>
#include <algorithm>
using namespace std;

int a[5] = {12, 34, 4, 33, 7};
int b[5] = {3, 44, 90, 4, 38};
int a_a[5], aa[5];

bool cmp(int x, int y)
{
return a[x] < a[y];
}

bool cmp_2(int x, int y)
{
return aa[x] < aa[y];
}

int main()
{
for(int i = 0; i < 5; ++i) {
a_a[aa[i] = i] = i;
}
sort(aa, aa + 5, cmp);
sort(b, b + 5);
sort(a_a, a_a + 5, cmp_2);

cout << "原来的a序列:\n";
for(int i = 0; i < 5; ++i) {
cout << a[i] << ' ';
}
cout << endl;
cout << "排序后的b序列:\n";
for(int i = 0; i < 5; ++i) {
cout << b[i] << ' ';
}
cout << endl << endl;
for(int i = 0; i < 5; ++i) {
cout << a[i] << " rank is " << a_a[i] << endl;
}
cout << endl;
for(int i = 0; i < 5; ++i) {
cout << a[i] << " 映射: " << b[a_a[i]] << endl;
}
}

#### 第二种

1. 对0-n的所有数组成的乱序列进行排列, 这算是特殊情况了.
2. 类似下标排序的, 下标的排序意味着顺序, 而非简单的大小, 或许从这种思路上出发, 可能会解决一些意想不到的问题. 但我还没有遇到过, 所以姑且做做猜想.

#include <iostream>
using namespace std;

const int Range = 0xfffff;
int rank[Range] = {};
int rankb[Range] = {};

int main()
{
int a[5] = {12, 34, 4, 33, 7};
int b[5] = {3, 44, 90, 4, 38};
for(int i = 0; i < 5; ++i) {
rank[a[i]] = 1;
}
int ran = 0;
for(int i = 0; i < Range; ++i) {
if(rank[i]) rank[i] = ran++;
}
ran = 0;
for(int i = 0; i < 5; ++i) {
rankb[b[i]] = 1;
}
for(int i = 0; i < Range; ++i) {
if(rankb[i]) b[ran++] = i;
}
cout << endl;
cout << "b排序后的序列:\n";
for(int i = 0; i < 5; ++i) {
cout << b[i] << ' ';
}
cout << endl;
cout << "\na的每个元素的排名\n";
for(int i = 0; i < 5; ++i) {
cout << a[i] << ' ' << rank[a[i]] << endl;
}
for(int i = 0; i < 5; ++i) {
cout << a[i] << " 映射 " << b[rank[a[i]]] << '\n';
}
cout << endl;
}

—————————以上————————

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120