提交时间:2022-06-19 17:46:42

运行 ID: 41976

#include<iostream> #include<math.h> using namespace std; bool notprime[10000]; short primenum[1229],pl,n,ni[5],nil,pi[5],pil,sum,minn=25,mini; void getprime(){ notprime[0]=notprime[1]=1; for(short i=2;i<10000;i++){ if(!notprime[i]){ primenum[pl++]=i; for(short j=i<<1;j<10000;j+=i) notprime[j]=1; } } } short change(short a,short b){ short t=abs(a-b); if(t>5) t=10-t; return t; } int main(){ getprime(); scanf("%hd",&n); while(n){ ni[nil++]=n%10; n/=10; } for(short i=1228;i>=0;i--){ short tpn=primenum[i]; pil=0; for(short j=0;j<5;j++) pi[j]=0; while(tpn){ pi[pil++]=tpn%10; tpn/=10; } sum=0; for(short j=0;j<5;j++) sum+=change(ni[j],pi[j]); if(sum<minn){ minn=sum; mini=primenum[i]; } } printf("%05hd",mini); return 0; }