#include <iostream>
using namespace std;
int main(){
int x=1;
// x-=6;
x-=6;
cout<<x;
}
int = 9+2;
引用
#include
逻辑判断
if(条件){}
and用&&
或||
条件与循环
条件判断switch default:==else
int main(){
int x;
cin>>x;
switch(x){
case 0:{
cout<<"bell";
break;
}
case 6:{
cout<<"bell bell";
break;
}
case 12:{
cout<<"bell bell bell";
break;
}
case 18:{
cout<<"bell bell bell bell";
break;
} default:
cout<<"罢工";
}
}
输入cin
循环
while
完成要求循环
for(变量定义;循环条件;变量变化){}
do while
至少循环一次
函数
优点
特点
函数不一定有返回值
有返回值用return
无返回值void
递归
#include <iostream>
#include <iomanip>
using namespace std;
int factorial(int n){
if(n==1){
return 1;
}
else{
return n*factorial(n-1);
}
}
int main(){
cout<<factorial(3)<<endl;
}
要终止条件
自我调用
要return
包含大量常见函数
#include <bits/stdc++.h>
把小数强制转换成整数
if(n==(int)n){}
int x = f;
优化性能
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,i,c = 0;
cin>>n;
for(i = 2;i<=sqrt(n);i++){
if(n % i==0){
c++;
break;
}
}
if(c==0&&n>1){
cout<<"T";
}else{
cout<<"F";
}
}