projecteuler.net 4--Largest palindrome product

本文介绍了一种通过编程方法寻找由两个三位数相乘得到的最大回文数的技术。使用C++实现了一个检查数字是否为回文数的函数,并通过遍历所有可能的三位数组合来找到符合条件的最大回文数及其因数。

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 <iostream>
#include <cmath>
bool check(int target)
{

    //std::cout<<target<<std::endl;
    int i=6;
    int j=0;
    int temp=0;
    int out=0;
    int hehe;
    hehe=target;
    if ((int)pow(10, i)>target)
    {

        //std::cout<<pow(10,i)<<std::endl;
        i--;
    }
    while(target!=0)
    {


            temp = target /(int)(pow(10, i));
            out=out+temp*pow(10,j);
            //std::cout<<out<<std::endl;
            target=target-temp*pow(10,i);
            i--;
            j++;

    }
    //std::cout<<out<<std::endl;
    if(out==hehe)
    {
        std::cout<<out<<" YES"<<std::endl;
        return (true);
    }
    else
        return(false);
}

int main2()
{
    //check(12345)==true;
    long sum,temp;
    int i,j;
    i=j=999;
    temp=0;
    int tempx,tempy;
    for(i=100;i<=999;i++)
    {
        for(j=100;j<=999;j++)
        {
            //std::cout<<i<<" "<<j<<std::endl;
            sum=i*j;
            if(check(sum)==true)
            {
                if(sum>=temp)
                {
                    temp = sum;
                    tempx=i;
                    tempy=j;
                }
            }
        }
    }
    std::cout<<temp<<" "<<tempx<<" "<<tempy<<" "<<std::endl;
}
int main()
{
    //check(906609);
    main2();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值