当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- Summary of decimal point of amount and price at work and pit
- Pycharm远程连接服务器来跑代码
- Leaflet load day map
- 赠书 | 《认知控制》:我们的大脑如何完成任务?
- Two point answer, 01 score planning (mean / median conversion), DP
- 常用控件及自定义控件
- Correlation of XOR / and
- Relevant knowledge of information entropy
- MySQL | basic commands
- Sword finger offer 05.58 Ⅱ string
猜你喜欢

The annual salary of 500000 is one line, and the annual salary of 1million is another line

ThreadLocal giant pit! Memory leaks are just Pediatrics

Leaflet loading ArcGIS for server map layers

VMware partial settings

How to convert data in cell cell into data in matrix

量化框架backtrader之一文读懂observer观测器

Sword finger offer 40.41 Sort (medium)

Practice with the topic of bit operation force deduction

One article of the quantification framework backtrader read observer

Setup instance of layout manager login interface
随机推荐
Combat readiness mathematical modeling 32 correlation analysis 2
服务器创建虚拟环境跑代码
Electron
Excerpt from three body
One article of the quantification framework backtrader read observer
9 articles, 6 interdits! Le Ministère de l'éducation et le Ministère de la gestion des urgences publient et publient conjointement neuf règlements sur la gestion de la sécurité incendie dans les établ
Understand the difference and use between jsonarray and jsonobject
Common evaluation indexes of classification model -- confusion matrix and ROC curve
Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
Usage of unique function
datasets Dataset类(2)
'coach, I want to play basketball!'—— AI Learning Series booklet for system students
Equation derivation: second order active bandpass filter design! (download: Tutorial + schematic + Video + code)
Codeforces Global Round 21A~D
Eigen(3):error: ‘Eigen’ has not been declared
备战数学建模31-数据插值与曲线拟合3
在线牛人博主
MySQL主从复制与读写分离
布局管理器~登录界面的搭建实例
工作上对金额价格类小数点的总结以及坑