Skip to content

Instantly share code, notes, and snippets.

@dodola
Created May 31, 2013 10:03
Show Gist options
  • Save dodola/5684037 to your computer and use it in GitHub Desktop.
Save dodola/5684037 to your computer and use it in GitHub Desktop.
ACM 常用函数
#include <stdio.h>
int getchar( void ); //读取一个字符, 一般用来去掉无用字符
char *gets( char *str ); //读取一行字符串
#include <stdlib.h>
void * malloc( size_t size ); //动态内存分配, 开辟大小为 size 的空间
void qsort( void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) ); //快速排序
Sample:
int compare_ints( const void* a, const void* b )
{
int* arg1 = (int*) a; int* arg2 = (int*) b;
if( *arg1 < *arg2 ) return -1;
else if( *arg1 == *arg2 ) return 0;
else return 1;
}
int array[] = { -2, 99, 0, -743, 2, 3, 4 }; int array_size = 7;
qsort( array, array_size, sizeof(int), compare_ints );
#include <math.h>
//求反正弦, arg∈[-1, 1], 返回值∈[-pi/2, +pi/2]
double asin( double arg );
//求正弦, arg为弧度, 弧度=角度*Pi/180.0, 返回值∈[-1, 1]
double sin( double arg );
//求e的arg次方
double exp( double arg );
//求num的对数, 基数为e
double log( double num );
//求num的根
double sqrt( double num );
//求base的exp次方
double pow( double base, double exp );
#include <string.h>
//初始化内存, 常用来初始化数组
void* memset( void* buffer, int ch, size_t count );
memset( the_array, 0, sizeof(the_array) );
//printf是它的变形, 常用来将数据格式化为字符串
int sprintf( char *buffer, const char *format, ... );
sprintf(s, "%d%d", 123, 4567); //s="1234567"
//scanf是它的变形, 常用来从字符串中提取数据
int sscanf( const char *buffer, const char *format, ... );
Sample:
char result[100]="24 hello", str[100]; int num;
sprintf( result, "%d %s", num,str );//num=24;str="hello" ;
//字符串比较, 返回值<0代表str1<str2, =0代表str1=str2, >0代表str1>str2
int strcmp( const char *str1, const char *str2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment