入门1 - - 精讲 -- 第一章节
2025-07-28 17:50:10
发布于:广东
入门1也是肥肠的简单
虽说难度可以说是很easy,但善良的我希望更多入门的兄弟们看一看。
1.基本框架:
#include <iostream>//头文件
using namespace std;
int main(){
return 0;//好习惯
}
虽说return 0可以省略,但是加上是一个好习惯。
2.定义:
int n;
//int的值去在-2147483647~2147483647之间
long long m;//<-好东西
//long long的值取在-9223372036854775807~9223372036854775807之间
//注意:定义不能重复,如(int n,n;)
3.曾经让我差一点把电脑砸了的分号
分号如同我们写作文时的句号,可多不可少。
正确示例:
#include <iostream>
using namespace std;
int main(){
int n;;;;;;;;;;;;;;;;;;;;;;;;;;
return 0;
}
//如果这样错了来找我!
注意:分号别加错了位置。
4.简单的输入输出
cin>>……
cout<<……
记法:C中输入加上in,C中输出加上out。 输入对应定义数,顺序要看题目中。
示例:
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
cout << n;
return 0;
}
5.加减乘除 取余
在我们日常生活中,加减乘除非常常见,但在比我们又聪明,又愚蠢的计算机语言中,取余也很重要。
加:+
减:-
乘:*
除:/
取余:%
切记!!!所有内容用英文符号!!!
错误示范:
#include《iostream》
using namespace std;
int main(){
int n;
cin》》n;
cout《《n;
return 0;
}
知道错在哪吧
正确示范:
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
cout << n+1 << n-1 << n*2 << n/2 << n%2;
return 0;
}
注意啦!!! 计算机在计算n/2时(如5/2)不会输出2.5,确实2(取整)
我很生气,觉得逼迫他输出2.5
示范:
#include <iostream>
using namespace std;
int main(){
double n;
//浮点数
cin >> n ;
//照常输入输出。
cout << n/2;
return 0;
//这样就可以了!!
}
那……如果我想输出->
*
**
***
怎么办呢?
以后可以用上循环。
示范:
#include <iostream>
using namespace std;
int main(){
cout << "*" << endl;
//连续输出,endl是指换行,"\n"也行。
cout << "**" << endl;
cout << "***" ;
return 0;
}
6.保留小数点
比如1/3保留两位小数是0.33
示例:
#include <iostream>
#include <cstdio>
//头文件cstdio是可以使用保留小数能力的。
using namespace std;
int main(){
int n;
//比如3
cin >> n;
printf("%.2f",n/3.0);
//2可以改变(小数点后多少位)
//3.0可以保证不出现小数不对齐。
return 0;
}
好了,课内内容你已经学完了。
别不信么!
做几道例题练练手。
1.变量输入输出(题单中入门1第一题)
示例代码:
#include <iostream>
using namespace std;
int main(){
int a ;
cin>>a;
cout<<a;
return 0;
}
2.输出ab之和。
定义a和b,0<=a,b<=1000000000000
输出a(换行)b(换行)a+b
示例代码:
#include <iostream>
using namespace std;
int main(){
long long a,b; //<-1000000000000超出了int的范围,用long long
cin >> a >> b;
cout << a <<endl<< b <<endl<< a+b ;
return 0;
}
好了,入门1 -- 第一章节你学完了。
我滴手快炸了,你也休息休息吧。明天更新入门1 -- 第二章节
你也点个赞再走吧!
这里空空如也
有帮助,赞一个