zoukankan      html  css  js  c++  java
  • [题解]openjudge-回文素数

    提交情况

    原题链

    解题思路

    整体思路:首先生成一个满足为n位的回文数,再判断其是否为素数。

    细节问题:我们枚举一个长度为10^(n - 1) ÷ 2长度的数,将这个数的前(n - 1) ÷ 2 - 1个数放到原数的后方生成新的回文数。example:我们需要一个长度为3为的回文数,那么我们要枚举的数是从10~99。假设我们枚举的一位数为10。那么最后生成的数即是101。

    源代码

    #include <bits/stdc++.h>
    using namespace std;
    int a[1000010];
    const int mod = 7;
    inline int make(int tmp){
    	int ans = tmp;
    	tmp /= 10;
    	while(tmp > 0){
    		ans = ans * 10 + tmp % 10;
    		tmp /= 10;
    	}
    	return ans;
    }
    inline bool prime(int k){
    	if(k < 2)return false;
    	if(k == 2)return true;
    	for(int i = 2;i * i <= k;i++){
    		if(k % i == 0)return false;
    	}
    	return true;
    }
    int main(){
    	int n;
    	cin>>n;
    	if(n % 2 == 0){
    		if(n == 2){
    			cout<<1<<endl<<11;
    			return 0;
    		}
    		else {
    			cout<<0;
    			return 0;
    		}
    	}
    	n = (n - 1) / 2; 
    	int h = pow(10,n),s = 0;
    	for(int i = h;i <= h * 10 - 1;i++){
    		int tmp = make(i);
    		if(prime(tmp)) a[++s] = tmp;
    	}
    	cout<<s<<endl;
    	for(int i = 1;i <= s;i++){
    		cout<<a[i]<<" ";
    	}
    	return 0;
    }
    
  • 相关阅读:
    uva 804WAWAWA--不想看了以后再说
    uva10129 play on words
    tree--
    打印素数表orz
    DeepFM模型
    国内常用镜像链接
    Thompson(汤普森)采样
    知识图谱简介
    显式反馈和隐式反馈
    RNN之LSTM及双向LSTM
  • 原文地址:https://www.cnblogs.com/czy--blog/p/11741853.html
Copyright © 2011-2022 走看看