C++ 基礎
編譯後,執行
g++ main.cpp && ./a.out
編譯後,執行並導入測資
g++ main.cpp && ./a.out < d1.txt
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 7;
int b = 2;
cout << a + b << endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double pi = 3.14159;
cout << fixed << setprecision(4);
cout << pi << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
string astr = "123";
cout << stoi(astr) + 864 << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n = 123;
cout << "C++ " + to_string(n) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
cout << "Sum: " << a+b << endl;
return 0;
}
讀取解題測資,不設任何輸入提示訊息
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << "Sum: " << a+b << endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double pi = 3.14159;
double r;
cout << "Radius: ";
cin >> r;
cout << fixed << setprecision(2);
cout << "Perimeter: " << 2*r*pi << endl;
return 0;
}
讀取解題測資,不設任何輸入提示訊息
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double pi = 3.14159;
double r;
cin >> r;
cout << fixed << setprecision(2);
cout << "Perimeter: " << 2*r*pi << endl;
return 0;
}
字串以雙引號 "abcde" 括住,字元以單引號 'a' 括住
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char grade;
int score;
cout << "Grade: ";
cin >> grade;
cout << "Score: ";
cin >> score;
grade = toupper(grade);
cout << grade << " " << score << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
string name;
int age;
cout << "What is your name? ";
cin >> name;
cout << "How old are you? ";
cin >> age;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
cout << endl;
cout << a << " + " << b << " = " << a + b << endl;
cout << a << " - " << b << " = " << a - b << endl;
cout << a << " * " << b << " = " << a * b << endl;
cout << endl;
cout << a << " / " << b << " = " << (double)a / b << endl;
cout << a << " / " << b << " = " << a / (double)b << endl;
cout << a << " / " << b << " = " << a / b << endl;
cout << a << " % " << b << " = " << a % b << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cout << "n: ";
cin >> n;
cout << "|" << n << "| = " << abs(n) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int sum = 10;
int diff = 4;
int large, small;
large = (sum + diff) / 2;
small = (sum - diff) / 2;
cout << "Large number: " << large << endl;
cout << "Small number: " << small << endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x, p, r;
cout << "x: ";
cin >> x;
p = pow(x, 2);
cout << "p: " << p << endl;
r = sqrt(p);
cout << "r: " << r << endl;
return 0;
}
十進位的2位數除以10,商數就是十位數,餘數就是個位數
#include <iostream>
using namespace std;
int main() {
int n;
cout << "n: ";
cin >> n;
cout << "units digit: " << n % 10 << endl;
cout << " tens digit: " << n / 10 << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
cout << (a & b) << endl;
cout << (a | b) << endl;
cout << (a ^ b) << endl;
cout << (a << b) << endl;
cout << (a >> b) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n = 987;
cout << "Dec: " << n << endl;
cout << "Oct: " << oct << n << endl;
cout << "Hex: " << hex << n << endl;
cout << showbase;
cout << "Oct: " << oct << n << endl;
cout << "Hex: " << hex << n << endl;
return 0;
}
# | 運算子 |
---|---|
1 | () |
2 | +(正號), -(負號), ++(遞增), --(遞減), !(NOT), ~(位元NOT), sizeof(取得記憶體空間大小), (強制轉型) |
3 | *(乘法), /(除法、商數), %(餘數) |
4 | +(加法), -(減法) |
5 | <<(位元左移), >>(位元右移) |
6 | >(大於), <(小於), >=(大於等於), <=(小於等於) |
7 | ==(是否相等), !=(是否不相等) |
8 | &(位元AND) |
9 | ^(位元XOR) |
10 | |(位元OR) |
11 | &&(條件AND) |
12 | ||(條件OR) |
13 | =, +=, -=, *=, /=, %= |
#include <iostream>
#include <climits>
#include <cfloat>
using namespace std;
int main() {
char i1 = CHAR_MAX;
short i2 = SHRT_MAX;
int i4 = INT_MAX;
long i8 = LONG_MAX;
float f4 = FLT_MAX;
double f8 = DBL_MAX;
cout << " char : " << (int)i1 << endl;
cout << " short : " << i2 << endl;
cout << " int : " << i4 << endl;
cout << " long : " << i8 << endl;
cout << " float : " << f4 << endl;
cout << "double : " << f8 << endl;
return 0;
}
#include <iostream>
#include <cmath>
#include <climits>
using namespace std;
int main() {
int bytes = sizeof(int);
int bits = bytes * 8;
cout << bytes << " bytes" << endl;
cout << bits << " bits" << endl;
cout << INT_MIN << " ~ " << INT_MAX << endl;
cout << -(int)(pow(2, bits)) << " ~ " << INT_MAX << endl;
cout << INT_MIN << " ~ " << (int)(pow(2, bits))-1 << endl;
return 0;
}
Type | Size | Range |
---|---|---|
char (CHAR) | 1 byte | -128 ~ 127 |
short (SHRT) | 2 bytes | -32768 ~ 32767 |
int (INT) | 4 bytes | -2147483648 ~ 2147483647 |
long (LONG) | 8 bytes | -9223372036854775808 ~ 9223372036854775807 |
unsigned char (UCHAR) | 1 byte | 0 ~ 255 |
unsigned short (USHRT) | 2 bytes | 0 ~ 65535 |
unsigned int (UINT) | 4 bytes | 0 ~ 4294967295 |
unsigned long (ULONG) | 8 bytes | 0 ~ 18446744073709551615 |
凱撒加解密字母
#include <iostream>
using namespace std;
int main() {
int i = 65;
char c = 'A';
cout << i << endl;
cout << c << endl;
cout << (char)i << endl;
cout << (char)(i+1) << endl;
cout << (int)c << endl;
cout << (int)(c+1) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, t;
cout << "Enter 2 numbers: ";
cin >> a >> b;
t = a;
a = b;
b = t;
cout << a << ' ' << b << endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int dividend = 19;
int divisor1 = 3;
double divisor2 = 6.0;
double answer;
answer = dividend / divisor1;
cout << "answer1: " << answer << endl;
answer = dividend / divisor2;
cout << "answer2: " << answer << endl;
answer = (double)dividend / divisor1;
cout << "answer3: " << answer << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter 3 numbers: ";
cin >> a >> b >> c;
cout << a << ' ' << &a << endl;
cout << b << ' ' << &b << endl;
cout << c << ' ' << &c << endl;
return 0;
}
兩個以上單字,第一個單字全小寫,第二個以上單字首字母大寫
#include <iostream>
using namespace std;
int main() {
string yourName;
string yourBestFriend;
cout << "What is your name? ";
cin >> yourName;
cout << "Who is your best friend? ";
cin >> yourBestFriend;
cout << yourName << " & " << yourBestFriend << endl;
return 0;
}
條件式: 項目一 比較運算 項目二 (條件成立 true / 條件不成立 false)
if 必須接條件式
#include <iostream>
using namespace std;
int main() {
string weather;
cout << "Beebo: Let's hang out." << endl;
cout << "How is the weather? ";
cin >> weather;
if (weather == "sunny") {
cout << "Sure. Why not?" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
string weather;
cout << "Beebo: Let's hang out." << endl;
cout << "How is the weather? ";
cin >> weather;
if (weather == "sunny") {
cout << "Sure. Why not?" << endl;
}
else {
cout << "I want to stay home." << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, c;
int max_num;
cout << "Enter 3 numbers: ";
cin >> a >> b >> c;
max_num = a;
if (max_num < b) {
max_num = b;
}
if (max_num < c) {
max_num = c;
}
cout << max_num << endl;
return 0;
}
條件式 ? 條件成立的運算式 : 不成立的運算式
#include <iostream>
using namespace std;
int main() {
int a, b, c;
int max_num;
cout << "Enter 3 numbers: ";
cin >> a >> b >> c;
max_num = (a > b) ? a : b;
max_num = (max_num > c) ? max_num : c;
cout << max_num << endl;
return 0;
}
移除多餘條件
#include <iostream>
using namespace std;
int main() {
int score;
cout << "Your score: ";
cin >> score;
if (score >= 80) {
cout << "A, GPA is 4";
}
else if (score < 80 && score >= 70) {
cout << "B, GPA is 3";
}
else if (score < 70 && score >= 60) {
cout << "C, GPA is 2";
}
else if (score < 60 && score >= 50) {
cout << "D, GPA is 1";
}
else {
cout << "F, GPA is 0";
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
string word;
string line;
cin >> word;
cout << word << endl;
// cin.ignore();
// cin.ignore(256, '\n');
getline(cin, line);
cout << line << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int price;
int tickets;
char creditCard;
int total;
int isCard;
cout << "Price? ";
cin >> price;
cout << "Tickets? ";
cin >> tickets;
cout << "Credit Card? (Y/n): ";
cin >> creditCard;
creditCard = toupper(creditCard);
isCard = (creditCard == 'Y');
total = isCard ? price*tickets*0.9 : price*tickets;
cout << "Total: $" << total << endl;
return 0;
}
先判斷 small 再判斷 furry
#include <iostream>
using namespace std;
int main() {
bool isFurry = true;
bool isSmall = true;
if (isFurry) {
if (isSmall)
cout << "Cat";
else
cout << "Bear";
}
else {
if (isSmall)
cout << "Gecko";
else
cout << "Crocodile";
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
char firstBorn;
char secondBorn;
char thirdBorn;
cin >> firstBorn >> secondBorn >> thirdBorn;
firstBorn = toupper(firstBorn);
secondBorn = toupper(secondBorn);
thirdBorn = toupper(thirdBorn);
if (firstBorn == 'M') {
cout << "1st child: Son -> Aaron" << endl;
if (secondBorn == 'M')
cout << "2nd child: Son" << endl;
else
cout << "2nd child: Daughter" << endl;
}
else {
cout << "1st child: Daughter -> Bella" << endl;
if (secondBorn == 'M')
cout << "2nd child: Son" << endl;
else
cout << "2nd child: Daughter" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int x, y;
char op;
cout << "x: ";
cin >> x;
cout << "y: ";
cin >> y;
cout << "Choose an operation (+ - * / %): ";
cin >> op;
switch(op) {
case '+': cout << x + y << endl;
break;
case '-': cout << x - y << endl;
break;
case '*': cout << x * y << endl;
break;
case '/': cout << x / y << endl;
break;
case '%': cout << x % y << endl;
break;
default: cout << "Wrong operation!" << endl;
}
return 0;
}
讀取一行字串
#include <iostream>
using namespace std;
int main() {
string msg;
int level;
cout << "Message: ";
getline(cin, msg);
cout << "Level (1 to 9): ";
cin >> level;
switch(level) {
case 1: cout << "Send \"" << msg << "\" to level 1\n";
case 2: cout << "Send \"" << msg << "\" to level 2\n";
case 3: cout << "Send \"" << msg << "\" to level 3\n";
case 4: cout << "Send \"" << msg << "\" to level 4\n";
case 5: cout << "Send \"" << msg << "\" to level 5\n";
case 6: cout << "Send \"" << msg << "\" to level 6\n";
case 7: cout << "Send \"" << msg << "\" to level 7\n";
case 8: cout << "Send \"" << msg << "\" to level 8\n";
case 9: cout << "Send \"" << msg << "\" to level 9\n"; break;
default: cout << "Wrong level!\n";
}
return 0;
}
使用 switch-case 語法改寫
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char grade;
cout << "Grade: ";
cin >> grade;
grade = toupper(grade);
if (grade == 'A') {
cout << "Excellent!";
}
else if (grade == 'B') {
cout << "Good!";
}
else if (grade == 'C' || grade == 'D') {
cout << "Poor!";
}
else if (grade == 'F') {
cout << "Egregious!";
}
else {
cout << "Invalid grade!";
}
cout << endl;
return 0;
}
等差數列、等比數列
#include <iostream>
using namespace std;
int main() {
int i;
for (i = 0; i < 10; i += 1) {
cout << i << endl;
}
return 0;
}
連續依序輸出三種符號,例如 @#$@#$...
#include <iostream>
using namespace std;
int main() {
int i;
for (i = 0; i < 10; i += 1) {
cout << '*';
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int i;
for (i = 1; i < 10; i++) {
cout << "i: " << i << endl;
i = i * 2;
cout << "Double of i: " << i << endl;
}
return 0;
}
凱撒加解密字母與單句
#include <iostream>
using namespace std;
int main() {
char upper = 'A';
char lower = 'a';
int i;
for (i = 0; i < 26; i += 1) {
cout << (char)(upper+i) << ' ';
}
cout << endl;
for (i = 0; i < 26; i += 1) {
cout << (char)(lower+i) << ' ';
}
cout << endl;
return 0;
}
等差數列、等比數列
#include <iostream>
using namespace std;
int main() {
int n = 0;
int i = 0;
cout << "n: ";
cin >> n;
for (i = n; i >= 1; i -= 1) {
cout << i << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int q, m;
for (q = 1; q <= 4; q++) {
cout << "Q: " << q << endl;
for (m = 1; m <= 3; m++) {
cout << " M: " << m + (q-1) * 3 << endl;
}
}
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int i, j;
string w = "81";
for (i = 1; i <= 9; i++) {
for (j = 1; j <= 9; j++) {
//
//
cout << setw(w.size()+2) << i*j;
}
cout << endl;
}
return 0;
}
正左直角、倒左直角、正右直角、倒右直角、正金字塔、倒金字塔、奇數菱形、三角沙漏
/*
# 正左直角
i *
1 1 *
2 2 **
3 3 ***
4 4 ****
5 5 *****
# 倒左直角
i *
1 5 *****
2 4 ****
3 3 ***
4 2 **
5 1 *
# 正右直角
i . *
1 4 1 *
2 3 2 **
3 2 3 ***
4 1 4 ****
5 0 5 *****
# 倒右直角
i . *
1 0 5 *****
2 1 4 ****
3 2 3 ***
4 3 2 **
5 4 1 *
# 正金字塔
i . *
1 4 1 *
2 3 3 ***
3 2 5 *****
4 1 7 *******
5 0 9 *********
# 倒金字塔
i . *
1 0 9 *********
2 1 7 *******
3 2 5 *****
4 3 3 ***
5 4 1 *
# 奇數菱形
i . *
1 2 1 *
2 1 3 ***
3 0 5 *****
4 1 3 ***
5 2 1 *
# 三角沙漏
i . *
1 0 5 *****
2 1 3 ***
3 2 1 *
4 1 3 ***
5 0 5 *****
*/
#include <iostream>
using namespace std;
int main() {
int n = 0;
int i = 0, j = 0;
cout << "n: ";
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
cout << '*';
}
cout << endl;
}
return 0;
}
各式數列三角
/*
Case 1:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Case 2:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Case 3:
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Case 4:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Case 5:
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
Case 6:
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
*/
#include <iostream>
using namespace std;
int main() {
int n = 0;
int i = 0, j = 0;
cout << "n: ";
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
cout << j << ' ';
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
int ri, ci, i;
cout << "2D index -> 1D index" << endl;
cout << "n: ";
cin >> n;
for (ri = 0; ri < n; ri++) {
for (ci = 0; ci < n; ci++) {
i = ri * n + ci;
cout << "ri: " << ri << ' '
<< "ci: " << ci << " -> "
<< "i: " << i << endl;
}
}
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n, m;
int ri, ci, i;
cout << "2D index <- 1D index" << endl;
cout << "n: ";
cin >> n;
m = (int)sqrt(n);
cout << "m: " << m << '\n';
for (i = 0; i < n; i++) {
ri = i / m;
ci = i % m;
cout << "ri: " << ri << ' '
<< "ci: " << ci << " <- "
<< "i: " << i << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int i, n, t, c;
cout << "n: ";
cin >> n;
cout << "t: ";
cin >> t;
c = 0;
for (i = t; i < n; i++) {
if (i % t == 0 || i % t == t - 1)
continue;
cout << i << endl;
c += 1;
if (c == 5) {
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
bool isPlus = true;
int total = 0;
while (isPlus) {
total += 1;
cout << total << endl;
if (total == 10) {
isPlus = false;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int total = 0;
while (true) {
total++;
if (total % 2 == 1) {
continue;
}
cout << total << endl;
if (total == 10) {
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n = 10;
while (n) {
cout << n << endl;
n = n - 1;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int total = 0;
int count = 0;
int x;
while (true) {
cin >> x;
if (x == -1)
break;
total += x;
count += 1;
}
cout << "Total: " << total << endl;
cout << "Count: " << count << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int i, step;
int limit = 10;
cout << "step: ";
cin >> step;
i = step;
while (true) {
cout << i << " ";
i += step;
if (i == limit) {
break;
}
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n = 10;
int i;
cout << "for loop: " << endl;
for (i = 0; i < n; i += 1) {
cout << i << endl;
}
cout << "while loop: " << endl;
i = 0;
while (i < n) {
cout << i << endl;
i += 1;
}
return 0;
}
依選項輸出圖形
#include <iostream>
using namespace std;
int main() {
char op;
int n, i;
while (true) {
cout << "1.正左直角 2.倒左直角" << endl;
cout << "3.正右直角 4.倒右直角" << endl;
cout << "5.正金字塔 6.倒金字塔" << endl;
cout << "7.奇數菱形 8.三角沙漏" << endl;
cout << "q.離開迴圈" << endl;
cout << "> ";
cin >> op;
if (op == 'q')
break;
cout << "n: ";
cin >> n;
if (true) {
for (i = 0; i < n; i++) {
cout << i << endl;
}
}
else if (true) {
for (i = 0; i < n; i++) {
cout << i << endl;
}
}
}
return 0;
}
10進位轉 2/8/10/16 進位
#include <iostream>
using namespace std;
int main() {
int n;
string strHex = "0123456789abcdef";
cout << strHex << endl;
cout << "N: ";
cin >> n;
while (true) {
break;
}
cout << "Bin: " << endl;
cout << "Oct: " << endl;
cout << "Dec: " << endl;
cout << "Hex: " << endl;
return 0;
}
/*
[輸入與輸出]
n: 5
-o-o-
x-x-x
-o-o-
x-x-x
-o-o-
n: 6
-o-o-o
x-x-x-
-o-o-o
x-x-x-
-o-o-o
x-x-x-
*/
#include <iostream>
using namespace std;
int main() {
int n, i, j;
cout << "n: ";
cin >> n;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cout << "-";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, i;
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
for (i = a; i <= b; i++) {
cout << i << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int arow[5] = {};
int brow[5];
int i;
cout << "arow: ";
for (i = 0; i < 5; i++) {
cout << arow[i] << " ";
}
cout << endl;
cout << "brow: ";
for (i = 0; i < 5; i++) {
cout << brow[i] << " ";
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int arow[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 10; i++) {
cout << i << ": " << arow[i] << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int arow[10] = {};
int i;
for (i = 0; i < 10; i++) {
cout << &arow[i] << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int arow[] = {9, 8, 7, 5, 2, 0};
int key;
int i = 0;
bool isFound = false;
int N = sizeof(arow) / sizeof(int);
cout << "Array size: " << N << endl;
cout << "key: ";
cin >> key;
for (i = 0; i < N && !isFound; i++) {
if (key == arow[i]) {
cout << "index: " << i << endl;
isFound = true;
}
}
if (!isFound) {
cout << -1 << endl;
}
return 0;
}
統計各點數次數
#include <iostream>
#include <iomanip>
using namespace std;
const int F = 6;
const int N = 30;
int main() {
int faces[F] = {1, 2, 3, 4, 5, 6};
int rnum;
int i;
// fixed random seed
// srand(1);
// time(0): seconds from 1970-01-01 00:00:00 to now
srand(time(0));
for (i = 1; i <= N; i++) {
rnum = rand() % F;
cout << setw(3) << faces[rnum];
if (i % 10 == 0) {
cout << endl;
}
}
return 0;
}
合計數、平均值、最大數、最小數
#include <iostream>
#include <cstdlib>
using namespace std;
const int N = 10;
int main() {
int numbers[N] = {};
int i;
// fixed random seed
srand(1);
for (i = 0; i < N; i++) {
numbers[i] = rand() % 100;
}
for (i = 0; i < N; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int numbers[] = {5, 39, 4, 23, 9, 16, 8, 47, 7, 41};
int i;
sort(numbers, numbers+10);
cout << "Ascending order:" << endl;
for (i = 0; i < 10; i++) {
cout << numbers[i] << ' ';
}
cout << endl;
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int numbers[] = {5, 39, 4, 23, 9, 16, 8, 47, 7, 41};
int i;
// begin() & end()
sort(begin(numbers), end(numbers));
cout << "Ascending order:" << endl;
for (i = 0; i < 10; i++) {
cout << numbers[i] << ' ';
}
cout << endl;
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int numbers[] = {5, 39, 4, 23, 9, 16, 8, 47, 7, 41};
int i;
sort(begin(numbers), end(numbers), greater<int>());
cout << "Descending order:" << endl;
for (i = 0; i < 10; i++) {
cout << numbers[i] << ' ';
}
cout << endl;
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
char word[] = "something";
int N = sizeof(word) / sizeof(char);
int i;
cout << "Size: " << N << endl;
// '\0': NUL
cout << "int of '\\0': " << int(word[N-1]) << endl;
cout << endl;
sort(begin(word), end(word)-1);
cout << "Ascending order:" << endl;
for (i = 0; i < 10; i++) {
cout << word[i] << ' ';
}
cout << endl;
sort(begin(word), end(word)-1, greater<char>());
cout << "Descending order:" << endl;
for (i = 0; i < 10; i++) {
cout << word[i] << ' ';
}
cout << endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int i, n;
string astr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
n = astr.size();
for (i = 0; i < n; i++) {
cout << astr[i] << " "
<< (int)astr[i] << " "
<< setw(2)
<< (int)astr[i]-65 << " "
<< setw(2)
<< (int)(astr[i]-'A') << endl;
}
return 0;
}
氣泡排序、選擇排序、插入排序
#include <iostream>
#include <cstdlib>
using namespace std;
const int N = 10;
int main() {
int numbers[N] = {};
int i;
// fixed random seed
srand(5);
for (i = 0; i < N; i++) {
numbers[i] = rand() % 50;
}
for (i = 0; i < N; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
眾數可能不止一個
#include <iostream>
#include <iomanip>
using namespace std;
const int N = 30;
int main() {
int numbers[N] = {};
int i;
// seed: 1607154996
// seed: 1607155295
// long seed = time(0);
long seed = 1607154996;
cout << "seed: " << seed << endl;
srand(seed);
for (i = 0; i < N; i++) {
numbers[i] = rand() % 100 + 1;
cout << setw(4) << numbers[i];
if ((i+1) % 10 == 0) {
cout << endl;
}
}
return 0;
}
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
#include <stdio.h>
int main() {
int a = 7;
int b = 2;
printf("%d\n", a + b);
return 0;
}
#include <stdio.h>
int main() {
double pi = 3.14159;
printf("%.4f\n", pi);
return 0;
}
#include <stdio.h>
int main() {
int n = 987;
printf("Dec: %d\n", n);
printf("Oct: %o\n", n);
printf("Hex: %x\n", n);
return 0;
}
#include <stdio.h>
int main() {
int i = 65;
char c = 'A';
printf("%d\n", i);
printf("%c\n", c);
printf("%c\n", (char)i);
printf("%c\n", (char)(i+1));
printf("%d\n", (int)c);
printf("%d\n", (int)(c+1));
return 0;
}
#include <stdio.h>
int main() {
int a, b;
printf("a: ");
scanf("%d", &a);
printf("b: ");
scanf("%d", &b);
printf("Sum: %d\n", a+b);
return 0;
}
讀取解題測資,不設任何輸入提示訊息
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("Sum: %d\n", a+b);
return 0;
}
#include <stdio.h>
int main() {
double pi = 3.14159;
double r;
printf("Radius: ");
scanf("%lf", &r);
printf("Perimeter: %.2f\n", 2*r*pi);
return 0;
}
字串以雙引號 "abcde" 括住,字元以單引號 'a' 括住
#include <stdio.h>
#include <ctype.h>
int main() {
char grade;
int score;
printf("Grade: ");
scanf("%c", &grade);
printf("Score: ");
scanf("%d", &score);
grade = toupper(grade);
printf("%c %d\n", grade, score);
return 0;
}
#include <stdio.h>
int main() {
char name[10];
int age;
printf("What is your name? ");
scanf("%s", name);
printf("How old are you? ");
scanf("%d", &age);
printf("Name: %s\n", name);
printf("Age: %d\n", age);
return 0;
}
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter 3 numbers: ");
scanf("%d %d %d", &a, &b, &c);
printf("%d %p\n", a, &a);
printf("%d %p\n", b, &b);
printf("%d %p\n", c, &c);
return 0;
}