【C# ACM】筛素数 -- 完美的素数
2018-02-19 01:01
295 查看
Time Limit: 1000 ms Memory Limit: 65536 KiB[align=center]Submit Statistic Discuss[/align]
每组测试数据只包含一个正整数 n (1 < n <= 10^6)。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QwQ {
class Program {
public static int[] prime = new int[MAX/3]; //定义全局变量 key[value] -> (第N-1个素数)[第N-1个素数值]
public static bool[] isPrime = new bool[MAX]; //判断是否为素数 数[是/否]
public static int cntOfPrime = 0; //总素数个数
public const int MAX = (int)1e6 + 100; // 最大值
static void Main(string[] args) {
getPrime();
while(true) {
string n = Console.ReadLine();
if(jdg(n)) {
Console.WriteLine("YES");
}
else {
Console.WriteLine("NO");
}
}
Console.ReadKey();
}
public static bool jdg(string n) {
int m = int.Parse(n);
if(m >= 0 && isPrime[m] == true) {
int sum = 0;
foreach(char i in n) {
sum += i - '0';
}
if(isPrime[sum] == true) return true;
}
return false;
}
public static void getPrime() {
for(int i = 2;i < MAX;i ++) {
isPrime[i] = true;
}
for(int i
4000
= 2;i < MAX;i ++) {
if(isPrime[i] == true) {
prime[cntOfPrime++] = i;
}
for(int j = 0;j < cntOfPrime && i * prime[j] < MAX;j++) {
isPrime[i * prime[j]] = false;
if(i % prime[j] <= 0) break;
}
}
}
}
}
/***************************************************
Take time: 76ms
Take Memory: 5344KB
****************************************************/
完美的素数
Time Limit: 1000 ms Memory Limit: 65536 KiB[align=center]Submit Statistic Discuss[/align]Problem Description
素数又称质数。指一个大于1的自然数,除了1和此整数自身外,不能被其他自然数整除的数。我们定义:如果一个素数是完美的素数,当且仅当它的每一位数字之和也是一个素数。现在给你一个正整数,你需要写个程序判断一下这个数按照上面的定义是不是一个完美的素数。Input
输入包含多组测试数据。每组测试数据只包含一个正整数 n (1 < n <= 10^6)。
Output
对于每组测试数据,如果 n 是完美的素数,输出“YES”,否则输出“NO”(输出均不含引号)。Sample Input
11 13
Sample Output
YES NO
Hint
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QwQ {
class Program {
public static int[] prime = new int[MAX/3]; //定义全局变量 key[value] -> (第N-1个素数)[第N-1个素数值]
public static bool[] isPrime = new bool[MAX]; //判断是否为素数 数[是/否]
public static int cntOfPrime = 0; //总素数个数
public const int MAX = (int)1e6 + 100; // 最大值
static void Main(string[] args) {
getPrime();
while(true) {
string n = Console.ReadLine();
if(jdg(n)) {
Console.WriteLine("YES");
}
else {
Console.WriteLine("NO");
}
}
Console.ReadKey();
}
public static bool jdg(string n) {
int m = int.Parse(n);
if(m >= 0 && isPrime[m] == true) {
int sum = 0;
foreach(char i in n) {
sum += i - '0';
}
if(isPrime[sum] == true) return true;
}
return false;
}
public static void getPrime() {
for(int i = 2;i < MAX;i ++) {
isPrime[i] = true;
}
for(int i
4000
= 2;i < MAX;i ++) {
if(isPrime[i] == true) {
prime[cntOfPrime++] = i;
}
for(int j = 0;j < cntOfPrime && i * prime[j] < MAX;j++) {
isPrime[i * prime[j]] = false;
if(i % prime[j] <= 0) break;
}
}
}
}
}
/***************************************************
Take time: 76ms
Take Memory: 5344KB
****************************************************/
相关文章推荐
- 山东理工大学第七届ACM校赛-完美素数 分类: 比赛 2015-06-26 10:36 15人阅读 评论(0) 收藏
- 山东理工大学第七届ACM校赛-完美素数
- C#调用FLASH [转自aowind的vb.net与FLASH的完美结合]
- 用筛选法求出100以内的全部素数,并按每行五个数显示。(C#)
- EditPlus配置完美的C#运行环境
- http://acm.hdu.edu.cn/showproblem.php?pid=2303 (素数打表 + java大数取模)
- c# 算法 -- 求素数
- ACM长沙赛区 Goldbach——素数筛
- ACM之素数环(DFS)
- c#完美实现验证码功能
- ACM模板——快速判断素数
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(八) 完美实现A*寻径动态动画
- C#实现的Rabin-Miller检测素数算法,可检测一个数是否为素数(质数)
- C# ComboBox控件“设置 DataSource 属性后无法修改项集合”的完美解决方法
- SuperWebSocket服务器端开发,实现c#与webSocket的完美结合
- C# 递归求任意两数之间的素数
- C#用标记法来求素数
- SDUT-2553 完美的素数
- C#完美实现多语言版本Windows应用程序
- 每日ACM小题--POJ1006——中国剩余定理的完美诠释 2013年11月04(附录POJ_1005水题一枚)