c++简单使用

取消同步流是为了解决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取消同步流

相关推荐

  1. C#Tcp简单使用

    2024-03-14 22:56:03       14 阅读
  2. QT C++ QCustomPlot 简单使用

    2024-03-14 22:56:03       13 阅读
  3. C#队列(Queue)简单使用方法

    2024-03-14 22:56:03       16 阅读
  4. C++11std::bind的简单使用

    2024-03-14 22:56:03       10 阅读
  5. C#中UDP的简单使用+样例

    2024-03-14 22:56:03       42 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-14 22:56:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-14 22:56:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-14 22:56:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-14 22:56:03       20 阅读

热门阅读

  1. 【matlab】如何将.mat文件与.nii文件互转

    2024-03-14 22:56:03       24 阅读
  2. CopyOnWriteArrayList是线程安全的吗?

    2024-03-14 22:56:03       22 阅读
  3. C语言如何定义⼆维数组?

    2024-03-14 22:56:03       24 阅读
  4. c# 多线程创建及线程同步

    2024-03-14 22:56:03       23 阅读
  5. Python学习DAY14_文档处理_Excel

    2024-03-14 22:56:03       18 阅读
  6. Unity3D 基于ECS的AI思考与怪物同步详解

    2024-03-14 22:56:03       24 阅读
  7. Memcached

    Memcached

    2024-03-14 22:56:03      16 阅读
  8. PCL点云裁剪CropBox

    2024-03-14 22:56:03       19 阅读
  9. 鸿蒙:警告弹窗

    2024-03-14 22:56:03       29 阅读