列表

详情


NC237152. This is a number theory problem

描述

PaiGuDragon is an excellent number theory player.

For a positive interger number x, if the digit sum of x is a prime number, the PaiGuDragon calls x nice number.(i.e. digit sum of 269 equals to 17, and 17 is a prime number so 269 is nice!)

PaiGuDragon is curious about what is the n th smallest nice number.

输入描述

An interger

输出描述

Output the n th smallest nice number.

示例1

输入:

3

输出:

5

示例2

输入:

5

输出:

11

示例3

输入:

12

输出:

25

示例4

输入:

100

输出:

269

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C 解法, 执行用时: 9ms, 内存消耗: 348K, 提交时间: 2022-06-04 12:22:15

#include<stdio.h>
#include<math.h>
int zhishu(int i)
{
	int j=0;
	while(i>0)
	{
		j+=i%10;
		i=i/10;
	}
	int k;
	for(k=2;k<=sqrt(j);k++)
	{
		if(j%k==0)
		return 0;
	}
	if(j>=2)
	return 1;
	else
	return 0;
}
int main()
{
	int n,count=0,i=1;
	scanf("%d",&n);
	while(count<n)
	{
	    i++;
		count+=zhishu(i);
	}
	printf("%d",i);
 } 

C++(clang++ 11.0.1) 解法, 执行用时: 10ms, 内存消耗: 488K, 提交时间: 2022-09-06 13:01:39

#include<bits/stdc++.h>
using namespace std;
int n,k;
int s[15];
int cc(int x)
{
	int res=0;
	while(x) res+=x%10,x/=10;
	if(res==1) return 0;
	for(int i=2;i<=res/i;i++)
	{
		if(res%i==0) return 0;
	}
	return 1;
}
int main()
{
	cin>>n;
	for(int i=2;i<=1e8;i++)
	{
		if(cc(i)) k++;
		if(k==n)
		{
			cout<<i;
			return 0;
		}
	}
}

Python3 解法, 执行用时: 1633ms, 内存消耗: 8844K, 提交时间: 2022-06-04 15:00:14

prime = [2]
for i in range(3, 100):
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        prime.append(i)
pr2 = []
for i in range(400000):
    sum = 0
    for s in str(i):
        sum += int(s)
    if sum in prime:
        pr2.append(i)
n = int(input())
print(pr2[n-1])

pypy3 解法, 执行用时: 416ms, 内存消耗: 26276K, 提交时间: 2022-06-04 12:18:54

n=int(input())
pri=set([2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97])
for i in range(1,384773):
    if sum(map(int,list(str(i)))) in pri:
        n-=1
        if n==0:
            print(i)
            quit()

上一题