1,求出最长公共子序列,并判断是否可以进行火星。。。。(记不清了)
#include<vector>
#include<algorithm>
using namespace std;
int main() {
int N;
cin >> N;
char c;
vector<char>a;
vector<char>b;
vector<vector<int>>dp(N+1, vector<int>(N+1, 0));
for (int i = 0; i < N; i++) {
cin >> c;
a.push_back(c);
}
for (int i = 0; i < N; i++) {
cin >> c;
b.push_back(c);
}
for (int i = 0; i < N; i++) {
dp[0][i] = 0;
dp[i][0] = 0;
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
dp[i][j] = (a[i - 1] == b[j - 1]) ? dp[i - 1][j - 1] + 1 : max(dp[i][j - 1], dp[i - 1][j]);
}
}
int max_length=dp[N][N];
double result = double(max_length) / double(N);
if (result >= 0.5) {
cout << result << " " << "No"<<endl;
}
else {
cout << result << " " << "Yes" << endl;
}
return 0;
}
2 回文素数(判断区间【N,M】删除整数中哪一数可以成为回文素数,并记录个数)例如 整数110.在删除0之后,11既是素数,也是回文数。
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
bool isPrime(int num) {
if (num < 3) {
return num > 1;
}
for (int i = 2; i <= sqrt(num); i++) {
if (num%i == 0) {
return false;
}
}
return true;
}
bool isHW(int num) {
int revNum = 0;
int ans = num;
while (num) {
int temp = num % 10;
revNum = revNum * 10 + temp;
num /= 10;
}
return ans == revNum;
}
int main() {
int N, M;
cin >> N >> M;
int count = 0;
for (int i = N; i <= M; i++) {
auto temp = to_string(i);
auto len = temp.size();
for (int j = 0; j < len; j++) {
string now_temp(temp);
now_temp.erase(j, 1);
int now_num = atoi(now_temp.c_str());
if (isHW(now_num) && isPrime(now_num)) {
count++;
break;
}
}
}
cout << count;
return 0;
}