码迷,mamicode.com
首页 > 其他好文 > 详细

1015. Reversible Primes (20)

时间:2018-03-05 22:28:11      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:clu   while   com   ble   lib   image   turn   分享图片   图片   

距离PAT考试还有13天最重要的是做透每一题

 

(1)思路

就是按照题目说的做

 

又把stringstream熟悉了一遍,输入到stringstream后可以输出到一个string对象

又把进制转换练习了一遍,10->d进制就是不断取余的过程,d->进制就是各位的权与各位相乘后相加

质数的判断就是除了1和它本身没有其他公因数的数 小于等于1时不是质数

string中的某个元素如下面的ans[i]的类型是char

 

虽然这里没用到 

atoi()配合c_str()可以将一个string转成int型的值

此时要包含<cstdlib>

#include <cstdio>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
using namespace std;

string to_rd(int a,int b) {
  stringstream ss;
  string s;
  while(a) {
    ss<<a%b;
    a/=b;
  }
  ss>>s;
  return s;
}
int to_10(string ans,int b) {
  int res=0;
  for(int i=0;i<ans.size();i++) {
    res=res*b+ans[i]-0;
  }
  return res;
}
bool is_prime(int n) {
  if(n <= 1) return false;
  int flag=0;
  for(int i=2;i<=sqrt(n);i++) {
    if(n % i == 0) flag=1;
  }
  return flag==1? false:true;
}
string f(int a,int b) {
  string ans=to_rd(a,b);
  int ians=to_10(ans,b);
  if(is_prime(ians) && is_prime(a)) return "Yes";
  else return "No";
}

int main() {
  int a,b;
  while(1) {
    scanf("%d",&a);
    if(a < 0) return 0;
    scanf("%d",&b);
    cout<<f(a,b)<<endl;
  }
  return 0;
}

技术分享图片

 

1015. Reversible Primes (20)

标签:clu   while   com   ble   lib   image   turn   分享图片   图片   

原文地址:https://www.cnblogs.com/tclan126/p/8511574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!