問題:
1. 隨機生成一個隨機數
2. 隨機生成十個不同的隨機數
3. 將以上生成的隨機數,隨機放在一個數組中20個不同的位置
4. 找到這十個隨機數在數組中最鄰近的空位置
5. 將十個隨機數分別移動到最鄰近的空位置
PS:在生成十個不同的隨機數時,判斷隨機數重複與否的方法,我的這個效率並不高。
歡迎各位批評指正^_^
代碼:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM 10
#define COUNT 20
#define MAX 1000//生成的隨機數均在0-(MAX-1)之間
int array[NUM]; //存放十個不同的隨機數 的數組
int location[NUM]; //存放十個不同隨機數的不同的隨機位置(0-COUNT) 的數組
int space[COUNT]; //在space數組中隨機找到10個位置,來存放10個不同的隨機數
void printArray(int array[], int num) {//打印數組前num個元素
for (int i = 0; i < num; i++)
printf("%d\t", array[i]);
}
void initialArray(int array[], int num1, int num2) {//把數組的前num1個元素初始化爲num2
for (int i = 0; i < num1; i++)
array[i] = num2;
}
void copyArray(int location[], int lable[], int num) {//把location數組的前num個元素複製到lable中
for (int i = 0; i < num; i++)
lable[i] = location[i];
}
int randomNumber(int max) {//生成0--(max-1)的隨機數;;一般來講,隨機數生成範圍是0-2^15-1
srand((unsigned)time(NULL));
return rand() % max;
}
bool findSame(int array[], int a) {//使數組array中沒有與array[a]相等的數;如果有相同的就再生成隨機數,直到沒有
for (int i = 0; i < a; i++)
if (array[i] == array[a]) {
return false;
}
return true;
}
void creatRanArray(int array[], int num, int max) {//產生NUM個不同的隨機數,放入array[]中,最大值爲max-1,
for (int i = 0; i < num; i++) {
array[i] = randomNumber(max);
bool b = !findSame(array, i);
while (b) {//有重複 ,,沒有的話,就不執行
array[i] = randomNumber(max);//再隨機生成一個
b = !findSame(array, i);//檢測,,,,直到沒有重複
}
}
}
void ranSpace(int space[], int count, int num1, int num2) {//space空間大小爲count,生成num1個不同的隨機數(最大爲MAX-1),隨機放入space[COUNT]中num2個不同的位置
// initialArray(space, count,-1);//把space數組每個位置初始爲-1,用以表示此位置爲空。
// creatRanArray(array,num1,MAX);//array數組,總數NUM,最大值MAX-1
creatRanArray(location, num2, count);//location數組中的值最大爲19,總個數爲NUM
for (int i = 0; i < num1; i++)
space[location[i]] = array[i];
}
void nearLocation(int location[]) {//找出每個隨機數最鄰近的空位置
//initialArray(space, -1);//把space數組每個位置初始爲-1,用以表示此位置爲空。
//ranSpace(space, COUNT);
int memory[NUM];//記憶每個隨機數最鄰近的空位置
int step[NUM];//每個元素向左或右離最鄰近的位置的距離
initialArray(step, NUM, 21);//將每個元素離最鄰近位置的距離初始爲21,因爲不可能有個元素最鄰近的空位置 爲21
initialArray(memory, NUM, -1); //將每個元素最鄰近位置初始爲-1
for (int i = 0; i < NUM; i++) {
int temp = location[i];//第一個元素的位置
//if((temp == 0 && space[location[i] + 1] != -1) || (temp == 19 && space[location[i] - 1] != -1))
//因爲找最近,當其爲邊界(such as 0)時,左邊沒有還有右邊
//優先左側空位置
for (temp--; temp >= 0 && space[temp] != -1; temp--);//第i個元素由其位置location[i]開始,向左尋找其最鄰近位置,找到就保保存
if (temp >= 0) {//得考慮,不然第0個元素就會出錯
step[i] = location[i] - temp;
memory[i] = temp;
}
if (step[i] > 1)//如果距離爲1,就已經找到最近的位置了,沒必要繼續向右尋找
{
temp = location[i];
for (temp++; temp < COUNT && space[temp] != -1; temp++);
if (temp < COUNT && temp - location[i] < step[i])//右邊最近距離比左邊小,就用右邊的;
memory[i] = temp;
}
}
for (int i = 0; i < NUM; i++)
printf("元素:%d,位置:%d,最鄰近的空位置:%d\n", space[location[i]], location[i], memory[i]);
//printArray(memory,NUM);
}
void paixu(int a[], int num) {//從小到大
int i, j, temp;
for (i = 0; i < num - 1; i++)
{
for (j = i + 1; j < num; j++)
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
int findSub(int antistop, int location[], int num) {//在location數組前num個元素中尋找antistop出現的位置,找到返回下標,否則返回-1
for (int i = 0; i < num; i++) {
if (antistop == location[i])
return i;
}
return -1;
}
void moveLocation(int location[]) {//找出每個隨機數最鄰近的空位置
int memory[NUM];//記憶每個隨機數最鄰近的空位置
int step[NUM];//每個元素向左或右離最鄰近的位置的距離
initialArray(step, NUM, 21);//將每個元素離最鄰近位置的距離初始爲21,因爲不可能有個元素最鄰近的空位置 爲21
initialArray(memory, NUM, -1); //將每個元素最鄰近位置初始爲-1
paixu(location, NUM);
puts("對location數組排序之後");
printArray(location, NUM);
for (int i = 0; i < NUM; i++) {
int temp = location[i];//第一個元素的位置
//優先左側空位置
for (temp--; temp >= 0 && space[temp] != -1; temp--);//第i個元素由其位置location[i]開始,向左尋找其最鄰近位置,找到就保保存
if (temp >= 0) {//得考慮,不然第0個元素就會出錯
step[i] = location[i] - temp;
memory[i] = temp;
}
if (step[i] > 1)//如果距離爲1,就已經找到最近的位置了,沒必要繼續向右尋找
{
temp = location[i];
for (temp++; temp < COUNT && space[temp] != -1; temp++);
if (temp < COUNT && temp - location[i] < step[i])//右邊最近距離比左邊小,就用右邊的;
memory[i] = temp;
}
space[memory[i]] = space[location[i]];//原來數字移到新位置
space[location[i]] = -1; //原位置 置空
}
puts("移動之後:");
printArray(space, COUNT);
puts("");
puts("");
}int main()
{
puts("生成一個隨機數:");
printf("%d\n", randomNumber(MAX));
puts("生成NUM個不同的數放入數組中,該數組爲:");
creatRanArray(array, NUM, MAX);
printArray(array, NUM);
puts("initialArray函數:初始爲-1");
initialArray(space, COUNT, -1);
printArray(space, COUNT);
ranSpace(space, COUNT, NUM, NUM);
puts("location的NUM個位置爲:");
printArray(location, NUM);
puts("生成NUM個不同的數放入一個擁有COUNT個位數的數組中,-1表示該位爲空,該數組爲:");
printArray(space, COUNT);
puts("每個元素最鄰近的空位置分別爲(左邊空位置優先):");
nearLocation(location);
moveLocation(location);
return 0;
}
第四問思路:根據location數組裏存放的位置,從location[1]到最後一個遍歷,先找左面的空位置,找到了就把距離記在step[i]中,位置記在memory[i]中,然後判斷下距離是否大於1,如果等於於1就不用找了,因爲左邊緊挨着空位置;然後往右找,找到空位置後判斷距離和原來的大小關係,如果大的話就用原來的,小的話就用右邊的。然後最臨近的空位置就找到了。
第五問思路:在上面找到一個空位置就馬上移動過去,然後下一次接着找下一個位置的空位置再移動