首页 > 代码库 > C语言学习_test2
C语言学习_test2
1.互满数
#include <stdio.h>
#include<math.h>
int fun(int n);
int main(void)
{
int x, y;
for(x = 1; x< 3000; x++)
{
for(y = 1; y < x; y++)
{
if(fun(x) == y && fun(y) == x)
printf("%d %d\t", x, y);
}
}
return 0;
}
int fun(int n)
{
int i, sum = 0;
for(i = 1; i <= sqrt(n); i++)
{
if(n % i == 0)
sum = sum + i;
}
return sum;
}
2.快速排序
#include <stdio.h>
#include<time.h>
#include<stdlib.h>
#define N 1000
void quick_sort(int a[N+1], int left, int right)
{
int i, j, temp;
if(left < right)
{
temp = a[left];
i = left;
j = right;
while (i != j)
{
while(a[j] <= temp && j > i)
j--;
if(j > i)
a[i++] = a[j];
while(a[i] >temp && j > i)
i++;
if(j > i)
a[j--] = a[i];
}
a[i] = temp;
quick_sort(a, left, i-1);
quick_sort(a, i+1, right);
}
}
int main(void)
{
int i;
int a[N+1];
srand(time(NULL));
for(i=1;i<=N;i++)
a[i] = rand()%N+1;
printf("\n 按原序输出: \n");
for(i=1;i<=N;i++)
printf("%8d", a[i]);
system("pause");
quick_sort(a, 1, N);
printf("\n 按新序输出: \n");
for(i=1;i<=N; i++)
printf("%8d", a[i]);
printf("\n");
return 0;
}