15求优化QAQ
查看原帖
15求优化QAQ
811437
littlewhite_楼主2024/3/24 18:21

记录

#include <bits/stdc++.h>
using namespace std;

bool is_palindrome(int n)
{
	int ans = 0, cnt = 0;
	int res = n;
    while (n > 0) 
	{
        n /= 10;
        cnt++;
    }
    n = res;
    for (int i = 1; i <= cnt; i++) 
	{
        ans = ans * 10 + n % 10;
        n /= 10;
    }
    if (ans == res) 
	{
        return true;
    }
    return false;
} 

int main()
{
	int n;
	cin >> n;
	int cnt = 0;
	for (int i = 1; i <= n; i++)
	{
		if (is_palindrome(i)) cnt++;
	}
	cout << cnt;
	return 0;
} 
2024/3/24 18:21
加载中...