Palindromic Numbers

Problem 1: Palindromic Numbers
A palindromic number is one that reads the same both ways, from the beginning or from the
end. For example, 525 and 8448 are palindromic numbers. Note that 525 and 8448 are also
products of two 2-digit numbers: 525 = 15 * 35 and 8448 = 96 * 88. On the other hand, 7777
is a palindromic number but not a product of two 2-digit numbers.
Write a C++ program that prompts the user for two integers M and N and output numbers in
the range of M and N (inclusively) which are either (1) palindromic only; (2) product of two
3-digit numbers only; or (3) both palindromic and product of two 3-digit numbers.
Input:
• A single line containing two integers M , N , and a char opt for the display
option. • You may assume that M <= N and both numbers are within the range 10000 to
999999 (inclusively), and opt must be one of the chars 'p' , 't' , 'b' .
Output:
• If the input opt is the char 'p' , then output the palindromic numbers in the
range of M and N (inclusively) in increasing order .
• If the output opt is the char 't' , then output the numbers which are a product
of two 3-digit numbers in the range of M and N (inclusively) in increasing order .
• If the input opt is the char 'b' , then output the numbers which are both
palindromic and a product of two 3-digit numbers in the range of M and N
(inclusively) in increasing order .
• If there is no number within the range that matches the criteria, no line will be
output.
Requirement:
• Your program must implement the following two helper functions:
bool isPalindrome(int x) which returns whether the parameter x
is a palindromic number.
bool isProduct(int x) which returns whether the parameter x is
a product of two 3-digit numbers.
• Call the helper functions in your main function to accomplish the task.
• You can ONLY use the simple data types char , bool , int , double . In other
words, you are not allowed to use other data types or data structures such as
arrays, strings or STL containers (e.g., vectors), etc.
Sample Test Cases
User inputs are shown in blue .
1_1
10000 10300 p
10001
10101
10201 1_2
10000 10300 t
10000
10100
10200
10201
10300
1_3
10000 10300 b
10201
1_4
99000 110000 b
99099
99199
99299
99599
99699
99799
99899
99999
101101
102201
105501
106601
108801
1_5 ( Note: there is no output in this test case )
100000 101000 b
最长回文子序列(Longest Palindromic Subsequence,LPS)问题是指在一个给定的字符串中找到一个最长的回文子序列。回文子序列是指一个序列本身不是回文串,但它是一个回文串的子序列。 在C++中,我们可以使用动态规划(Dynamic Programming,DP)的方法来解决这个问题。动态规划的主要思想是将一个大问题分解成小问题,然后从小问题出发,逐渐求得大问题的解。 以下是一个使用动态规划解决最长回文子序列问题的C++示例代码: ```cpp #include <iostream> #include <vector> #include <string> using namespace std; // 函数用于计算字符串str的最长回文子序列的长度 int longestPalindromeSubseq(string str) { int n = str.size(); // 创建一个二维数组dp,用于存储子问题的解,初始化所有值为0 vector<vector<int>> dp(n, vector<int>(n, 0)); // 单个字符的最长回文子序列长度为1,所以对角线上的元素设置为1 for (int i = 0; i < n; i++) { dp[i][i] = 1; } // 如果两个字符相同,那么它俩组成的子序列长度为2 for (int cl = 2; cl <= n; cl++) { for (int i = 0; i < n - cl + 1; i++) { int j = i + cl - 1; if (str[i] == str[j] && cl == 2) { dp[i][j] = 2; } else if (str[i] == str[j]) { dp[i][j] = dp[i + 1][j - 1] + 2; } else { dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]); } } } // 返回整个字符串的最长回文子序列长度 return dp[0][n - 1]; } int main() { string str; cout << "请输入一个字符串:" << endl; cin >> str; cout << "最长回文子序列的长度为:" << longestPalindromeSubseq(str) << endl; return 0; } ``` 在这段代码中,`dp[i][j]`表示从字符串的第`i`个字符到第`j`个字符组成的子串的最长回文子序列的长度。通过初始化对角线以及递推式逐步填充这个二维数组,最终可以得到整个字符串的最长回文子序列长度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值