当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- Recent important news
- SwiftUI找回丢失的列表视图(List)动画
- Error when redis is started: could not create server TCP listening socket *: 6379: bind: address already in use - solution
- Comparison of disk partition modes (MBR and GPT)
- A remove the underline from the label
- Online bull Blogger
- vmware部分设置
- Question bank and answers of the latest Guizhou construction eight (Mechanics) simulated examination in 2022
- Complete diagram / Euler loop
- 在线牛人博主
猜你喜欢

Codeforces Global Round 21A~D

GDAL multiband synthesis tool

Win10 home vs pro vs enterprise vs enterprise LTSC

备战数学建模31-数据插值与曲线拟合3

Complimentary Book Cognitive Control: how does our brain accomplish tasks?

从Celsius到三箭:加密百亿巨头们的多米诺,史诗级流动性的枯竭

One article of the quantification framework backtrader read observer

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

年薪50万是一条线,年薪100万又是一条线…...

The annual salary of 500000 is one line, and the annual salary of 1million is another line
随机推荐
Never use redis expired monitoring to implement scheduled tasks!
通俗语言说BM3D
秒懂JSONArray和JSONObject的区别和使用
布局管理器~登录界面的搭建实例
Comparison of disk partition modes (MBR and GPT)
Caelus - full scene offline mixed Department solution
C language | Consortium
Heap optimization dijkstra/hash table storage node number
How to convert data in cell cell into data in matrix
ThreadLocal巨坑!内存泄露只是小儿科...
Eigen(3):error: ‘Eigen’ has not been declared
Correlation analysis related knowledge
Complete diagram / Euler loop
NAACL2022:(代码实践)好的视觉引导促进更好的特征提取,多模态命名实体识别(附源代码下载)...
使用宝塔面板部署flask环境
Two point answer, 01 score planning (mean / median conversion), DP
Leaflet loading ArcGIS for server map layers
Transformers datacollatorwithpadding class
这才是优美的文件系统挂载方式,亲测有效
Deploy the flask environment using the pagoda panel