题目描述

因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数。

写一个程序来找出范围 [a,b](5≤a<b≤100,000,000)( 一亿)间的所有回文质数。

输入格式

第 1 行: 二个整数 a 和 b .

输出格式

输出一个回文质数的列表,一行一个。

输入输出样例

输入

  1. 5 500
输出

  1. 5
  2. 7
  3. 11
  4. 101
  5. 131
  6. 151
  7. 181
  8. 191
  9. 313
  10. 353
  11. 373
  12. 383

说明/提示

Hint 1: Generate the palindromes and see if they are prime.

提示 1: 找出所有的回文数再判断它们是不是质数(素数).

Hint 2: Generate palindromes by combining digits properly. You might need more than one of the loops like below.

提示 2: 要产生正确的回文数,你可能需要几个像下面这样的循环。

题目翻译来自NOCOW。

USACO Training Section 1.5

产生长度为5的回文数:

  1. for (d1 = 1; d1 <= 9; d1+=2) { // 只有奇数才会是素数
  2. for (d2 = 0; d2 <= 9; d2++) {
  3. for (d3 = 0; d3 <= 9; d3++) {
  4. palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;//(处理回文数...)
  5. }
  6. }
  7. }

思路:打表(呀呀呀,我都不好意思了)

代码:

  1. #include<cstdio>
  2. #include<sstream>
  3. #include<iostream>
  4. using namespace std;
  5. //bool su(int x){
  6. // if(x==1){
  7. // return false;
  8. // }
  9. // for(int i=2;i<x;i++){
  10. // if(x%i==0){
  11. // return false;
  12. // }
  13. // }
  14. // return true;
  15. //}
  16. //bool hui(int x){
  17. // stringstream ss;
  18. // ss<<x;
  19. // string tem=ss.str();
  20. // for(int i=0;i<tem.size()/2;i++){
  21. // if(tem[i]!=tem[tem.size()-1-i]){
  22. // return false;
  23. // }
  24. // }
  25. // return true;
  26. //}
  27. //int main(){
  28. // for(int i=5;i<=100000000;i+=2){
  29. // if(hui(i)&&su(i)){
  30. // printf("%d,",i);
  31. // }
  32. // }
  33. //}
  34. int num[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
  35. int main(){
  36. int a,b;scanf("%d %d",&a,&b);
  37. for(int i=;i<sizeof(num)/sizeof(num[]);i++){
  38. if(num[i]>=a&&num[i]<=b){
  39. printf("%d\n",num[i]);
  40. }
  41. }
  42. }

遇到问题:

  1. stringstream 头文件有三个

    1. #include<sstream>
    2. #include<iostream>
    3. using namespace std;
  2. 数组元素个数 = sizeof(num)/sizeof(num[0])
  1.  

P1217 [USACO1.5]回文质数 Prime Palindromes(stringstream,sizeof(num)/sizeof(num[0]),打表)的更多相关文章

  1. P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

  2. 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

  3. luogu P1217 [USACO1.5]回文质数 Prime Palindromes x

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

  4. P1217 [USACO1.5]回文质数 Prime Palindromes

    题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...

  5. 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes【取回文数/数论/字符串】

    题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...

  6. P1217 [USACO1.5]回文质数 Prime Palindromes(技巧+暴力枚举+线性筛)

    技巧:就是偶数位的回文数字一定不是质数---------证明:奇数位之和sum1==偶数位之和sum2的数字可以被11整除.(11除外,这是一个坑点) 最高位,最低位必须是 1, 3, 7, 9 暴力 ...

  7. (函数)P1217 [USACO1.5]回文质数 Prime Palindromes

    题解: 第一次: 算法复杂度过高,导致编译超时,需要优化 #include<stdio.h>#include<math.h>int a[100000001] = { 0 };i ...

  8. Java实现 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes

    import java.util.Scanner; public class Main { private static Scanner cin; public static void main(St ...

  9. [USACO1.5]回文质数 Prime Palindromes

    题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...

随机推荐

  1. CentOS 7 上CNVnator安装

    1.到github上下载最新版本 https://github.com/abyzovlab/CNVnator/releases 2.先看INSTALL文件,要求以下依赖,我的机器上已经安装了前两个,所 ...

  2. 1240: 函数strcmp的设计

    #include <string.h>#include <stdio.h>int mycmp(char*s1,char*s2);int main(){ int sum; cha ...

  3. 十五 awk文本处理

    Awk 语法和基础命令 以行为处理单位 对数据进行逐行处理 处理完当前行,把当前行的处理结果输出后自动对下一行进行处理 直到文件中所有行处理完为止 创造者:Aho.Weinberger.Kernigh ...

  4. qt连接mysql数据库实例

    qt5.2版本已经封装进去了mysql驱动,所以省去了我们现编译的麻烦!!! #include <QCoreApplication> #include <QDebug> #in ...

  5. 谈恋爱就像TCP连接

    这是一张很内涵的漫画--爱情是靠不住的,即使你使用TCP连接也是如此.一心要握手成功,却被RST的男人就是个彻底的杯具-- 小知识: 一个虚拟连接的建立是通过三次握手来实现的. 第一次握手:建立连接时 ...

  6. [flask]邮件配置-20171227

    环境变量配置: # PowerShell设置环境变量: $env:MAIL_USERNAME = "" $env:MAIL_PASSWORD = "" 国内: ...

  7. angularJS 传参的四种方法 【修改】

    1. 基于ui-router的页面跳转传参(1) 在AngularJS的app.js中用ui-router定义路由,比如现在有两个页面,一个页面(producers.html)放置了多个produce ...

  8. 如何查看MySql的sql语句性能

    原文链接:https://blog.csdn.net/jwq101666/article/details/78561022Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通 ...

  9. Yandex Big Data Essentials Week1 Unix Command Line Interface Processes managing

    free displays the total amount of free and used memory free [options] top provides a dynamic real-ti ...

  10. NR / 5G - MAC Scheduler