2336
cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <cstdio>
class Solution {
public:
Solution() {
std::cin >> n;
number1 = new int[n];
for (int i = 0; i < n; i++) scanf("%d", &number1[i]);
std::cin >> m;
number2 = new int[m];
for (int i = 0; i < m; i++) scanf("%d", &number2[i]);
}
int binarySearch(int arr[], int target, int left, int right) {
if (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == target) {
return 1;
}
else if (arr[mid] < target) {
return binarySearch(arr, target, mid + 1, right); // 修正更新边界值
}
else {
return binarySearch(arr, target, left, mid - 1); // 修正更新边界值
}
}
return 0;
}
void solve() {
bool found = false; // 添加一个变量来标记是否找到匹配的数字对
for (int i = 0; i < m; i++) {
if (binarySearch(number1, 10000 - number2[i], 0, n - 1)) {
std::cout << "YES"; // 添加换行符
found = true; // 标记找到匹配的数字对
break; // 找到即可跳出循环
}
}
if (!found) {
std::cout << "NO"; // 如果没有找到匹配的数字对,输出 "NO"
}
}
int n, m;
int* number1, * number2;
};
int main()
{
Solution a;
a.solve();
return 0;
}
2503
cpp
#include <iostream>
#include <cstring>
#include <string>
#include <map>
class Solution {
private:
std::map<std::string, std::string> dictionary;
// 解析输入行并更新字典的私有辅助函数
void parseAndUpdateDictionary(const std::string& str) {
std::string str1, str2;
for (size_t i = 0; i < str.size(); i++) {
if (str[i] == ' ') {
str1 = str.substr(0, i);
str2 = str.substr(i + 1);
break;
}
}
dictionary[str2] = str1;
}
public:
// 构造函数,初始化字典
Solution() {}
// 读取字典输入并初始化字典的公有方法
void readDictionary() {
std::string str;
while (std::getline(std::cin, str) && !str.empty()) {
parseAndUpdateDictionary(str);
}
}
// 翻译外语单词的公有方法
void translateWords() {
std::string word;
while (std::cin >> word) {
if (!dictionary[word].empty()) {
std::cout << dictionary[word] << std::endl;
}
else {
std::cout << "eh\n";
}
}
}
};
int main() {
// 创建 Solution 对象
Solution solution;
// 读取字典输入并初始化字典
solution.readDictionary();
// 翻译外语单词
solution.translateWords();
return 0;
}