C++题解:蒜头君爱数数

   目录

题目 

题解


题目 

蒜头君在数学课上学习了素数,在信息课上学习了回文数,她现在很想知道,对于区间 [L,R] ,其中有多少个数既是素数又是回文数

输入格式

第一行两个整数分别表示 L,R

输出格式

一个整数表示有多少个数满足条件

数据范围

对于 20% 的数据,L=R

对于 50% 的数据,L≤R≤10³

对于 100% 的数据,L≤R≤10⁶

输出时每行末尾的多余空格,不影响答案正确性

要求使用「文件输入输出」的方式解题,输入文件为 count.in,输出文件为 count.out

样例输入复制

7 11

样例输出复制

2

题解:

知识点:枚举

分析:题目比较简单,最普通的枚举。先判断此数是否是回文串再判断是否是质数时间复杂度更低一些,因为质数常有而回文数不常有【引经据典,哈】

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#define _for(i,a,b) for (int i=(a);i<=(b);i++)//更方便
using namespace std;
int reverse(int x){//返回其回文串
    int ans=0;//注意初始化
    while (x){
        ans=ans*10+x%10;
        x/=10;
    }
    return ans;
}
bool is_prime(int a){//判断是否属质数
    int m=sqrt(a);
    for (int i=2;i<=m;i++){
        if (a%i==0){
            return false;
        }
    }
    return true;
}
int main(){
    freopen("count.in","r",stdin);
    freopen("count.out","w",stdout);
    int l,r,ans=0;
    scanf("%d%d",&l,&r);
    _for(i,l,r){
        if (reverse(i)==i && is_prime(i)){//判断
            ans++;
        }
    }
    printf("%d\n",ans);
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于扫雷游戏的题解,我可以给你一些思路和代码示例。首先,你需要了解扫雷游戏的规则和要求。接下来,你可以使用C++语言来实现游戏逻辑和界面。 下面是一个简单的扫雷游戏的C++代码示例: ```cpp #include <iostream> #include <vector> #include <random> using namespace std; class MinesweeperGame { private: int rows; int cols; vector<vector<char>> board; vector<vector<bool>> revealed; vector<pair<int, int>> directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; public: MinesweeperGame(int m, int n, int mineCount) { rows = m; cols = n; board.resize(rows, vector<char>(cols, ' ')); revealed.resize(rows, vector<bool>(cols, false)); placeMines(mineCount); calculateNumbers(); } void printBoard() { cout << " "; for (int j = 0; j < cols; j++) { cout << j << " "; } cout << endl; for (int i = 0; i < rows; i++) { cout << i << " |"; for (int j = 0; j < cols; j++) { cout << board[i][j] << "|"; } cout << endl; } } bool isGameOver() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i][j] == 'M' && revealed[i][j]) { return true; } } } return false; } void reveal(int row, int col) { if (row < 0 || row >= rows || col < 0 || col >= cols || revealed[row][col]) { return; } revealed[row][col] = true; if (board[row][col] == 'M') { return; } if (board[row][col] == '0') { for (auto dir : directions) { reveal(row + dir.first, col + dir.second); } } } private: void placeMines(int mineCount) { random_device rd; mt1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值