@在已知输入数据规模的前提下,定义数组的时,数组的大小一定要严格且略大于该规模!
@C语言中qsort函数的用法。头文件:#include <stdlib.h>。
#include <stdio.h>
#include <stdlib.h>
int comp_inc(const void *first, const void *second);//控制qsort函数,使其成为单调不减函数。
int comp_dec(const void *first, const void *second);//控制qsort函数,使其成为单调不增函数。
int main()
{
int test_array_1[5]={99, 123, 0, -12, 99};
int test_array_2[8]={-100, 19, -123, 66, 3, 978, 66, 0};
qsort(test_array_1, 5, sizeof(test_array_1[0]), comp_inc);
qsort(test_array_2, 8, sizeof(test_array_2[0]), comp_dec);
int i;
for(i=0; i<5; ++i)
{
printf("%d", test_array_1[i]);
if(i!=4)
printf(" ");
else
printf(".");
}
printf("\n");
for(i=0; i<8; ++i)
{
printf("%d", test_array_2[i]);
if(i!=7)
printf(" ");
else
printf(".");
}
printf("\n");
return 0;
}
int comp_inc(const void *first, const void *second)
{
return *(int *)first-*(int *)second;
}
int comp_dec(const void *first, const void *second)
{
return *(int *)second-*(int *)first;
}
@质数定义:在大于1的自然数中,除了1和其本身之外不再有其他因数。
@在C语言中实现输出大小写字母转换有两种
str[i]+=32; or str[i]-=32;//or前面的语句为大写字母转换成小写字母;or后面的语句小写字母转换成大写字母。
printf("%c", str[i]);
--------------------------
printf("%c", str-32); or printf("%c", str+32);//or前面的语句为将小写字母转换成大写字母并输出;or后面的语句为将大写字母转换成小写字母并输出。
@C语言中long long类型的数据读入或输入用%lld 或者 %I64d。Codeforces上只允许使用%I64d。
@自然数中奇数求和公式:1+3+5+...+(2n-1)=n*n。
@中位数:对于有限的数集,可以通过把所有观察值按高低排序后找出正中间的一个作为中位数;如果观察值是偶数个,通常取最中间的两个数值的平均数作为中位数。
【注】中位数和众数(众数指一组数据中出现次数最多的数值)不同,众数有时不止一个,而中位数只能有一个!
@C语言中,向上取整函数为floor()、向下取整函数为ceil(),它们的头文件为#include<math.h>。不过还可以通过其他方式实现向上取整和向下取整,详见代码。
#include <stdio.h>
#include <math.h>
int main()
{
float test=9.9;
int a;
int b;
a=floor(test);
b=ceil(test);
printf("a=%d\n", a);
printf("b=%d\n", b);
/*如果所给的数据是两个n和m,求n/m的向上取整结果和向下取整结果,可以用下面的方法,
不过使用上面的函数形式,不仅可以对n/m进行取整,还可以对一个给定的浮点数数据
进行取整,这一点是下面方法无法实现的!*/
int c;
int d;
int n=5;
int m=2;
c=n/m;//对n/m向下取整
d=((n-1)/m)+1;//对n/m向上取整,如果n为数组下标且数组下标是从0开始的,那么(n-1)应该改为(n-2),因为给定n个数,在从0开始的数组中,n-1才是这组数组最后一个元素所在位置!
printf("c=%d\n", c);
printf("d=%d\n", d);
float n_1=5.0;
float m_1=2.0;
//printf("n_1/m_1=%f\n", n_1/m_1);
int a_1=floor(n_1/m_1);
int b_1=ceil(n_1/m_1);
printf("a_1=%d\n", a_1);
printf("b_1=%d\n", b_1);
return 0;
}
@puts();函数只能用于输出字符串,没有控制格式。默认输出后换行。等价于printf("%s\n", s);