|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void bogo_sort(int, int);
int array[9];
void bogo_sort(int a[], int length)
{
int temp, i, index;
srand(time(NULL));
for (i = 0; i < length; i++)
{
index = rand() % (length - i) + i;
if (index != i)
{
temp = a[i];
a[i] = a[index];
a[index] = temp;
}
if(i == length - 1)
{
if((a[0] == 67 && a[1] == 70) && (a[2] == 73 && a[3] == 101))
if((a[4] == 104 && a[5] == 105) && (a[6] == 108 && a[7] == 111))
break;
}
}
}
int main()
{
int array[] = {73, 108, 111, 118, 101, 70, 105, 104, 67};
int i, length;
time_t begin, end;
begin = time(NULL);
length = sizeof(array) / sizeof(array[0]);
bogo_sort(array, length);
printf("排序后的结果是:");
for (i = 0; i < length; i++)
{
printf("%d ", array[i]);
}
putchar('\n');
end = time(NULL);
printf("总共耗时:%ld秒\n", end - begin);
return 0;
}
这个代码在第9行报错:9 6 D:\FishC\S1E32\test0.c [Error] conflicting types for 'bogo_sort'
但是我不明白我已经声明函数bogo_sort是void类型的,为什么还会冲突呢?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//void bogo_sort(int, int);
void bogo_sort(int*, int);
int array[9];
void bogo_sort(int a[], int length)
{
int temp, i, index;
srand(time(NULL));
for (i = 0; i < length; i++)
{
index = rand() % (length - i) + i;
if (index != i)
{
temp = a[i];
a[i] = a[index];
a[index] = temp;
}
if(i == length - 1)
{
if((a[0] == 67 && a[1] == 70) && (a[2] == 73 && a[3] == 101))
if((a[4] == 104 && a[5] == 105) && (a[6] == 108 && a[7] == 111))
break;
}
}
}
int main()
{
int array[] = {73, 108, 111, 118, 101, 70, 105, 104, 67};
int i, length;
time_t begin, end;
begin = time(NULL);
length = sizeof(array) / sizeof(array[0]);
bogo_sort(array, length);
printf("排序后的结果是:");
for (i = 0; i < length; i++)
{
printf("%d ", array[i]);
}
putchar('\n');
end = time(NULL);
printf("总共耗时:%ld秒\n", end - begin);
return 0;
}
|
|