算法竞赛入门经典第四章
素数判定`
int is_prime(
int n)
{
if(n
<=1) return 0;
int m
=floor(sqrt(n
)+0.5);
for(int i
=2;i
<=m
;i
++)
if(n
%i
==0) return 0;
return 1;
}
四舍五入避免了浮点误差。
C语言中的floor(),ceil(),round()
#include<math.h>
double ceil(x
);
double floor(x
);
double round(x
);
交换a,b的值
void swap(int* a
,int*b
)
{
int t
=*a
;
*a
=*b
;
*b
=t
;
}
int main()
{
int a
=3,b
=4;
swap(&a
,&b
);
printf("%d %d\n",a
,b
);
return 0;
}
快速排序
#include<stdlib.h>
void qsort(void* base
,size_t num
,size_t size
,
int(*comparator
)(const void*,const void*))
清空数组(多数据问题一定要清空数组)
#include<string.h>
memset(code
,0,sizeof(code
));
转载请注明原文地址:https://ipadbbs.8miu.com/read-10591.html