Project Euler P4 Largest palindrome product 题解

考虑暴力。

我们做两重循环,枚举乘数和被乘数,判断回文取 \(\max\) 即可。

时间复杂度 \(O(900^2)=O(810000)\)

#include <bits/stdc++.h>
using namespace std;
int a[10],tot,ans;
bool hw(int n) {
	memset(a,0,sizeof a);
	tot=0;
	while(n) {
		a[++tot]=n%10;
		n/=10;
	}
	for(int i=1;i<=tot/2;i++)
	    if(a[i]!=a[tot-i+1])
	        return 0;
	return 1;
}//分拆位数判定回文
int main() {
	for(int i=999;i>=100;i--)
	    for(int j=999;j>=100;j--)
	        if(hw(i*j))
	            ans=max(ans,i*j);//取最大值
	printf("%d",ans);
}

猜你喜欢

转载自www.cnblogs.com/lajiccf/p/12939624.html