当前位置:网站首页>信息学奥赛一本通 1405:质数的和与积 (思维题)
信息学奥赛一本通 1405:质数的和与积 (思维题)
2022-06-26 13:56:00 【GHOSTANDBREAD】
信息学奥赛一本通(C++版)在线评测系统 (ssoier.cn)
【题目描述】
两个质数的和是S,它们的积最大是多少?
【输入】
一个不大于10000的正整数S,为两个质数的和。
【输出】
一个整数,为两个质数的最大乘积。数据保证有解。
【输入样例】
50【输出样例】
589思路:
一个数拆成两部分,如何乘积最大,例如:50拆成1和49以及50拆成25和25,显然这两个数越靠近中间,乘积越大,题目有个坑就是这两个质数可以是一样的,但是题目没有说明。
代码:
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
bool flag = false;
bool isprime(int x) {
if(x == 1) return false;
for(int i = 2; i <= x / i; i ++) {
if(x % i == 0) return false;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cout.tie(nullptr);
int x;
cin >> x;
int h = x / 2;
for(int i = h; i <= x; i ++) {
for(int j = i; j >= 0; j --) {
if(isprime(i) && isprime(j) && i + j == x) {
cout << i * j;
flag = true;
break;
}
}
if(flag) break;
}
return 0;
}
边栏推荐
- Deploy the flask environment using the pagoda panel
- MySQL | basic commands
- The annual salary of 500000 is one line, and the annual salary of 1million is another line
- VMware partial settings
- Never use redis expired monitoring to implement scheduled tasks!
- Matlab programming related knowledge
- D - Face Produces Unhappiness
- DOS command
- 扩展-Hooks
- Sword finger offer 05.58 Ⅱ string
猜你喜欢

聊聊 RPA 方向的规划:简单有价值的事情长期坚持做

How to call self written functions in MATLAB

Codeforces Global Round 21A~D

9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册

服务器创建虚拟环境跑代码

Sword finger offer 10 Ⅰ 10Ⅱ. 63 dynamic planning (simple)

Sword finger offer 21.57.58 I Double pointer (simple)

备战数学建模30-回归分析2

Leaflet load day map
随机推荐
Relevant knowledge of information entropy
ArcGIS secondary development method - layer related operations (add, modify)
MHA高可用配合及故障切换
在线牛人博主
C language | Consortium
Electron
Combat readiness mathematical modeling 31 data interpolation and curve fitting 3
9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions
PostGIS create spatial database
Installation tutorial about origin2019
K gold Chef (two conditions, two points and difference)
C language | the difference between heap and stack
GDAL grid data types and their type codes
Server create virtual environment run code
这才是优美的文件系统挂载方式,亲测有效
Comparison of disk partition modes (MBR and GPT)
Introduction to granular computing
ThreadLocal巨坑!内存泄露只是小儿科...
MySQL master-slave replication and read-write separation
idea快捷键