共通関数ライブラリ



一般的に使用される数学関数

#include <math.h>
fabs(double x) return double;取绝对值;
floor(double x) return double;向下取整;
ceil(double x) return double;向上取整;
pow(double r, double p) return double; 求幂;r的p次方
sqrt(double x) return double; 根号;
log(double x) return double;
sin(double x) cos(double x) tan(double x) return double;
asin(double x) acos(double x) atan(double x) return double;
round(double x) return double; 对x进行四舍五入;

スイッチの使い方

switch(x)
{
    
    
	case 1: 
		...
		break;
	case 2:
		...
		break;
	default:
		...
}

memset and fill、string.h関数、アルゴリズム関数

#include <string.h>
memset(arr, 0, sizeof(arr));
strlen(s); //字符串s
strcmp(a, b); //字符串a,b,
//if(a==b) return 0;
//if(a>b) return 正整数;
//if(a<b) return 负整数;
strcpy(a, b); //b复制给a;
strcat(a,b) //a后接b;
#include <stdio.h>
scanf("%d", &n); == scanf(screen, "%d", &n); //screen表示屏幕;
printf("%d", n); == printf(screen, "%d", n); //screen表示屏幕;
sscanf() == string + scanf;
sprintf() == string + printf;
sscanf(str, "%d", &n); //把字符串str中的内容以"%d"的格式写到n中(从左到右);
sprintf(str, "%d", n); //把n以%d的格式写到字符串str中(从左到右)

memsetは、0または-1の値を割り当てるためにのみ使用できます。memsetはバイト単位で割り当てられる
ため、fill関数を使用して他の値を割り当てます。

#include <algorithm>
fill(arr, arr+n, 233)

関数パラメーターとして配列を取ります

配列がパラメーターとして使用される場合、パラメーター内の配列の最初の次元は長さを入力する必要はなく(2次元配列の場合、2番目の次元は長さを入力する必要があります)、実際の呼び出しでは、配列名が必要です。
配列をパラメーターとして使用する場合、関数内の配列要素の変更は、元の配列要素の変更と同等です(これは通常のローカル変数とは異なります)。

void test(int a[], int b[][5])
{
    
    
	...
}

構造コンストラクター

いわゆるコンストラクターは、構造体を初期化するために使用される関数であり、構造体で直接定義されます。
コンストラクターの特性:戻り値の型を記述する必要はなく、関数名は構造体名と同じです。
一般的に定義されている構造の場合、デフォルトのコンストラクター(ただし表示されません)が内部で生成されます。「studentInfo(){}」はデフォルトで生成されるコンストラクターです。このコンストラクターの関数名は構造体の型名と同じであり、戻り値の型がないことがわかります。

struct stuentInfo
{
    
    
	int id;
	char gender;
	studentInfo(){
    
    }
};

struct stuentInfo
{
    
    
	int id;
	char gender;
	studentInfo(int _id, char _gender):id(_id), gender(_gender){
    
    }
};
studentInfostu = studentInfo(10086, 'M');
char str[100];
cin.getline(str, 100); //输入一行;
string str;
getline(cin, str);

浮動小数点数の比較

const double eps = 1e-8;
#define Equ(a, b) (fabs((a)-(b))) < (eps) //==等于
#define More(a, b) (((a)-(b)) > (eps)) //>大于
#define Less(a, b)  (((a)-(b)) < (-eps)) //<小于
#define MoreEqu(a, b) (((a)-(b)) > (-eps)) //>=大于等于
#define LessEqu(a, b) (((a)-(b)) < (eps)) //<=小于等于
const double Pi = acos(-1.0)

一般的なOJシステムの場合、1秒間に実行できる操作の数は約10 ^ 7〜10 ^ 8であり、

while(scanf("%d", &n) != EOF)
{
    
    
	...
}

迅速なべき乗

typedef long long LL;
LL pow(LL a, LL b, LL m) // 求 a^b % m;
{
    
    
    LL ans = 1;
	while(b > 0)
	{
    
    
		if(b % 2 == 1)
		{
    
    
			ans = ans * a % m;
		}
        a = a * a % m;
		b = b / 2;
	}
	return ans;
}

最大公約数

int  gcd(int a, int b)
{
    
    
	if(b==0) return a;
	else return gcd(b, a % b);
}

おすすめ

転載: blog.csdn.net/weixin_42137874/article/details/113607760