方法一(string):
#include <iostream>
#include <string>
using namespace std;// 检查是否为回文数
bool isPalindrome(int n) {string str = to_string(n);int left = 0, right = str.size() - 1;while (left < right) {if (str[left] != str[right]) {return false;}left++;right--;}return true;
}// 检查是否包含数字7
bool containsSeven(int n) {string str = to_string(n);return str.find('7') != string::npos; // 找到字符 '7' 返回 true
}int main() {int a, b;cin >> a >> b;int count = 0;for (int i = a; i <= b; ++i) {if (isPalindrome(i) && containsSeven(i)) {count++;}}cout << count << endl;return 0;
}
方法二:(数字翻转)
#include<bits/stdc++.h>
using namespace std;
int huiwen(int n){//判断是否回文int a=n,b=0;while(n){b=b*10+n%10;n/=10;}return b==a;
}
int check(int n){//判断是否含7while(n){if(n%10==7) return 1 ;n/=10;}return 0;
}
int main(){int a,b,cnt=0;cin>>a>>b;for(int i=a;i<=b;i++){if(huiwen(i) && check(i)) cnt++;}cout<<cnt;return 0;
}