poj.org 部分答案(二)

2336

#define _CRT_SECURE_NO_WARNINGS
#include
#include 
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

#include 
#include 
#include 
#include 

class Solution {
private:
    std::map 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;
}

3714 

#include
#include
#include
#include
using namespace std;
struct node
{
	double x;
	double y;
	int type;
}a[200005];
int n;
vector temp;
bool cmp1(node &a,node &b)
{
	return a.x> 1;
    double d1 = solve(l, mid);
    double d2 = solve(mid + 1, r);
    double d = min(d1, d2);
	int i;
	for(i=l;i<=r;i++)
	{
		if(fabs(a[i].x-a[mid].x)>t;
	while(t--)
	{
		int n;
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>a[i].x>>a[i].y;
			a[i].type=1;
		}
		for(i=n+1;i<=2*n;i++)
		{
			cin>>a[i].x>>a[i].y;
			a[i].type=2;
		}
		sort(a+1,a+2*n+1,cmp1);
		double ans=solve(1,2*n);
		printf("%.3lf\n",ans);
	}
	return 0;
}

3233 

这个题目用记忆算法的话会爆
 

#include
using namespace std;

class Solution {
public:
    Solution() {
        cin >> n >> k >> m;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cin >> contain[i][j];
                if(i == j)  tmp[i][j] = 1;
                else tmp[i][j] = 0;
                sum[i][j] = 0;
            }
        }
    }

    void mul() {
        int t[31][31];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                t[i][j] = 0;
                for (int x = 0; x < n; x++) {
                    t[i][j] = (t[i][j] + tmp[i][x] * contain[x][j]) % m;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                tmp[i][j] = t[i][j];
                sum[i][j] = (sum[i][j] + tmp[i][j]) % m;
            }
        }
    }

    void fun() {
        for (int i = 0; i < k; i++) {
            mul();
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << sum[i][j] << " ";
            }
            cout << "\n";
        }
    }

private:
    int n, k, m;    // 进行 k 次 
    int contain[31][31];
    int tmp[31][31];
    int sum[31][31];
};

int main() {
    Solution a;
    a.fun();
    system("pause");
    return 0;
}

我们改用快速幂

#include 
#include 

class Solution {
private:
    int b[65][65];
    int x[65][65];
    int n, k, m;

    void mul(int a[65][65], int x[65][65]) {
        int i, j, k;
        int t[65][65];
        memset(t, 0, sizeof(t));
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                for (k = 0; k < n; k++) {
                    t[i][j] = (t[i][j] + a[i][k] * x[k][j]) % m;
                }
            }
        }
        memcpy(a, t, sizeof(t));
    }

public:
    void solve() {
        int i, j;
        std::cin >> n >> k >> m;
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                std::cin >> x[i][j];
            }
        }
        for (i = 0; i < n; i++) {
            x[i + n][i] = 1;
            x[i + n][i + n] = 1;
            b[i][i] = 1;
            b[i][i + n] = 1;
        }
        n <<= 1;
        for (i = 0; (1 << i) <= k; i++) {
            if (k & (1 << i)) {
                mul(b, x);
            }
            mul(x, x);
        }
        n >>= 1;
        for (i = 0; i < n; i++) {
            b[i][i] = (b[i][i] - 1 + m) % m;
            for (j = 0; j < n - 1; j++) {
                std::cout << b[i][j] << " ";
            }
            std::cout << b[i][n - 1] << std::endl;
        }
    }
};

int main() {
    Solution solution;
    solution.solve();
    return 0;
}
#include 
#include 
int b[65][65], x[65][65];
int n, k, m;
void mul(int a[65][65], int x[65][65])
{
    int i, j, k;
    int t[65][65];
    memset(t, 0, sizeof(t));
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            for (k = 0; k < n; k++)
                t[i][j] = (t[i][j] + a[i][k] * x[k][j]) % m;
    memcpy(a, t, sizeof(t));
}
int main()
{
    int i, j;
    scanf("%d%d%d", &n, &k, &m);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            scanf("%d", &x[i][j]);
    for (i = 0; i < n; i++)
    {
        x[i + n][i] = 1;
        x[i + n][i + n] = 1;
        b[i][i] = 1;
        b[i][i + n] = 1;
    }
    n <<= 1;
    for (i = 0; (1 << i) <= k; i++)
    {
        if (k & (1 << i))
            mul(b, x);
        mul(x, x);
    }
    n >>= 1;
    for (i = 0; i < n; i++)
        b[i][i] = (b[i][i] - 1 + m) % m;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n - 1; j++)
            printf("%d ", b[i][j]);
        printf("%d\n", b[i][n - 1]);
    }
    return 0;
}

 

你可能感兴趣的:(算法,c++,数据结构)