owned this note changed a year ago
Published Linked with GitHub

程式基礎概念


基本格式與輸出


#include <iostream> //標頭檔 using namespace std; int main(){ //主函數 cout << "Hello World!"; //輸出 return 0; //函數執行結束 }

試著練習MDjudge上的A001


換行


#include<iostream> using namespace std; int main(){ cout << "你要輸出的文字"; cout << "輸出這句後會換行" << endl; cout << "這個也會換行喔~" << '\n'; return 0; }

試著練習MDjudge上的A002


變數型態


EXAMPLES

integer(整數):0、-1、99
double(浮點數):1.83、-0.06、5.0
char(字元):'a'、'D'、'5'、'%'
string(字串):"Hello World!"
bool(布林值):true、false


integer(整數) 在程式中會寫成int


知道了變數型態

那就來創造變數吧!

並且來輸入數值


#include<iostream> using namespace std; int main(){ int a; //宣告變數 cin >> a; //輸入 cout << a; //輸出 return 0; }

賦值的符號是 =

左邊放接受資料的變數,右邊放賦予的值

#include<iostream> using namespace std; int main(){ int a = 5; int b; b = 48763; return 0; }

也可更改變數值

#include<iostream> using namespace std; int main(){ int a = 13; int b = 33424; b = a; return 0; }

變數命名規則

  • 不能以數字開頭
    例:1a, 2p
  • 不能跟保留字一樣
    例:if, int… 保留字清單

  • 建議:
    變數拿來做什麼的就取什麼,
    這樣自己比較好懂
    到後面會有很多變數

試著練習MDjudge上的A003


算數運算符號

+:加法
-:減法
*:乘法
/:除法
%:取餘數


#include<iostream> using namespace std; int main(){ int a=9,b=2; cout<<a+b<<endl; //輸出11 cout<<a-b<<endl; //輸出7 cout<<a*b<<endl; //輸出18 cout<<a%b<<endl; //輸出1 return 0; }

除法比較特殊

#include<iostream> using namespace std; int main(){ int a=9,b=2; cout<<a/b<<endl; //輸出4 double c=9.0,d=2.0; double e=c/d; cout<<e; //輸出4.5 return 0; }

試著練習MDjudge上的A004

Select a repo