c++代码格式与语法基础
#include <bits/stdc++.h> //使用万能头文件
using namespace std;
int main()
{
cout << "Hello,World!" << endl;//利用cout将字符串输出,字符串用双引号扩起
printf("Hello,World!");//利用printf将字符串输出
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x=3;
double d=3.14;
char ch='A';
char s[]="hello";
bool b=ture;
cout << x << '\n';//'\n是换行符号,比endl更快'//3
cout << d << '\n';//3.14
cout << ch << '\n';//A
cout << s << '\n';//hello
cout << b << '\n';//1
return 0;
}
手撕代码-1(数组)
#include <bits/stdc++.h>
using namespace std;
//const表示常量,后续不可以被修改
const int N=1e5+9;
int a[N];
//开了一个大小为N的全局数组,下标为[0,N-1],自动初始化为0(在全局变量里)
int main()
{
return 0;
}
手撕代码-2(typedef)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;//将long long类型定义为ll(方便后续使用)
//const表示常量,后续不可以被修改
const int N=1e5+9;
//开一个大小为N的全局数组(类型为long long),下标为【0,N-1】,自动初始化为0
ll a[N];
int main()
{
return 0;
}
手撕代码-3(字符串)
#include <bits/stdc++.h>
using namespace std;
int main()
{
char s[]="hello";
for(i=0;i<=4;i++)
{
cout << s[i]; //hello
}
cout << '\n';
cout << s <<'\n'; //hello
return 0;
}
手撕代码-4(交换变量)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a=5,b=3;
int tmp=b;
b=a;
a=tmp;
cout << a << '' << b << '\n';//3 5
return 0;
}
手撕代码-5(判断)
#include <bits/stdc++.h>
using namespace std;
int main()
{
//输出1~n中所有的偶数
int n=10;
for(int i=i;i<=n;i++)
{
if(i%2==0)
cout << i << '\n';
}
return 0;
}