Open sx349 opened 4 years ago
# 999 999
# 998 899
# ...
for i in range(999, 100, -1):
num_temp = i%10*100 + (i//10)%10*10 + i//100
num = i * 1000 + num_temp
for i in range(999, 100, -1):
if num%i == 0:
if num//i <= 999 and num//i >= 100:
print(i, num//i, num)
break
else:
continue
break
def huiwen( N ):
strN = str(N)
lens = int(len(strN))
half = int(lens / 2)
for x in range(half ):
if strN[x] != strN[-x-1]:
return False
return True
max = 0
for x in range(999,100,-1):
for y in range(999,100,-1):
z = x * y
if huiwen(z):
if max < z:
print("{} * {} = {} 是回文".format(x, y, z))
max = z
print(max)
995 * 583 = 580085 是回文
993 * 913 = 906609 是回文
906609
public class Problem4 { public static void main(String[] args) { String s; for(int i = 998001;i >= 100000;i--) { s = String.valueOf(i); if(s.charAt(0) == s.charAt(5) && s.charAt(1) == s.charAt(4) && s.charAt(2) == s.charAt(3)) { // System.out.println(i); for(int j = 100;j <= 999;j++) { int k = i / j; int len = String.valueOf(k).length(); if(i % j == 0 && len == 3) { System.out.println(i + "," + j + "," + k); return; } } } } } }
先写个判断回文数的谓词:
palindromeQ[x_]:=(IntegerDigits@x === Reverse@IntegerDigits@x)
用 Outer
构建所有三位数相乘可能的结果构成的列表,可以用 DeleteDuplicates
去重:
Outer[Times, Range[100, 999], Range[100, 999]] //Flatten //DeleteDuplicates
再筛一遍,取最大即可
Cases[%, _?palindromeQ] //Max
用时 0.34s (用 AbsoluteTiming
计时)
palindromeQ :: Int -> Bool
palindromeQ n = n_digits == reverse n_digits
where n_digits = show n
main = do
print . maximum $ filter palindromeQ [x*y | x <- [100..999], y <- [100..999]]
用时 0.07s ,Haskell yyds!(终端使用 time
计时)
using namespace std;
int func(int num) { int x = num, y = 0; while (num) { y = 10 * y + num % 10; num /= 10; } if (x == y) return 1; return 0; }
int main() { int ans = 1; for (int i = 100; i <= 999; i++) { for (int j = 100; j <= 999; j++) { if (func(i j) && ans < (i j)) { ans = i * j; } } } cout << ans << endl; return 0; }
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int func(int num) {
int x = num, y = 0;
while (num) {
y = 10 * y + num % 10;
num /= 10;
}
if (x == y) return 1;
return 0;
}
int main() {
int ans = 1;
for (int i = 100; i <= 999; i++) {
for (int j = 100; j <= 999; j++) {
if (func(i * j) && ans < (i * j)) {
ans = i * j;
}
}
}
cout << ans << endl;
return 0;
}
using namespace std; int a[7]; int recycle(int i) {int m; for( m=0;;m++) { if(i==0) break; if(i>0) {a[m]=i%10; i=i/10; }
} for(int q=0;q<=(m-1)/2;q++) { if(a[q]!=a[m-1-q]) return 0; }
return 1;
} int main() { int ans; for(int i=999;i>=100;i--) { for(int j=999;j>=i;j--) {if(recycle(ij)&&ans<ij) { ans=i*j; } }
} cout<<ans<<endl; return 0; }
def is_palindrome(n): if str(n) == str(n)[::-1]: return True
a, b, max = 100, 100, 10000 for i in range(100,1000): for j in range(100, 1000): p = i*j if is_palindrome(p): if p >= max: a, b, max =i, j, p
print(a,b,max)
using namespace std;
int func(int x) { int raw = x, t = 0; while(x) { t = t * 10 + x % 10; x /= 10; } return raw == t; }
int main() { int ans = 0; for (int i = 100; i < 1000; i++) { for (int j = i; j < 1000; j++) { if (func(i j)) { ans = max(ans, i j); } } } cout << ans << endl; return 0; }
using namespace std;
int func(int x) { int raw = x, t = 0; while (x) { t = t * 10 + x % 10; x /= 10; } return raw == t; }
int main() { int ans = 0; for (int i = 100; i < 1000; i++) { for (int j = i; j < 1000; j++) { if (func(i j)) { ans = max(ans, i j); } } } cout << ans << endl; return 0; }
bool huiwen(int n){
int a[6];
int j=0;
int i=0;
while(n!=0){
if(n/10!=0){
a[i]=n%10;
n=n/10;
i++;
}else{
a[i]=n;
n=n/10;
}
}
while(j<=i){
if(a[j]==a[i-j]){
j++;
}else{
return false;
}
} return true; }
int main(){
int res=0;
for(int i=100;i<1000;i++){
for(int j=100;j<1000;j++){
if(huiwen(i*j)){
if(i*j>=res){
res=i*j;
}
}
}
}
printf("%d",res);
return 0;
}
print(max((i for i in (x*y for x in range(100,999) for y in range(100,999)) if str(i)[::-1]==str(i))))
using namespace std;
stringstream ss;
string s;
bool isPalindrome (int x){
ss << x;
ss >> s;
ss.clear();
int len = s.size();
for (int i = 0; i < len/2; ++i){
if (s[i] != s[len-i-1]) return false;
}
return true;
}
int main(){
vector<int> v;
for (int i = 999;i >= 100; --i){
for (int j = 999; j >= 100; --j){
if (isPalindrome(i*j)) v.push_back(i*j);
}
}
sort(v.begin(), v.begin()+v.size());
cout << v[v.size()-1];
return 0;
}
// 最大回文乘积
#include <iostream>
using namespace std;
// ①
bool check(int x) {
int t = x;
int digit = 0;
while (t) {
digit = digit * 10 + t % 10;
t /= 10;
}
return digit == x;
}
int main() {
// int ans = 0;
// for (int i = 100; i <= 999; i++) {
// for (int j = 100; j <= 999; j++) {
// int t = i * j;
// if (check(t)) {
// ans = max(ans, t);
// }
// }
// }
// cout << ans << endl;
// `abccba` = 10^5a + 10^4b + 10^3c + 10^2c + 10b + a
// = 100000a + 10000b + 1000c + 100c + 10b + a
// = 100001a + 10010b + 1100c
// = 100001a + 10010b + 1100c
// = 11(9091a + 910b + 100c)
// 因此这个6位数是11的倍数
int ans = 0;
for (int i = 999; i >= 100; i--) {
for (int j = 999; j >= 100; j--) {
int t = i * j;
if (t % 11 ||!check(t)) continue;
ans = max(t, ans);
}
}
cout << ans << endl;
return 0;
}
using namespace std;
int a;
int equality(int x) { int p = x,t=0; while(x) { t = t*10+x%10; x/=10; }
return t == p;
}
int main() { int res = 0; for(int i = 100;i<1000;i++) { for(int j = i;j<1000;j++) { a=i*j; if(equality(a)) { res = max(res,a); } } }
cout<<res;
return 0;
}
for i in range(999,900,-1): for j in range(999,900,-1): n=i*j if str(n)==str(n)[::-1]: print(i,j,n) quit()
m = Table[a*b, {a, 100, 999}, {b, 100, 999}];
n = Table[ If[IntegerReverse[m[[i, j]]] == m[[i, j]], m[[i, j]], 0], {i, 1, 900}, {j, 1, 900}];
p = Table[Max[n[[k]]], {k, 1, 900}];
Max[p]
using namespace std;
int m[10];
bool HuiWen(int num)
{
int count=0;
int temp=num;
while(temp!=0)
{
m[count++]=temp%10;
temp/=10;
}
for(int i=0;i<count/2;++i)
{
if(m[i]!=m[count-1-i]) return false;
}
return true;
}
int main()
{
int result=0;
int k;
for(int i=101;i<=999;++i)
{
for(int j=101;j<=999;++j)
{
k=i*j;
if(HuiWen(k))
{
if(k>result) result=k;
}
}
}
cout<<result;
return 0;
}
int func(int x) { int raw = x, t = 0; while (x != 0) { t = t * 10 + x % 10; x /= 10; } return raw == t; }
int main() { int ans = 0; for (int i = 100; i < 1000; i++) { for (int j = i; j < 1000; j++) { if (func(i j) && i j > ans) { ans = i * j; } } } printf("%d\n", ans); return 0; }
#include <stdio.h>
_Bool isPa(int x);
int main()
{
int x = 999;
while (1) {
if (isPa(x * x)) {
printf("%d", x * x);
break;
}
if (isPa(x * (x - 1))) {
printf("%d", x * (x - 1));
break;
}
--x;
}
return 0;
}
_Bool isPa(int x)
{
int y = 0, z = x;
do {
y = y * 10 + x % 10;
} while ((x /= 10) > 0);
return z == y;
}
.....................................................................................................................................
698896
package oulajihua;
public class 回文数 { public static void main(String[] args) { int max = 0; for(int i = 100; i <= 999; i++) { for(int j = 100; j <= 999; j++) { if(max <= i j && isHuiWenShu(i j + "")) { max = i * j; } } } System.out.println(max); } public static boolean isHuiWenShu(String str) { StringBuffer sb = new StringBuffer(str); StringBuffer s2 = sb.reverse(); String s3 = s2.toString(); boolean flag = s3.equals(str); return flag; } }
// ans = 906609
class P4 {
public static Integer flip(Integer i) {
return Integer.parseInt(String.valueOf(new StringBuilder(String.valueOf(i)).reverse()));
}
public static void solve() {
int max = 0;
for (int i = 999; i >= 100; i--) {
for (int j = 999; j >= 100; j--) {
Integer tmp = i * j;
Integer tmpFlip = flip(tmp);
if (tmp.equals(tmpFlip)) {
max = Math.max(tmp, max);
}
}
}
System.out.println(max);
}
}
public class T04 {
public static void main(String[] args) {
int ans = 0;
for (int i = 999; i >= 100; i--) {
for (int j = 999; j >=100 ; j--) {
if (isPal(i * j) && i*j > ans){
ans = i * j;
}
}
}
System.out.println(ans); //906609
}
public static boolean isPal(int n){
StringBuffer s1 = new StringBuffer(String.valueOf(n));
if (s1.reverse().toString().equals(String.valueOf(n))){
return true;
}
return false;
}
}
using namespace std; int sum,res; int raw(int x) { int store = x, t = 0; while (1) { t = t 10 + x % 10; x /= 10; if (x == 0) break; } return t == store; } int main() { for (int i = 900;i <= 999;i++) { for (int j = 900;j <= 999;j++) { sum = i j; if (raw(sum)) res = i * j; } } cout << res; return 0; }
def palindrome_num (num): str_num = str(num) a = len(str_num) i = 0 while i <=a//2: if str_num[i] == str_num[a-1-i]: i += 1 else: return False break return True
p_num = [] for m in range(999,99,-1): for n in range(m,99,-1): num = m*n if palindrome_num(num): p_num.append(num) break print(max(p_num))
using namespace std;
int func(int x) { int t = 0, row = x; while(x) { t = t * 10 + x % 10; x /= 10; } return t == row; }
int main() { int ans = 0; for(int i = 100; i < 1000; i++) { for(int j = i ; j < 1000; j++) { int t = i * j; if(func(t)){ cout << t << endl; ans = max(ans,t); } } } cout << ans << endl; return 0; }
Select[Flatten[Transpose[{Range[100, 999]}] . {Range[100, 999]}],
IntegerDigits[#] == Reverse[IntegerDigits[#]] &] // Max
Select[Table[i*j, {i, 100, 999}, {j, 100, 999}] // Flatten,
IntegerReverse[#] == # &] // Max
bool check(ll x) { ll y=0,p=x; while(p) { y=y10+p%10; p=p/10; } return x==y; } int main() { ll ans=0; for(ll i=100;i<=999;i++) { for(ll j=100;j<=999;j++) { if(ij>ans&&check(ij))ans=ij; } } printf("%lld\n",ans); }
偷懒,不想用数学方法
using namespace std;
string reverse1(string a){ int left=0; int right=a.length()-1; while(left<right){ char temp=a[left]; a[left]=a[right]; a[right]=temp; left++; right--; } return a; }
int main(){
vector
int main() { int i,j,m,n=0,p; char a[7]; for(i=998001;i>0;i--) { m=i; itoa(i,a,10); if(a[0]==a[5]&&a[1]==a[4]&&a[2]==a[3]) { for(j=999;j>0;j--) { if(m%j==0) { if(j>100&&m/j<1000) { n=1; p=j; break; } else break; } } if(n==1) { printf("%d %d %s\n",j,m/j,a); break; } } } }
bool isHuiwenOrNot(int *arr, int bit) { bool flag = true;
for (int i = 0; i < bit; i++)
{
if (arr[i] != arr[bit - 1 - i])
flag = false;
}
return flag;
}
int main()
{
int a, b;
int maxSum = 0;
for (int i = 100; i < 1000; i++)
{
for (int j = 100; j < 1000; j++)
{
int sum = i j;
int bit = 0;
int arr[6]; // the maximum bit is less than 6(999x999)
while (sum != 0)
{
arr[bit] = sum % 10;
sum /= 10;
bit++;
}
if (isHuiwenOrNot(arr, bit) && ij > maxSum)
{
maxSum = i*j;
a = i;
b = j;
}
}
}
printf("the maximum HuiWen number is %d, which is multiplied by %d and %d", maxSum, a, b);
return 0;
}
int main() { printf("%d",999*999); }
int main() { int max=111; for(int i=1;i<10;i++) for(int j=1;j<10;j++) for(int k=1;k<10;k++) { if(i==j&&j==k) { if(i(1010)+j(10)+k1>=max) max=i(1010)+j(10)+k1; } } printf("%d",max*max); }
int main() { int max=111; for(int i=1;i<10;i++) for(int j=1;j<10;j++) for(int k=1;k<10;k++) { if(i==j&&j==k) { if(i(1010)+j(10)+k1>=max) max=i(1010)+j(10)+k1; } } printf("%d",maxmax); }
Julia
global Sets = Set([])
for i ∈ 9:-1:1
for j ∈ 9:-1:1
for k ∈ 9:-1:1
for l ∈ 9:-1:1
pro = string((900+i*10+j)*(900+k*10+l))
if pro == pro[end:-1:1]
global Sets = union(Sets,parse(Int,pro))
end
end
end
end
end
println("maximum palindrome product is ",maximum(Sets))
# maximum palindrome product is 906609
from count_time import count_time
def is_palidromem(num):
return str(num) == str(num)[::-1]
@count_time
def largest_palindrome_product(num: int) -> int:
largest_palindrome = 0
num_range = range(10**num-1, 10**(num-1)-1, -1)
for i in num_range:
for j in num_range:
res = i * j
if res < largest_palindrome:
break
if is_palidromem(res) and res > largest_palindrome:
largest_palindrome = res
largest_i = i
largest_j = j
return f"{largest_palindrome} = {largest_i} x {largest_j}"
if __name__ == '__main__':
largest_palindrome = largest_palindrome_product(3)
print(largest_palindrome)
#include <stdio.h>
#define MAX_N 1000
int isPalindromic(int num) {
int _num = num, p = 0;
while (_num) {
p = p * 10 + _num % 10;
_num /= 10;
}
return p == num;
}
int main() {
int maxN = 0;
for (int i = 100; i < MAX_N; i++) {
for (int j = i; j < MAX_N; j++) {
if (isPalindromic(i * j) && (i * j) > maxN) {
maxN = i * j;
}
}
}
printf("%d\n", maxN);
return 0;
}
using namespace std;
/ Largest palindrome product 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. 最大回文乘积 回文数是指从前往后读和从后往前读都一样的数。由两个2位数相乘得到的回文数中,最大的是 9009 = 91 x 99 求最大的由两个3位数相乘得到的回文数 /
bool isHuiwen(int num) { int ori_num = num; int Hui_num = 0;
while (num > 0)
{
Hui_num = Hui_num * 10 + num % 10;
num /= 10;
}
return (Hui_num == ori_num);
}
int main() { int max = 0; for (int i = 999; i >= 100; i--) { for (int j = 999; j >= 100; j--) { if (isHuiwen(i j) && i j > max) { max = i * j; //break; } } }
cout << max << endl;
return 0;
}
思路点拨:
主要思考要点是,如何判断这个数是回文数,第一反应是转成字符串,然后首尾比对,看了其他人题解后发现可以用数学方法判断:
原理大致为:任何一个数除以10的余数就是该数最后一位;任何一个数除以10的商就是排除掉最后一位后的数;所以 ,一个数1234 就可以通过这种方法得到 123 和 4;接下来对 123 进行同样的操作,就得到 12 和 3;接下来得到 1 和 2;接着得到 0 和 1;整个过程是个循环,当商不是 0 的时候就一直如此;每次拿到一个余数,都用来构造新数,新数=新数*10+余数;所以经过四次循环后,我们得到新数 4321 ,如果是回文,那么新数应该等于原数,否则,说明不是回文。
我的代码:
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(int num)
{
int neww = 0;
int old = num;
while(num > 0)
{
neww = neww * 10 + num % 10;
num /= 10;
}
return neww == old;
}
int main()
{
int maxx = 0;
int num;
for(int i = 100; i < 1000; i ++)
{
for(int j = i; j < 1000; j ++)
{
num = i * j;
if(isPalindrome(num) && num > maxx)
{
maxx = num;
}
}
}
printf("%d", maxx);
return 0;
}
number_digits <- function(n) {
MAX<-0;#最大回文数 for(i in 100:999)
https://pe-cn.github.io/4/
Problem 4 Largest palindrome productA 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 m