C程序编程实例
1.快速排序算法
#include <stdio.h>
#define SIZE 10
void fastSort(int *a, int iLeft,int iRight)
{
int i = 0;
int j = 0;
int k = 0;
i = iLeft;
j = iRight;
k = a[iLeft];
if(i >= j)
{
return;
}
while(i < j)
{
while(i < j && k <= a[j])
{
--j;
}
a[i] = a[j];
while(i < j && k >= a[i])
{
++i;
}
a[j] = a[i];
}
a[i] = k;
fastSort(a,iLeft,i-1);
fastSort(a,i+1,iRight);
}
int main()
{
int i = 0;
int a[SIZE] = {6,2,3,7,8,9,4,11,5,12};
fastSort(a,0,9);
for(i=0;i<10;++i)
printf("%5d",a[i]);
printf("\n");
return 0;
}
2.判断一个整数是否为对称数,如12321
#include <stdio.h>
int main()
{
int num =12321;
int sum = 0;
int temp = 0;
int n = num;
while(n > 0)
{
temp = n % 10;
sum = sum * 10 + temp;
n /= 10;
}
if(sum == num)
printf("是对称整数.\n");
else
printf("不是对称整数.\n");
return 0;
}
3.判断一个数是否为对称数如1234.321不准用字条处理
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
int isSymmetryNum(double num)
{
int flag = 0;
char *cp;
char cpNum[SIZE] = {0};
char caNum[SIZE] = {0};
int i = 0;
int iLen = 0;
int count = 8;
int j = 0;
cp = gcvt(num,count,cpNum);
//for(i=0;i<8;++i)
// printf("%c",cp[i]);
//printf("\n");
//for(i=0;i<8;++i)
// printf("%c",cpNum[i]);
//printf("\n");
iLen = strlen(cp);
//printf("iLen = %d\n",iLen);
for(i=0;i<iLen;++i)
{
if( cp[i] != 46)
{
caNum[j] = cp[i];
j++;
}
}
j--;
for(i=0;i<j/2;++i)
{
if(caNum[i] != caNum[j-i])
{
flag = 0;
return flag;
}
if(i >= j/2)
{
flag = 1;
break;
}
}
flag = 1;
//puts(cp);
return flag;
}
int main()
{
double num = 1234.321;
int i = 0;
i = isSymmetryNum(num);
if(1 == i)
printf("it is a symmetry num.\n");
else
printf("it is not a symmetry num.\n");
return 0;
}