整数拆段
This commit is contained in:
parent
83fac884dc
commit
4dc41586f5
|
@ -0,0 +1,25 @@
|
|||
# 整数拆段
|
||||
|
||||
[问题描述]
|
||||
|
||||
教学案例,必须使用函数完成求解!
|
||||
|
||||
将一个长度小于15位的数字串拆成2段,使其和为最小的素数。
|
||||
例如数字串‘13304’,拆的方法有:
|
||||
1 + 3304 = 3305
|
||||
13 + 304 = 317
|
||||
133 + 04 = 137
|
||||
1330 + 4 = 1334
|
||||
从上面可看出,和为素数的有:317 与137,最小的是137
|
||||
|
||||
[输入格式]
|
||||
一个长度小于15的数字串。
|
||||
|
||||
[输出格式]
|
||||
最小的和为素数的数,若无素数则输出 -1。
|
||||
|
||||
[输入样例]
|
||||
13304
|
||||
|
||||
[输出样例]
|
||||
137
|
|
@ -0,0 +1,3 @@
|
|||
# 标题
|
||||
|
||||
* 内容
|
|
@ -0,0 +1,6 @@
|
|||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Hello Easy C++ project!" << std::endl;
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,28 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
bool pan(long long n){
|
||||
if(n<2){
|
||||
return 0;
|
||||
}
|
||||
for(int i=2;i*i<=n;i++){
|
||||
if(n%i==0){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int main(){
|
||||
long long n,a=10,b,ans=1000000000000000;
|
||||
cin>>n;
|
||||
for(int i=10;a>=10;i*=10){
|
||||
a=n/i;
|
||||
b=n%i;
|
||||
if(a+b<ans&&pan(a+b)==1){
|
||||
ans=a+b;
|
||||
}
|
||||
}
|
||||
if(ans==1000000000000000){
|
||||
ans=-1;
|
||||
}
|
||||
cout<<ans;
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
13304
|
|
@ -0,0 +1 @@
|
|||
33
|
|
@ -0,0 +1,12 @@
|
|||
## z:\Chao\src\1653_numcutup\test\in.txt
|
||||
2020/03/27 ÖÜÎå 17:59:38.77
|
||||
137
|
||||
-----------------------------------------------
|
||||
Process exited after 160 ms with return value 0
|
||||
|
||||
## z:\Chao\src\1653_numcutup\test\in2.txt
|
||||
2020/03/27 ÖÜÎå 17:59:38.77
|
||||
-1
|
||||
-----------------------------------------------
|
||||
Process exited after 90 ms with return value 0
|
||||
|
Loading…
Reference in New Issue