质数(数字字符串的相互转换)

cpp 复制代码
#include <iostream>

using namespace std;

bool is_prime(int num) {
    if (num < 2)
        return false;
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0)
            return false;
    }
    return true;
}

int main() {
    int T;
    cin >> T;
    
    while (T--) {
        int x;
        cin >> x;
        for(int i=1;;i++)
        {
            string str=to_string(x)+to_string(i);//数字转字符串进行拼接
            int y=stoi(str);//字符串转数字
            if(is_prime(y))
            {
                cout << y << endl;
                break;
            }
        }
    }
    return 0;
}
相关推荐
蒸蒸yyyyzwd4 小时前
秋招学习笔记 day46
c++·八股
码匠许师傅7 小时前
【C++三方组件】cpp-httplib:一个头文件起 HTTP 服务
开发语言·c++·http
无名猿8 小时前
new/delete 与 malloc/free:为什么绝对不能混用
c++·内存管理·现代c++·踩坑记录
2601_9622186110 小时前
C++中decltype关键字的实现
开发语言·c++
繁华的地方不一定留下你的脚印10 小时前
C++ std::variant 与 std::visit:安全保存多种类型,写清每个处理分支
开发语言·c++
程序员老陆11 小时前
WIN32_LEAN_AND_MEAN:Windows 头文件里的“精简编译开关”
c++·windows
hold?fish:palm12 小时前
45 二叉树的右视图
开发语言·c++·算法
ShineWinsu12 小时前
对于Redis:Redis的认识以及分布式系统的解析
linux·数据库·c++·redis·缓存·消息队列·分布式架构
布莱克60512 小时前
在C++中,构造函数能不能是虚函数?析构函数为什么要虚?
开发语言·c++·构造函数·析构函数·虚函数
旖旎夜光13 小时前
LeetCode 137 : 只出现一次的数字 II(位运算) —— 题解
c++·笔记·学习·算法·leetcode·力控