變數與運算

Sprout 2021/03/06

變數

數學中的變數

雨婷原本有一些糖果,她吃掉了15顆,還剩下13顆。請問雨婷原本有幾顆糖果?


雨婷原本有x顆糖果

x - 15 = 13

x = 28

程式中的變數

電腦記憶體中的一個儲存位置

 

我們可以對裡面的值做更改和運算

 

在 C/C++ 裡面,變數有多種型態

宣告變數

使用變數前,要先「宣告」

變數型態

變數名稱

int chihuahua;

常見的變數型態

  1. 整數 int, long long int
  2. 浮點數(小數)double, float 
  3. 布林值(true or false)bool
  4. 字元 char

 

                    and more​ 

常見的變數型態

#include <iostream>
using namespace std;
int main() {
    int a; // 宣告一個型別為整數、名字叫做a的變數
    double b;
    bool c;
    char d;
    
    return 0;
}

bit/byte

bit 是記憶體中最小的單位(一個0/1的單位)

1 byte = 8 bits

一個 int 有 4 bytes = 32 bits 

➤ 有 2^32 種可能

➤ 可以儲存 −2^31 ~ 2^31−1 的所有整數

➤ 即 -2147483648 ~ 2147483647

float vs. double

float: 4bytes

➤ 精度低、佔的記體體較少

 

double: 8 bytes

➤ 精度高、佔的記憶體較多

變數命名規則

  1. 使用英文字母大小寫、數字、底線
  2. 不能以數字開頭
  3. 不能跟 C++ 的 keyword 同名
  4. 大小寫有差別

➤ int ABC 跟 int abc 是兩個不同變數

➤ int 是 keyword 但 Int 不是

變數命名建議

使用有意義的字當作變數名稱

  • int first_num;
  • int firstNum;
  • int FirstNum;
  • int a;
  • int aa;
  • int aaa;

O/X

Q1. int double;

 

Q2. int Double;

 

Q3. int 302a;

 

Q4. float _a;

 

Q5. Float f;

一次宣告多個變數

  • int firstNum, secNum, thirdNum;

宣告時就賦值

相當於

int firstNum = 3;
int firstNum = 3;
int firstNum;
firstNum = 3;

輸入/輸出變數的值

#include<iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    cout << a << b;
    return 0;
}

運算

四則運算

#include <iostream>
using namespace std;
int main() {
    int x = 8, y = 4;
    cout << x + y << endl;
    cout << x - y << endl;
    cout << x * y << endl;
    cout << x / y << endl;
    cout << (x + y) * (x - y) << endl;
    
    return 0;
}

除法

#include <iostream>
using namespace std;
int main() {
    int x = 6, y = 4;
    cout << x / y << endl;
    
    float a = 6, b = 4;
    cout << a / b << endl;
    
    cout << x / b << endl;
    
    return 0;
}

模運算, Mod

#include <iostream>
using namespace std;
int main() {
    int x = 6, y = 4;
    cout << x << " / " << y << " = " << x / y << " ... " << x % y << endl;    
    
    return 0;
}

取餘數

小練習 -- 雞兔同籠

a  顆頭、b 隻腳,請問有幾隻雞、幾隻兔

假設有 x 隻雞、y 隻兔

➤ x + y = a --- (1)

➤ 2x + 4y = b --- (2)

 

(1)*2

➤ 2x + 2y = 2a --- (3)

 

 

(2)-(3)

➤ y = (b-2a)/2

➤ x = a-y

小練習 -- 雞兔同籠

➤ y = (b-2a)/2

➤ x = a-y

#include <iostream>
using namespace std;
int main() {
    int a = 35, b = 94;
    int x, y;
    
    // todo
    
    cout << "chicken: " << x << endl;
    cout << "rabbit: " << y << endl;
    
    return 0;
}

小練習 -- 雞兔同籠

#include <iostream>
using namespace std;
int main() {
    int a = 35, b = 94;
    int x, y;
    
    y = (b-2*a)/2;
    x = a-y;
    
    cout << "chicken: " << x << endl;
    cout << "rabbit: " << y << endl;
    
    return 0;
}

➤ y = (b-2a)/2

➤ x = a-y

小練習 -- 雞兔同籠

若 a, b 改成任意輸入的值呢?

#include <iostream>
using namespace std;
int main() {
    int a, b, x, y;
    
    // todo
    
    y = (b-2*a)/2;
    x = a-y;
    
    cout << "chicken: " << x << endl;
    cout << "rabbit: " << y << endl;
    
    return 0;
}

小練習 -- 雞兔同籠

#include <iostream>
using namespace std;
int main() {
    int a, b, x, y;
    
    cin >> a >> b;
    
    y = (b-2*a)/2;
    x = a-y;
    
    cout << "chicken: " << x << endl;
    cout << "rabbit: " << y << endl;
    
    return 0;
}

課堂練習

卡住的話可以舉手問講師&助教哦

#include <iostream>
using namespace std;
int main() {

    // 宣告變數
    // 輸入變數
    // 輸出答案
    
    return 0;
}

提示

#include <iostream>
using namespace std;
int main() {

    int r1, r2;
    cin >> r1 >> r2;
    cout << 4 * 3 * (r1 * r1 + r2 * r2);
    
    return 0;
}

Solution

更新變數的值

x 遞增 1

  • x = x + 1;
  • x += 1;
  • x++;
  • ++x

x 遞減 1

  • x = x - 1;
  • x -= 1;
  • x--;
  • --x

'=' 是賦予、指定的意思,不是數學上的相等

More examples

  • a += 5;   即 a = a + 5;  
  • b -= 5;    即 b = b - 5;
  • c *= 5;    即 c = c * 5;
  • d /= 5;    即 d = d / 5;
  • e %= 5;  即 e =  e % 5;

Homework

Deadline: 3/12 Fri. 23:59

HW 提問方式

❌ 翻拍螢幕、螢幕截圖

✅ 使用可以貼 code 的網站,並複製連結

ideone

codepad

大家務必把今天的內容弄懂哦,否則之後會越來越跟不上QAQ

Made with Slides.com