鱼C论坛

 找回密码
 立即注册
查看: 3122|回复: 12

题目36:求出100万以下所有十进制和二进制表示都是回文的数字之和

[复制链接]
发表于 2015-4-23 23:56:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 永恒的蓝色梦想 于 2020-6-3 19:05 编辑
Double-base palindromes

The decimal number, BaiduShurufa_2015-4-23_23-57-24.png (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

(Please note that the palindromic number, in either base, may not include leading zeros.)

题目:

十进制数字 BaiduShurufa_2015-4-23_23-55-10.png (二进制),可以看出在十进制和二进制下都是回文(从左向右读和从右向左读都一样)。

求 100 万以下所有在十进制和二进制下都是回文的数字之和。

(注意在两种进制下的数字都不包括最前面的 0)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-30 11:38:38 | 显示全部楼层
0:(2)  (10)0
1:(2)1  (10)1
3:(2)11  (10)3
5:(2)101  (10)5
7:(2)111  (10)7
9:(2)1001  (10)9
33:(2)100001  (10)33
99:(2)1100011  (10)99
313:(2)100111001  (10)313
585:(2)1001001001  (10)585
717:(2)1011001101  (10)717
7447:(2)1110100010111  (10)7447
9009:(2)10001100110001  (10)9009
15351:(2)11101111110111  (10)15351
32223:(2)111110111011111  (10)32223
39993:(2)1001110000111001  (10)39993
53235:(2)1100111111110011  (10)53235
53835:(2)1101001001001011  (10)53835
73737:(2)10010000000001001  (10)73737
585585:(2)10001110111101110001  (10)585585

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. string calc(int n)
  5. {
  6.         string result;
  7.         /* 转为二进制 不断取余*/
  8.         while (n)
  9.         {
  10.                 result+=(char)((n%2)+'0');
  11.                 n/=2;
  12.         }
  13.         reverse(result.begin(),result.end());
  14.         return result;

  15. }
  16. /* 是否为回文数字 */
  17. bool is(string n)
  18. {
  19.         char *buf = (char*)n.data();
  20.        
  21.         int len = strlen(buf);
  22.         char *p1,*p2;
  23.         p1 = buf;
  24.         p2 = buf + len - 1;
  25.         while (p1<p2 && *p1 == *p2 )
  26.         {
  27.                 p1++;
  28.                 p2--;
  29.         }
  30.    return p1>=p2;
  31. }
  32. int main(void)
  33. {

  34.         for (int i=0;i<1000000;i++)
  35.         {
  36.                 char buf[12];
  37.                 itoa(i,buf,10);
  38.                 string str(buf);
  39.                 if(is(calc(i)) && is(str))
  40.                 {
  41.                         std::cout<<i<<":"<<"(2)"<<calc(i)<<"  (10)"<<i<<endl;
  42.                 }
  43.         }
  44.         return 0;
  45. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-3 10:26:15 | 显示全部楼层
  1. def Rec(number):
  2.       temp = list(number)
  3.       temp.reverse()
  4.       tmp = list(number)
  5.       if temp == tmp:
  6.             return True
  7.       return False
  8.       


  9. list1=[]
  10. for i in range(1,1000000):
  11.       n = str(i)
  12.       if Rec(n):
  13.             temp = bin(i)
  14.             if Rec(str(temp[2:])):
  15.                   list1.append(i)

  16. print(sum(list1))
复制代码

结果:872187
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-14 09:22:53 | 显示全部楼层
  1. # encoding:utf-8
  2. # 寻找100万以下的十进制和二进制都是回文数的数之和
  3. from time import time

  4. def euler036(N=1000000):
  5.     print(sum([i for i in range(1, N + 1) if str(i) == str(i)[::-1] and str(bin(i))[2:] == str(bin(i))[2:][::-1]]))
  6. if __name__ == '__main__':
  7.     start = time()
  8.     euler036(1000000)
  9.     print('cost %.6f sec' % (time() - start))
复制代码


872187
cost 0.666491 sec
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-14 21:57:17 | 显示全部楼层
此代码使用matlab编程
Problem36所用时间为: 0.21198秒
Problem36的答案为: 872187
  1. %% Problem36.m
  2. % 最后编辑时间:17-06-14 22:10
  3. % 找出100w以下的数使得,其二进制数和十进制数都为回文数,求其和
  4. % Problem36所用时间为: 0.21198秒
  5. % Problem36的答案为: 872187
  6. function Output = Problem36()
  7. tic
  8. Output = 0;
  9. %个位数
  10. for aa = 1:9
  11.     if fliplr(num2str(dec2bin(aa))) == num2str(dec2bin(aa))
  12.         Output = Output + aa;
  13.     end
  14. end
  15. %十位数
  16. for aa = 1:9
  17.     if fliplr(num2str(dec2bin(aa*10 + aa))) == num2str(dec2bin(aa*10 + aa))
  18.         Output = Output + aa*10 + aa;
  19.     end
  20. end

  21. %百位数
  22. for aa = 1:9
  23.     for bb = 0:9
  24.        if fliplr(num2str(dec2bin(aa*100 + bb*10 + aa))) == num2str(dec2bin(aa*100 + bb*10 + aa))
  25.           Output = Output + aa*100 + bb*10 + aa;
  26.        end  
  27.     end
  28. end

  29. %千位数
  30. for aa = 1:9
  31.     for bb = 0:9
  32.        if fliplr(num2str(dec2bin(aa*1000 + bb*100 + bb*10 + aa))) == num2str(dec2bin(aa*1000 + bb*100 + bb*10 + aa))
  33.           Output = Output + aa*1000 + bb*100 + bb*10 + aa;
  34.        end  
  35.     end
  36. end

  37. %万位数
  38. for aa = 1:9
  39.     for bb = 0:9
  40.         for cc = 0:9
  41.             if fliplr(num2str(dec2bin(aa*10000 + bb*1000 + cc*100 + bb*10 + aa))) == num2str(dec2bin(aa*10000 + bb*1000 + cc*100 + bb*10 + aa))
  42.                 Output = Output + aa*10000 + bb*1000 + cc*100 + bb*10 + aa;
  43.             end
  44.         end
  45.     end
  46. end

  47. %十万位
  48. for aa = 1:9
  49.     for bb = 0:9
  50.         for cc = 0:9
  51.             if fliplr(num2str(dec2bin(aa*100000 + bb*10000 + cc*1000 + cc*100 + bb*10 + aa))) == num2str(dec2bin(aa*100000 + bb*10000 + cc*1000 + cc*100 + bb*10 + aa))
  52.                 Output = Output + aa*100000 + bb*10000 + cc*1000 + cc*100 + bb*10 + aa;
  53.             end
  54.         end
  55.     end
  56. end
  57. toc
  58. disp('此代码使用matlab编程')
  59. disp(['Problem36所用时间为: ',num2str(toc),'秒'])
  60. disp(['Problem36的答案为: ',num2str(Output)])
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-25 16:38:54 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-7-2 18:36 编辑
  1. import time
  2. start = time.time()

  3. c = []
  4. for i in range(1, 1000000):
  5.     binary = bin(i)
  6.     binary = str(binary)
  7.     i = str(i)

  8.     if i == i[::-1] and  binary[2:] == binary[::-1].replace('b0', ''):
  9.         c.append(i)
  10. print(sum(map(int, c)))

  11. end = time.time()
  12. print(end - start)
复制代码


用时0.703022956848
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-27 16:53:10 | 显示全部楼层

  1. numList = []

  2. for num in range(1000000):
  3.     decNumList = list(str(num))
  4.     decNumTempList = list(str(num))
  5.     binNumList = list(str(bin(num))[2:])
  6.     binNumTempList = list(str(bin(num))[2:])
  7.     decNumTempList.reverse()
  8.     binNumTempList.reverse()
  9.     if decNumList == decNumTempList:
  10.         if binNumList == binNumTempList:
  11.             numList.append(num)

  12. print(sum(numList))
复制代码
872187
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-13 16:39:59 | 显示全部楼层
送分题:872187

  1. print(sum(num for num in range(1, 1000000) if str(num) == str(num)[::-1] and str(bin(num)).split('b')[1] == str(bin(num)).split('b')[1][::-1]))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-18 22:31:53 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-5-9 13:14 编辑

C++ 44ms
  1. #include<iostream>
  2. using namespace std;


  3. int main() {
  4.     int i, j, res = 0, sum = 0;

  5.     for (i = 1; i < 1000000; i++) {
  6.         j = i;

  7.         while (j) {
  8.             res = (res << 1) | (j & 1);
  9.             j >>= 1;
  10.         }

  11.         if (res == i) {
  12.             while (res) {
  13.                 j = j * 10 + res % 10;
  14.                 res /= 10;
  15.             }

  16.             if (j == i) {
  17.                 sum += i;
  18.             }
  19.         }
  20.         else {
  21.             res = 0;
  22.         }
  23.     }

  24.     cout << sum << endl;
  25.     return 0;
  26. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-5 16:36:57 | 显示全部楼层
872187

Process returned 0 (0x0)   execution time : 0.321 s
Press any key to continue.
利用vector判断回文串
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;

  4. const int M = 1e6;
  5. vector<int> decimal;
  6. vector<int> bin;

  7. bool judge(int x,int base,vector<int> & v){
  8.   while(x){
  9.     v.push_back(x % base);
  10.     x /= base;
  11.   }
  12.   int len = v.size();
  13.   for (int i = 0;i < v.size()/2;i++)
  14.     if (v[i] != v[len-1-i]) return false;

  15.   return true;
  16. }

  17. int main(){
  18.   int ans = 0;
  19.   for (int i = 1;i < M;i++){
  20.     decimal.clear();
  21.     bin.clear();
  22.     if (judge(i,10,decimal) && judge(i,2,bin))  ans += i;
  23.   }
  24.   cout << ans << endl;
  25.   return 0;
  26. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-16 19:22:23 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>

  4. void reverse(char str[], char rts[]);
  5. void Dec2bin(int, char []);

  6. void reverse(char str[], char rts[])//回文
  7. {
  8.         int i, j, k;
  9.         k = strlen(str);

  10.         for (i = 0, j = k - 1; i < k; i++, j--)
  11.         {
  12.                 rts[i] = str[j];
  13.         }
  14.         rts[k] = '\0';
  15. }
  16. void Dec2bin(int num, char bin[])//十进制转二进制
  17. {
  18.         int i = 0, j, k;
  19.         j = num;
  20.         while (j)
  21.         {
  22.                 k = j % 2;
  23.                 bin[i++] = k + '0';
  24.                 j /= 2;               
  25.         }
  26.         bin[i] = '\0';

  27. }
  28. main()
  29. {
  30.         char str[128], rts[128], bin[128];
  31.         int i, j, k, len, flag = 1, sum = 0;

  32.         for (i = 1; i < 1000000; i += 2) //由于要二进制回文后也相等,所以只能是偶数
  33.         {
  34.                 sprintf(str, "%d", i);
  35.                 reverse(str, rts);
  36.                 k = atoi(rts);
  37.                 if (k == i)
  38.                 {
  39.                         Dec2bin(i, bin);
  40.                         reverse(bin, rts);
  41.                         len = strlen(bin);
  42.                         for (j = 0; j < len; j++)
  43.                         {
  44.                                 if (bin[j] != rts[j])
  45.                                 {
  46.                                         flag = 0;
  47.                                         break;
  48.                                 }
  49.                         }
  50.                         if (flag)
  51.                         {
  52.                                 sum += i;
  53.                                 printf("%d ", i);
  54.                         }
  55.                         flag = 1;
  56.                 }       
  57.         }
  58.         printf("\n%d\n", sum);
  59. }
复制代码


答案为:
1 3 5 7 9 33 99 313 585 717 7447 9009 15351 32223 39993 53235 53835 73737 585585

总和为:872187
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-24 15:22:55 | 显示全部楼层
  1. import time as t

  2. start = t.perf_counter()
  3. palindromic_nums = []
  4. for num in range(1000000):
  5.     num_str = str(num)
  6.     num_bin = str(bin(num)).lstrip('0b')
  7.     if num_str == num_str[::-1] and num_bin == num_bin[::-1]:
  8.         palindromic_nums.append(num)

  9. print(palindromic_nums)
  10. print(sum(palindromic_nums))
  11. print("It costs %f s" % (t.perf_counter() - start))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-15 11:45:17 | 显示全部楼层
结果
  1. $ time ./main
  2. 872187

  3. real        0m0.004s
  4. user        0m0.004s
  5. sys        0m0.000s
复制代码


两个判断回文的函数
  1. fn palindrome(x: u32) -> bool {
  2.     let mut x = x;
  3.     let bits: &mut [u8] = &mut [0; 10];
  4.     let mut i: usize = 0;
  5.     while x != 0 {
  6.         bits[i] = (x % 10) as u8;
  7.         i += 1;
  8.         x /= 10;
  9.     }
  10.     for j in 0..=i / 2 {
  11.         if bits[j] != bits[i - 1 - j] {
  12.             return false;
  13.         }
  14.     }
  15.     true
  16. }

  17. fn palindrome2(x: u32) -> bool {
  18.     let len = u32::BITS - x.leading_zeros();
  19.     for j in 0..len / 2 {
  20.         if ((x >> j) ^ (x >> (len - 1 - j))) & 1 != 0 {
  21.             return false;
  22.         }
  23.     }
  24.     true
  25. }

  26. #[cfg(test)]
  27. mod tests {
  28.     use super::*;
  29.     #[test]
  30.     fn test_palindrome() {
  31.         assert!(palindrome(123321));
  32.         assert!(palindrome(12321));
  33.         assert!(!palindrome(2321));
  34.         assert!(palindrome(1));
  35.         assert!(palindrome(585));
  36.     }
  37.     #[test]
  38.     fn test_palindrome2() {
  39.         assert!(palindrome2(0b101101));
  40.         assert!(palindrome2(0b101));
  41.         assert!(!palindrome2(0b1010));
  42.         assert!(palindrome2(0b1));
  43.         assert!(palindrome2(585));
  44.     }
  45. }
复制代码

主程序
  1. fn main() {
  2.     let mut sum = 0;
  3.     for i in 1..1e6 as u32 {
  4.         if palindrome2(i) && palindrome(i) {
  5.             sum += i;
  6.         }
  7.     }
  8.     println!("{sum}");
  9. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 00:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表