求助
查看原帖
求助
944438
Only_Him楼主2024/5/24 22:26

rt

#include<bits/stdc++.h>
using namespace std;
#define N 100000
bool prime[N];
int tmp[N];
int n;
int cnt = 0;
void find_prime(int m = N) {
    for (int i = 2; i <= m; i++) {
        if (!prime[i]){
        	tmp[cnt++] = i;
//        	cout << cnt << ' ' << i << endl;
		}         
        for (int j = 0; j < cnt && i * tmp[j] <= m; j++) {
            prime[i * tmp[j]] = true;
            if (i % tmp[j] == 0)break;
        }
    }
}
vector<string> ans;
int now = 0x3fffffff;
signed main(){
	string s;
	cin >> s;
	find_prime();
	for (int i = 1;i <= cnt;i++){
		int res = 0;
		string t = to_string(tmp[i]);
		while(t.size() != 5){
			t = '0' + t;
		}
		for (int i = 0;i < 5;i++){
			if (s[i] == t[i]) continue;
			else {
				int x = abs(s[i] - t[i]);
				res += min(x,10 - x);
			}
		}
		if (res < now){
			ans.clear();
			now = res;
			ans.push_back(t);
		} else if (res == now){
			ans.push_back(t);
		}
	}
	sort(ans.begin(),ans.end());
//	for (auto i : ans) cout << i << endl;
	cout << ans[ans.size() - 1] << endl;
	return 0;
} 
2024/5/24 22:26
加载中...