优化判断素数的方法
https://ac.nowcoder.com/acm/contest/95700/D
一 :考试时候想的 但是制作回文数的过程太麻烦了
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long int;
int main()
{
ll t;
cin >> t;
bool is_prime = true;
int digits = 0;
ll temp = t;
while(temp!=0)
{
digits ++;
temp /= 10;
}
int time_ = (digits+1)/2;
ll newt = t*pow(10,time_);
//cout << newt;
temp = t;
//cout << time_;
while(time_--) // 2--
{
temp /= 10;
//cout << temp<<' ';
newt += temp%10*pow(10,time_);
//cout << newt<<' ';
}
for(int i = 2 ; i <= newt / i ; i++)
{
if(newt % i == 0)
{
is_prime = false;
break;
}
}
if(is_prime) cout << "prime";
else cout <<"noprime";
return 0;
}
利用string以及reverse函数 比第一个省时间不少
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long int;
int main()
{
string str;
cin >> str;
string revstr = str;
revstr.pop_back();
reverse(revstr.begin(),revstr.end());
str += revstr;
ll rever = 0;
for(int i = 0 ; i < str.length() ; i++)
{
rever = rever *10 + str[i]-'0';
}
bool is_prime = true;
for(int i= 2 ; i <= rever/i ; i++)
{
if(rever%i==0)
{
is_prime = false;
break;
}
}
if(is_prime) cout << "prime";
else cout << "noprime";
return 0;
}