poj.org 部分答案(二)

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;
}
相关推荐
oioihoii1 小时前
C++网络编程:从Socket混乱到优雅Reactor的蜕变之路
开发语言·网络·c++
程序员东岸1 小时前
《数据结构——排序(中)》选择与交换的艺术:从直接选择到堆排序的性能跃迁
数据结构·笔记·算法·leetcode·排序算法
程序员-King.1 小时前
day104—对向双指针—接雨水(LeetCode-42)
算法·贪心算法
笨鸟要努力1 小时前
Qt C++ windows 设置系统时间
c++·windows·qt
牢七1 小时前
数据结构11111
数据结构
神仙别闹2 小时前
基于C++实现(控制台)应用递推法完成经典型算法的应用
开发语言·c++·算法
Ayanami_Reii2 小时前
进阶数据结构应用-一个简单的整数问题2(线段树解法)
数据结构·算法·线段树·延迟标记
listhi5202 小时前
基于改进SET的时频分析MATLAB实现
开发语言·算法·matlab
Keep_Trying_Go3 小时前
基于Zero-Shot的目标计数算法详解(Open-world Text-specified Object Counting)
人工智能·pytorch·python·算法·多模态·目标统计
xl.liu3 小时前
零售行业仓库商品数据标记
算法·零售