Given A and B, count the numbers N such that A ≤ N ≤ B and N is a palindrome.
Examples:
Palindromes: 121, 11, 11411
Not Palindromes: 122, 10
Example:
Input: a = 10, b = 20
Output: 1
Approach
C++
#include <bits/stdc++.h>using namespace std;bool ispalindrome(int n){int res = 0;int y = n;while (y){res = res * 10 + y % 10;y = y / 10;}if (n == res)return true;return false;}int countPalindromes(int a, int b){int cnt = 0;for (int i = a; i <= b; i++){if (ispalindrome(i))cnt++;}return cnt;}int main(){int a = 10, b = 20;cout << countPalindromes(a, b) << "\n";return 0;}
No comments:
Post a Comment