Sprout 2021/03/06
雨婷原本有一些糖果,她吃掉了15顆,還剩下13顆。請問雨婷原本有幾顆糖果?
設雨婷原本有x顆糖果
x - 15 = 13
x = 28
電腦記憶體中的一個儲存位置
我們可以對裡面的值做更改和運算
在 C/C++ 裡面,變數有多種型態
使用變數前,要先「宣告」
變數型態
變數名稱
int chihuahua;
#include <iostream>
using namespace std;
int main() {
int a; // 宣告一個型別為整數、名字叫做a的變數
double b;
bool c;
char d;
return 0;
}
bit 是記憶體中最小的單位(一個0/1的單位)
1 byte = 8 bits
一個 int 有 4 bytes = 32 bits
➤ 有 2^32 種可能
➤ 可以儲存 −2^31 ~ 2^31−1 的所有整數
➤ 即 -2147483648 ~ 2147483647
float: 4bytes
➤ 精度低、佔的記體體較少
double: 8 bytes
➤ 精度高、佔的記憶體較多
➤ int ABC 跟 int abc 是兩個不同變數
➤ int 是 keyword 但 Int 不是
Q1. int double;
Q2. int Double;
Q3. int 302a;
Q4. float _a;
Q5. Float f;
相當於
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;
}
#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;
}
x 遞增 1
x 遞減 1
'=' 是賦予、指定的意思,不是數學上的相等
Deadline: 3/12 Fri. 23:59
❌ 翻拍螢幕、螢幕截圖
大家務必把今天的內容弄懂哦,否則之後會越來越跟不上QAQ