注册 登录
编程论坛 C++教室

差值最小

风卷浪起 发布于 2023-09-29 19:30, 742 次点击
F. 差值最小
内存限制:128 MiB
时间限制:1000 ms
标准输入输出
题目类型:传统
评测方式:文本比较
题目描述
输入一个正整数n,请输出连续的五个质数,使得这五个质数的和与5*n的差值最小。

输入格式
一个正整数n

输出格式
五个质数,从小到大输出,每个一行。

样例
输入样例
10000
样例输出
9967
9973
10007
10009
10037
数据范围与提示
100<=n<=20000
各位大佬帮个忙
6 回复
#2
rjsp2023-09-29 22:50
在n前后找素数
#3
风卷浪起2023-09-30 15:30
谢谢!
#4
风卷浪起2023-09-30 15:51
能不能给个具体的代码
#5
风卷浪起2023-10-01 22:12
我做出来了!
(有点复杂)
#include <bits/stdc++.h>
using namespace std;
long long a[5], b[5];
bool prime(int n)
{
    for (int i = 2; i <= sqrt(n); i ++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}
int find_prime(int n, int f)
{
    int i = n+f;
    while (!prime(i))
    {
        i += f;
    }
    return i;
}
int main()
{
    int n, num1, num2;
    cin >> n;
    if (prime(n))
    {
        a[1] = find_prime(n, -1);
        a[0] = find_prime(a[1], -1);
        a[2] = n;
        a[3] = find_prime(a[2], 1);
        a[4] = find_prime(a[3], 1);
        cout << a[0] << endl;
        cout << a[1] << endl;
        cout << a[2] << endl;
        cout << a[3] << endl;
        cout << a[4] << endl;
    }
    else
    {
        a[1] = find_prime(n, -1);
        a[0] = find_prime(a[1], -1);
        a[2] = find_prime(a[1], 1);
        a[3] = find_prime(a[2], 1);
        a[4] = find_prime(a[3], 1);
        num1 = abs(a[0] + a[1] + a[2] + a[3] + a[4] - 5 * n);
        b[2] = find_prime(n, -1);
        b[1] = find_prime(b[2], -1);
        b[0] = find_prime(b[1], -1);
        b[3] = find_prime(b[2], 1);
        b[4] = find_prime(b[3], 1);
        num2 = abs(b[0] + b[1] + b[2] + b[3] + b[4] - 5 * n);
        if (num1 > num2)
        {
            cout << b[0] << endl;
            cout << b[1] << endl;
            cout << b[2] << endl;
            cout << b[3] << endl;
            cout << b[4] << endl;
        }
        else
        {
            cout << a[0] << endl;
            cout << a[1] << endl;
            cout << a[2] << endl;
            cout << a[3] << endl;
            cout << a[4] << endl;
        }
    }
}
#6
apull2023-10-01 23:32
稍微改了下你的代码,供参考
程序代码:


bool prime(int n)
{
    for (int i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}


void  find_prime(int *a, int n)
{
    int i = n, cnt = 4, f = -1;
    while (cnt < 10)
    {
        if (prime(i))
        {
            a[cnt] = i;
            cnt += f;
            if (cnt == 0)
            {
                i = n;
                f = 1;
                cnt = 5;
            }
        }
        i += f;
    }
}


int main()
{
    int a[10];
    int n, num1, num2, min, t, inx = 0;
    cin >> n;
    find_prime(a, n);
    min = num1 = 5 * n;

    for (int i = 0; i <= 6; i++)
    {
        num2 = 0;
        for (int j = 0; j < 5; j++)      
            num2 += a[i + j];
        
        t = abs(num1 - num2);
        if (t < min)
        {
            min = t;
            inx = i;
        }
    }

    for (int i = inx; i < inx + 5; i++)
        cout << a[i] << endl;
}




[此贴子已经被作者于2023-10-1 23:34编辑过]

#7
风卷浪起2024-02-24 21:40
谢谢
1