OI/ACM C++語法基礎
阿新 • • 發佈:2021-01-31
OI/ACM C++語法基礎
第一課
變數、輸入輸出、表示式和順序語句
生涯的開始:Hello World
注意:程式碼量非常重要,所以每個程式碼都儘量手打一遍
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
初識變數
1.變數的定義
變數必須先定義,才可以使用。不能重名。
變數定義的方式:
#include <iostream> using namespace std; int main() { int a = 0; int b, c = a, d = 10 / 2; return 0; }
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-NWczrfbm-1611978425799)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210130112825320.png)]
2.輸入輸出
整數輸入輸出:
#include <iostream> using namespace std; int main() { int a; cin >> a; cout << a << endl; return 0; }
字串輸入輸出:
#include <iostream>
using namespace std;
int main() {
string str;
cin >> str;
cout << str << endl;
return 0;
}
浮點數輸入輸出:
#include <iostream> using namespace std; int main() { float a; double b; cin >> a >> b; cout << a << b; return 0; }
表示式
#include <iostream>
using namespace std;
int main() {
int a = 3 + 2 / 2;
int b = a;
float c = b / a;
cout << c << endl;
return 0;
}
3.自增自減
程式碼:
#include <iostream>
using namespace std;
int main() {
int a = 0;
int b = a++;
int c = 5;
c--;
cout << a << b << c;
return 0;
}
最後講講一些基礎知識:
1.^表示次方
2.char ‘x’: 一定是單引號!!!
3.科學計數法:1.23e3 == 1.23*10^3
關於型別轉換:
對照ANSll碼轉換即可!