intmain(){ int a, b; vector<pair<int, int>> depositary; while (cin >> a) { if (a == -1) break; cin >> b; depositary.push_back(make_pair(a, b)); } for (int i = 0; i < depositary.size(); i++) { int days = 0; int food = depositary[i].first * depositary[i].second; while (food != 0) { food -= depositary[i].first; days++; while (food <= (depositary[i].first - 1) * depositary[i].second) { depositary[i].first--; } } cout << days << "\n"; } return0; }
string solve(string num, int len){ int n = stoi(num), a = 0, b = 0; switch (len) { case4: a = n / 100; b = n % 100; if (a > 9) a = (a / 10) * (a % 10); if (b > 9) b = (b / 10) * (b % 10); if (a == 0) returnto_string(b); num = to_string(a) + to_string(b); return num; case3: a = (n / 100) * ((n / 10) % 10); b = ((n / 10) % 10) * (n % 10); num = to_string(a) + to_string(b); return num; case2: a = n / 10; b = n % 10; num = to_string(a * b); return num; default: return"0"; } return"0"; }
intmain(){ string num; cin >> num;
while (num.size() != 1) { num = solve(num, num.size()); } cout << num; return0; }
int m, a; cin >> m >> a; int T[m]; for (int i = 0; i < m; i++) { cin >> T[i]; } for (int i = 0; i < m; i++) { if (a <= T[i]) break; a += T[i]; } cout << a << "\n";
voidsolve(int *data, int n, int qq){ int left = 0; int right = n; while (left <= right) { int mid = (left + right) / 2; if (data[mid] < qq) { left = mid + 1; } elseif (data[mid] > qq) { right = mid - 1; } else { cout << mid + 1 << "\n"; return; } } cout << 0 << "\n"; return; }
intmain(){ int n, k; cin >> n >> k; int data[n], q[k]; for (int i = 0; i < n; i++) { cin >> data[i]; } for (int i = 0; i < k; i++) { cin >> q[i]; } for (int i = 0; i < k; i++) { solve(data, n, q[i]); } return0; }
intmain(){ ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; longlong rock[n], robot[m]; for (int i = 0; i < n; i++) cin >> rock[i]; for (int i = 0; i < m; i++) cin >> robot[i]; sort(rock, rock + n); sort(robot, robot + m);
longlong sum = 0; int index_bot = m - 1; for (int i = n - 1; i >= 0 && index_bot >= 0; i--) { if (robot[index_bot] >= rock[i]) { sum += rock[i]; index_bot--; } } cout << sum << "\n"; return0; }
int N, M; while (cin >> N >> M) { vector<int> d[N]; queue<int> q; bool check[800] = {false}; for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a--; b--; d[a].push_back(b); } // 將能走的路徑都設為 true int A, B; cin >> A >> B; A--; B--; q.push(A); check[A] = true; while (!q.empty()) { int temp = q.front(); q.pop(); for (auto &i : d[temp]) { if (!check[i]) { check[i] = true; q.push(i); } } } // 訪問 B 點是否能走到 if (check[B]) { cout << "Yes!!!\n"; } else { cout << "No!!!\n"; } } return0; }
vector<int> vec; vec.resize(10, 5); for (auto &v : vec) { cout << v << " "; }
1 2
輸出: 5 5 5 5 5 5 5 5 5 5
課堂小考
對空的整數型態 vector 使用 resize(10.5, 3.3) 後,capacity() 變為多少,且元素設定為多少。
A. capacity: 11,且元素皆為 4
B. capacity: 11,且元素皆為 3
C. capacity: 10,且元素皆為 3
D. capacity: 10,且元素皆為 4
E. Run Time Error