# coding ```cpp #include <iostream> using namespace std; int main() { int luckyNum[] = {5, 13, 88}; int guess[3]; string s[] = {"first", "second", "third"}; for (int i = 0; i < 3; i++) { cout << "Enter the " << s[i] << " lucky number: "; cin >> guess[i]; } // i j guess[i] luckyNum[j] // 0 0 5 5 // 0 1 5 13 // 0 2 5 88 // 1 0 5 5 // 1 1 5 13 // 1 2 5 88 // 2 0 5 5 // 2 1 5 13 // 2 2 5 88 bool correct[3] = {false, false, false}; // first loop for `guess` for (int i = 0; i < 3; i++) { // second loop for `luckyNum` for (int j = 0; j < 3; j++) { if (guess[i] == luckyNum[j]) { correct[j] = true; // guess = {5, 5, 5} // luckyNum = {5, 13, 88} // correct = {T, F, F} } } } for (int i = 0; i < 3; i++) { if (correct[i] == false) { cout << "You Lose!!" << endl; return 0; } } cout << "You are Baba's lucky guy!!" << endl; return 0; } ``` ``` O| | ------ X|O|X ------ | |O ``` ```cpp #include <iostream> using namespace std; class Phone { string color; string name; int quantity; public: Phone(string acolor, string aname, int aquantity) { color = acolor name = aname quantity = aquantity } string getName() { return name; } void setName(string newName) { name = newName; } }; int main() { Phone phone1("White", "Iphone", 3); // phone1.name = "abc"; // failed Phone phone2("Pink", "Samsung", 5); cout << phone1.getName() << endl; return 0; } ``` ```cpp #include <iostream> using namespace std; void print(int arry[]) { cout << sizeof(arry) << endl; }; int main() { int guess[] = {3, 44, 55, 66, 4, 33, 555, 55}; cout << sizeof(guess) << endl; print(guess); return 0; } ``` ```cpp #include <iostream> //#include <limits> using namespace std; void print(int arry[], int size) { for (int i = 0; i < size; i++) { cout << arry[i] << "\t"; } cout << endl; }; int main() { int count = 0; const int SIZE = 10; int guess[SIZE]; for (int i = 0; i < SIZE; i++) { if (cin >> guess[i]) { count++; } else { break; } } print(guess, count); cin.clear(); cin.ignore(1000000, '\t'); // cin.ignore(numeric_limits<streamsize> max(), '\n'); string test; cin >> test; cout << test << endl; return 0; } ``` ```cpp #include <cstdlib> #include <ctime> #include <iostream> #include <string> using namespace std; void playgame() { int random = rand() % 100; cout << "Enter the number: "; while (true) { int guess; cin >> guess; if (guess == random) { cout << "You win!" << endl; break; } else if (guess < random) { cout << "too low" << endl; } else { cout << "too high" << endl; } } } int main() { srand(time(NULL)); int choice; do { cout << "0.Quit" << endl; cout << "1.Play" << endl; cin >> choice; switch (choice) { case 0: cout << "Thanks for nothing." << endl; return 0; case 1: playgame(); break; } } while (choice != 0); ; } ``` ```cpp #include <cstdlib> #include <ctime> #include <iostream> #include <string> using namespace std; void playgame() { int random = rand() % 100; while (true) { string s; int guess; bool isDigit = true; cout << "Enter the number: "; cin >> s; for (int i = 0; i < s.size(); ++i) { if (!isdigit(s[i])) { cout << "This is not a number!!!!!" << endl; isDigit = false; break; } } if (!isDigit) continue; guess = atoi(s.c_str()); if (guess == random) { cout << "You win!" << endl; break; } else if (guess < random) { cout << "too low" << endl; } else { cout << "too high" << endl; } } } int main() { srand(time(NULL)); int choice; do { cout << "0.Quit" << endl; cout << "1.Play" << endl; cin >> choice; switch (choice) { case 0: cout << "Thanks for nothing." << endl; return 0; case 1: playgame(); break; default: cout << "Wrong number" << endl; break; } } while (choice != 0); ; } ``` ```cpp #include <iostream> #include <limits> using namespace std; void print(int arry[], int size) { for (int i = 0; i < size; i++) { cout << arry[i] << "\t"; } cout << endl; }; int main() { int count = 0; const int SIZE = 10; int guess[SIZE]; for (int i = 0; i < SIZE; i++) { if (cin >> guess[i]) { count++; } else { break; } } print(guess, count); /* cout << "============" << endl; int a = 10; int* p = &a; cout << a << endl; cout << &a << endl; cout << p << endl; cout << *p << endl; *p = 3; cout << a << endl; a = 20; cout << a << endl; cout << *p << endl; cout << "============" << endl; int arr[3] = {1, 2, 3}; cout << arr << endl; cout << arr[0] << endl; cout << &(arr[0]) << endl; cout << arr[1] << endl; cout << &(arr[1]) << endl; cout << "============" << endl; cout << sizeof(a) << endl; cout << sizeof(&a) << endl; cout << "============" << endl; */ cin.clear(); // cin.ignore(1000000, 'n'); cin.ignore(numeric_limits<streamsize>::max(), '\n'); string test; cin >> test; cout << test << endl; return 0; } ``` ```cpp #include <cstdlib> #include <ctime> #include <iostream> #include <string> using namespace std; void play_array(int array[], int count) { for (int i = 0; i < count; i++) { cout << array[i] << "\t" << "\n"; } } void playgame() { int guesses[100]; int guess_count = 0; int random = rand() % 100; while (true) { string turn; int num; bool isDig = true; cout << "Enter a number: "; cin >> turn; for (int i = 0; i < turn.size(); i++) { if (!isdigit(turn[i])) { cout << "This is not a number." << endl; isDig = false; break; } } if (!isDig) continue; num = atoi(turn.c_str()); guesses[guess_count++] = num; if (random == num) { cout << "You win!" << endl; break; } else if (num < random) { cout << "too low" << endl; } else { cout << "too high" << endl; } } play_array(guesses, guess_count); } int main() { srand(time(NULL)); int choice; do { cout << "0.Quit" << endl; cout << "1.Play" << endl; cin >> choice; switch (choice) { case 0: cout << "Thanks for nothing." << endl; return 0; case 1: playgame(); break; default: cout << "Select again" << endl; break; } } while (choice != 0); return 0; } ``` ```cpp #include <cstdlib> #include <ctime> #include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; void play_vector(vector<int> vector) { for (int i = 0; i < vector.size(); i++) { cout << vector[i] << "\t" << endl; } } void playgame() { vector<int> guesses; int count = 0; int random = rand() % 100; while (true) { string turn; bool isNum = true; cout << "Enter a number:"; cin >> turn; count++; for (int i = 0; i < turn.size(); i++) { if (!isdigit(turn[i])) { cout << "This is not a number!" << endl; isNum = false; break; } } if (!isNum) continue; int guess = atoi(turn.c_str()); guesses.push_back(guess); if (guess == random) { cout << "You win!" << endl; break; } else if (guess < random) { cout << "too low" << endl; } else { cout << "too high" << endl; } } // check if there is file `best_score` // https://dotblogs.com.tw/v6610688/2013/11/11/cplusplus_check_file_exist // if no, output `count` to `best_score` // if yes, read the best score from `best_score` // ofstream file("best_score"); // int i = 9999; // file << i; ifstream input("best_score"); int best_score; input >> best_score; ofstream output("best_score"); if (count < best_score) { output << count; } else { output << best_score; } play_vector(guesses); } int main() { srand(time(NULL)); int choice; do { cout << "0.Quit" << endl; cout << "1.Play" << endl; cin >> choice; switch (choice) { case 0: cout << "Thanks for nothing." << endl; return 0; case 1: playgame(); break; default: cout << "Select again" << endl; break; } } while (choice != 0); return 0; } ``` ```cpp #include <cstdlib> #include <ctime> #include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; void play_vector(vector<int> vector) { for (int i = 0; i < vector.size(); i++) { cout << vector[i] << "\t" << endl; } } void playgame() { vector<int> guesses; int count = 0; int random = rand() % 100; while (true) { string turn; bool isNum = true; cout << "Enter a number:"; cin >> turn; count++; for (int i = 0; i < turn.size(); i++) { if (!isdigit(turn[i])) { cout << "This is not a number!" << endl; isNum = false; break; } } if (!isNum) continue; int guess = atoi(turn.c_str()); guesses.push_back(guess); if (guess == random) { cout << "You win!" << endl; break; } else if (guess < random) { cout << "too low" << endl; } else { cout << "too high" << endl; } } ifstream input("best_score"); ofstream output("best_score"); int best_score; if (input.good()) { input >> best_score; if (count < best_score) { output << count; } else { output << best_score; } } else { output << count; } play_vector(guesses); } int main() { srand(time(NULL)); int choice; do { cout << "0.Quit" << endl; cout << "1.Play" << endl; cin >> choice; switch (choice) { case 0: cout << "Thanks for nothing." << endl; return 0; case 1: playgame(); break; default: cout << "Select again" << endl; break; } } while (choice != 0); return 0; } ``` ```cpp #include <iostream> using namespace std; int main() { int num; int x; int y; for (int j = 1; j <= 5; j++) { cin >> num; for (int i = 1; i <= 5; i++) { x = i; y = j; } if (num == 1) { cout << abs(x - 3) + abs(y - 3) << endl; } } return 0; } ``` ```cpp #include <iostream> using namespace std; int main() { int num; int x; int y; // 0 0 0 0 0 // 0 0 0 0 0 // 0 1 0 0 0 // 0 0 0 0 0 // 0 0 0 0 0 // num:0 // x:1 // i:1 // j:1 // y:1 // for (int i = 1; i <= 5; i++) { x = i; for (int j = 1; j <= 5; j++) { cin >> num; y = j; if (num == 1) { cout << abs(x - 3) + abs(y - 3) << endl; } } } return 0; } ``` ```cpp #include <algorithm> #include <iostream> using namespace std; int main() { char line[50]; int n; int t; cin >> n >> t; cin >> line; for(int j =0; j < t; j++){ for (int i = 0; i < n - 1; i++) { if (line[i] == 'B' && line[i + 1] == 'G') { swap(line[i], line[i + 1]); i++; } } } // line = BBBGG -> BBGBG -> BGBGB // j=1 // n=5 // t=2 // line=BGBGB // i=3 // line[i] = B // line[i+1] =G // cout << line << endl; // https://codeforces.com/problemset/problem/266/B // Let's say that the positions in the queue are sequentially numbered by integers from 1 to n, // at that the person in the position number 1 is served first. // Then, if at time x a boy stands on the i-th position and a girl stands on the (i + 1)-th position, // then at time x + 1 the i-th position will have a girl and the (i + 1)-th position will have a boy. // The time is given in seconds. } ``` ```cpp #include <iostream> #include <string> using namespace std; int main() { string a; cin >> a; for (int i = 0; i < a.length(); i++) { if (a[i] == '.') { cout << 0 << endl; } else if (a[i] == '-' && a[i + 1] == '.') { cout << 1 << endl; i++; } else if (a[i] == '-' && a[i + 1] == '-') { cout << 2 << endl; i++; } } // a = .-.-- 012 // i = 5 // a[i] = - // a[i+1] = - // 012 return 0; } ``` ```cpp #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; int main() { int x; string a; cin >> x; while (true) { x = x + 1; string a = to_string(x); int j = 0; if (a[j] != a[j + 1] && a[j] != a[j + 2] && a[j] != a[j + 3] && a[j + 1] != a[j + 2] && a[j + 1] != a[j + 3] && a[j + 2] != a[j + 3]) { cout << a << endl; break; } } return 0; } ``` ```cpp #include <iostream> #include <string> using namespace std; int main() { string s; int times; bool on = true; for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { cin >> times; times = times % 2; s = to_string(times); cout << s[i]; // if (s[i] == 1) { // cout << !on; // } else { // cout << on; // } } cout << endl; } } // s = // 1 0 0 // 0 0 0 // 0 0 1 // // j = 0 // i = 0 // s[0] = 1 // s[1] s[-1] s[0] // 1 0 0 // 0 0 0 // 0 0 1 // int x; // bool on = true; // int light[3][3]; // for (int i = 0; i < 3; i++) { // for (int j = 0; j < 3; j++) { // cin >> light[i][j]; // x = light [i][j]% 2; // if (x == 1) { // light[i][j] && light[i + 1][j] && light[i - 1][j] && light[i][j + // 1] // && // light[i][j - 1] == !on; // } else { // bool on = true; // } // if (on) { // cout << on; // } else { // cout << !on; // } // } // if (x == 0) { // cout << on; // } else { // cout << !on; // } // } // cout << endl; ``` ```cpp #include <iostream> #include <string> using namespace std; int main() { // times = // 1 0 0 // 0 0 0 // 0 0 1 int times[3][3]; for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { cin >> times[i][j]; } } // j = 0 // i = 0 // a = 1 // times[i][j] = 1 // times[i + 1][j] = 0 // times[i - 1][j] // for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { int a = times[i][j]; if (i - 1 >= 0) { a += times[i - 1][j]; } if (j - 1 >= 0) { a += times[i][j - 1]; } if (i + 1 < 3) { a += times[i + 1][j]; } if (j + 1 < 3) { a += times[i][j + 1]; } is if(a % 2 == 1){ cout << 0; } else { cout << 1; } } cout << endl; } } ``` ```cpp #include <iostream> using namespace std; int main() { int ans[3][3] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}; int x; for (int j = 0; j < 3; ++j) { for (int i = 0; i < 3; ++i) { cin >> x; ans[i][j] += x; if (i != 0) ans[i - 1][j] += x; if (i != 2) ans[i + 1][j] += x; if (j != 0) ans[i][j - 1] += x; if (j != 2) ans[i][j + 1] += x; } } for (int j = 0; j < 3; ++j) { for (int i = 0; i < 3; ++i) { cout << ans[i][j] % 2; } cout << endl; } return 0; } ``` ```cpp #include <cctype> #include <iostream> using namespace std; int main() { string str; cin >> str; int upper = 0; int lower = 0; for (int i = 0; i < str.length(); i++) { if (isupper(str[i])) { upper++; } else if (islower(str[i])) { lower++; } } for (int j = 0; j < str.length(); j++) { if (lower >= upper) { if (isupper(str[j])) { str[j] = str[j] + 32; } } else { if (islower(str[j])) { str[j] = str[j] - 32; } } } cout << str << endl; } ``` ```cpp #include <cctype> #include <iostream> using namespace std; int main() { string str; cin >> str; for (int i = 0; i < str.length(); i++) { if (islower(str[0])) { str[0] = str[0] - 32; } } cout << str << endl; } ``` ```cpp #include <iostream> using namespace std; int main() { int i = 123; // the "length" of 0 is 1: int len = 1; // and for numbers greater than 0: if (i > 0) { // we count how many times it can be divided by 10: // (how many times we can cut off the last digit until we end up with 0) for (len = 0; i > 0; len++) { i = i / 10; } } // and that's our "length": std::cout << len; } ``` ```cpp #include <iostream> using namespace std; int main() { int x; cin >> x; x = abs(x); int n = 1; while(x /= 10) ++n; cout << n << endl; } ``` ```cpp #include <iostream> using namespace std; int main() { string s; cin >> s; for(int i = 0; i < s.length(); i++) { if(s[i] != '4' && s[i] != '7') { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; } ``` ```cpp #include <iostream> using namespace std; int main() { string s; cin >> s; bool ans = true; for (int i = 0; i < s.length(); i++) { if (s[i] != '4' && s[i] != '7') { ans = false; break; } } if (ans) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; } ``` https://cses.fi/problemset/task/1621 https://codeforces.com/problemset/problem/110/A ``` g++ -std=c++17 ``` ```cpp #include <iostream> using namespace std; int main() { string s; cin >> s; int count = 0; // 474700 for (int i = 0; i < s.length(); i++) { if (s[i] == '4' || s[i] == '7') { count++; } } if (count == 4 || count == 7) { cout << "YES" << endl; } else { cout << "NO" << endl; } } ``` ```cpp #include <iostream> using namespace std; int main() { int take = 0; int n; string s; cin >> n; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == s[i + 1]) { take++; } } cout << take << endl; } ``` ```cpp #include <iostream> using namespace std; int main() { int n; int m; int x[] = {2, 3, 5, 7}; cin >> n >> m; if (m == 3) { cout << "YES" << endl; return 0; } for (int j = 1; j < m - n; j++) { bool isPrime = true; for (int k = 0; k < 4; k++) { if (n + j <= x[k]) break; if ((n + j) % x[k] == 0) { isPrime = false; break; } } //n = 3 //m = 7 //j = 1 //isPrime = false //k = 0 //n + j = 4 //x[k] = 2 if (isPrime) { cout << "NO" << endl; return 0; } } if (m == 5 || m == 7) { cout << "YES" << endl; return 0; } if (m % 2 == 0 || m % 3 == 0 || m % 5 == 0 || m % 7 == 0) { cout << "NO" << endl; } else { cout << "YES" << endl; } return 0; } ``` https://www.sciencedaily.com/releases/2018/01/180104164507.htm https://en.wikipedia.org/wiki/Largest_known_prime_number https://zh.wikipedia.org/wiki/%E6%A2%85%E6%A3%AE%E7%B4%A0%E6%95%B0 https://zh.wikipedia.org/wiki/%E8%B2%BB%E9%A6%AC%E6%95%B8 ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n, m, a; cin >> n >> m; vector<int> v; for (int i = 1; i <= n; i++) { cin >> a; v.push_back(a); } int max1 = 0, max2, x, locate; for (int i = 0; i < n; i++) { x = v[i] / m; if (v[i] % m != 0) { max2 = x + 1; if (max2 >= max1) { max1 = max2; locate = i; } } else if (v[i] % m == 0) { max2 = x; if (max2 >= max1) { max1 = max2; locate = i; } } } cout << locate + 1 << endl; return 0; } ``` ``` cpp #include <iostream> #include <queue> using namespace std; int main() { int n, m, ans; cin >> n >> m; int a; queue<int> x, y; for (int i = 1; i <= n; i++) { cin >> a; x.push(a); y.push(i); } while (!x.empty()) { if (x.front() <= m) { x.pop(); y.pop(); } else { x.push(x.front() - m); y.push(y.front()); x.pop(); y.pop(); } } ans = y.back(); cout << ans << endl; return 0; } // 1 3 1 4 2 // m: 2 // x: 1 4 2 1 // y: 3 4 5 2 ``` 巴巴~貼個題目網址 等等看~ https://codeforces.com/problemset/problem/450/A https://codeforces.com/problemset/problem/165/A ```cpp bool compX(pair<int, int> a, pair<int, int> b) { if (a.first == b.first) { return a.second < b.second; } return a.first < b.first; } bool compY(pair<int, int> a, pair<int, int> b) { if (a.second == b.second) { return a.first < b.first; } return a.second < b.second; } int main() { int n; cin >> n; vector<pair<int, int>> vec(n); for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; vec[i] = {x, y}; } sort(vec.begin(), vec.end(), compX); for (int i = 0; i < vec.size(); ++i) { cout << vec[i].first << " " << vec[i].second << endl; } // ... sort(vec.begin(), vec.end(), compY); // ... } // 8 // 1 1 // 4 2 // 3 1 // 1 2 // 0 2 // 0 1 // 1 0 // 1 3 ``` ```cpp #include <iostream> #include <vector> #include <map> using namespace std; bool compX(pair<int, int> a, pair<int, int> b) { if (a.first == b.first) { return a.second < b.second; } else { return a.first < b.first; } } int main() { int n, x, y; cin >> n; vector<pair<int, int>> vec(n); for (int i = 0; i < n; i++) { cin >> x >> y; vec[i] = {x, y}; } sort(vec.begin(), vec.end(), compX); map <pair<int , int> > m; // 8 // 0 1 // 0 2 // 1 0 // 1 1 // 1 2 // 1 3 // 3 1 // 4 2 // if the times of duplicate number < 3 // ```