project euler 4

来源:互联网 发布:简单软件的c语言程序 编辑:程序博客网 时间:2024/06/07 17:20

题目:

https://projecteuler.net/problem=4

题意:

Largest palindrome product
Problem 4
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.

求两个三位数的乘积中最大的回文数字

思路:

直接枚举两个三位数,判断它们的乘积是不是回文数,取乘积是回文数的最大值

代码:

#include <bits/stdc++.h>using namespace std;bool isPalindrome(int x){    int rx = 0, cx = x;    while(cx)    {        rx = rx * 10 + cx % 10;        cx /= 10;    }    return x == rx;}int main(){    int ans = 0;    for(int i = 100; i < 1000; ++i)        for(int j = 100; j < 1000; ++j)        {            if(isPalindrome(i * j))                ans = max(ans, i * j);        }    printf("%d\n", ans);    return 0;}
原创粉丝点击