
取消同步流是为了解决C++有时遇到空格或回车(不到\0)就会停下的问题
#include<bits/stdc++.h>
using namespace std;
int main()
{
	//取消同步流
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int a, b;
	cin >> a>> b;
	cout << a + b << '\n';
	return 0;
} 
c++基础
数组
#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int x=2;
	double y=3.14;
	char ch='A';//字符常量''
	char s[]="hello world!";//字符串""
	bool m = true;
	cout << x << '\n' << y <<'\n' << ch << '\n' << s << '\n'<< m << '\n';
	//'\n'比endl更快
	return 0;
} 

const
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
/*1e5 表示科学计数法中的 10 的 5 次方,即 100000,
所以 N 的值为 100009。这样定义常量的好处是可以在程序中
多次使用该常量而不需要重复写具体的数值,提高了代码的可读性和维护性。
*/
char a[N];//定义一个大小为N的全局数组,只有N为常量时,才可以这样定义全局数组
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);//可以不写
	return 0;
} 
typedef
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
/*1e5 表示科学计数法中的 10 的 5 次方,即 100000,
所以 N 的值为 100009。这样定义常量的好处是可以在程序中
多次使用该常量而不需要重复写具体的数值,提高了代码的可读性和维护性。
*/
typedef long long ll;//将longlong类型定义为ll方便后续使用
ll a[N];//定义一个大小为N的全局数组(类型为long long)
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);//可以不写
	return 0;
} 
字符串
#include<bits/stdc++.h>
using namespace std;
int main()
{
	char s[] = "hello";
	for (int i = 0; i <= 4; i ++ )
	{
		cout << s[i] << '\n';
	}
	cout << s << '\n';
	return 0;
} 
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a = 3;
	int b = 5;
	int tmp;
	tmp = a;
	a = b;
	b = tmp;
	cout << a << b << '\n';
	return 0;
} 
判断奇偶
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a;
	cin>> a;
	if (a % 2 == 0)//注意双等号
		cout << "偶数" << '\n';
	else if(a%2==1)
		cout << "奇数" << '\n';
	return 0;
} 
求1-n之间的所有偶数
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		if (i % 2 == 0)
			cout << i << '\n';
	}
	return 0;
} 
scanf和printf

注意&
#include<bits/stdc++.h>
using namespace std;
int main()
{
	double a,b;
	scanf_s("%lf %lf", &a, &b);
	printf("%.2lf, %.3lf", a, b);//保留2、3位小数
	return 0;
} 
C++设置精度 fixed setprecision()

 
C++中cin遇到空格或回车就结束

疑惑不懂scanf和cin取消同步流



















