2336
#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
#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;
}
3714
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
double x;
double y;
int type;
}a[200005];
int n;
vector<node> temp;
bool cmp1(node &a,node &b)
{
return a.x<b.x;
}
bool cmp2(node &a,node &b)
{
return a.y<b.y;
}
double solve(int l,int r)
{
if(l==r) return 100000000000;
if(l==r-1&&a[l].type!=a[r].type)
{
return sqrt(pow(fabs(a[l].x-a[r].x),2)+pow(fabs(a[l].y-a[r].y),2));
}
if(l==r-1&&a[l].type==a[r].type)
{
return 100000000000;
}
int mid = l + r >> 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)<d) temp.push_back(a[i]);
}
sort(temp.begin(),temp.end(),cmp2);
int j;
for(i=0;i<temp.size();i++)
{
for(j=i+1;j<temp.size()&&temp[j].y-temp[i].y<d;j++)
{
if(temp[i].type==temp[j].type) continue;
double d3=sqrt(pow(fabs(temp[i].x-temp[j].x),2)+pow(fabs(temp[i].y-temp[j].y),2));
d=min(d,d3);
}
}
temp.clear();
return d;
}
int main()
{
int t;
int i;
cin>>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<iostream>
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 <iostream>
#include <cstring>
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 <stdio.h>
#include <string.h>
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;
}
2506
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Fibonacci {
string num;
Fibonacci operator+(Fibonacci& other) {
int lenOther = other.num.size();
int len = num.size();
int i;
int carry = 0;
string result;
reverse(num.begin(), num.end());
reverse(other.num.begin(), other.num.end());
for (i = 0; i < max(lenOther, len) + 1; i++) {
char ca, cb;
if (i >= len)
ca = '0';
else
ca = num[i];
if (i >= lenOther)
cb = '0';
else
cb = other.num[i];
char cc = cb - '0' + ca - '0' + carry;
carry = 0;
if (cc >= 10) {
carry = 1;
cc -= 10;
}
if (i == max(len, lenOther) && cc == 0)
continue;
result.push_back(cc + '0');
}
reverse(num.begin(), num.end());
reverse(other.num.begin(), other.num.end());
reverse(result.begin(), result.end());
return Fibonacci(result);
}
Fibonacci(const string& str) {
num = str;
}
void operator=(Fibonacci other) {
num = other.num;
}
Fibonacci() {}
};
int main() {
Fibonacci fibonacci[505];
int n;
fibonacci[0] = Fibonacci("1");
fibonacci[1] = Fibonacci("1");
fibonacci[2] = Fibonacci("3");
int i;
for (i = 3; i <= 255; i++) {
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2] + fibonacci[i - 2];
}
while (cin >> n)
cout << fibonacci[n].num << endl;
return 0;
}