首页 > 其他 > 详细

1015. Reversible Primes (20)

时间:2015-12-06 11:19:35      阅读:164      评论:0      收藏:0      [点我收藏+]

1,不同基底的反数算法,

2,输入 cin 使用



reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.

Now given any two positive integers N (< 105) and D (1 < D <= 10), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line "Yes" if N is a reversible prime with radix D, or "No" if not.

Sample Input:
73 10
23 2
23 10
-2
Sample Output:
Yes
Yes
No

 
  1. #pragma warning(disable:4996)
  2. #include <stdio.h>
  3. #include <iostream>
  4. using namespace std;
  5. bool isPrime(int i) {
  6. if (i < 2)
  7. return false;
  8. if (i == 2 || i == 3)
  9. return true;
  10. for (int j = 2; j*j <= i; j++) {
  11. if (i%j == 0)
  12. return false;
  13. }
  14. return true;
  15. }
  16. int main(void) {
  17. while (1)
  18. {
  19. int num = 0, radix = 0;
  20. cin >> num;
  21. //scanf("%d", &num);
  22. if (num < 0)
  23. break;
  24. else {
  25. cin >> radix;
  26. if (isPrime(num) == true) {
  27. int reverse = 0,d=0,temp=0;
  28. while (num>=radix)
  29. {
  30. d = num%radix;
  31. num = num / radix;
  32. reverse = (reverse)*radix+d;
  33. }
  34. reverse = reverse * radix;
  35. reverse += num;
  36. if (isPrime(reverse) == true)
  37. cout << "Yes" << endl;
  38. else
  39. cout << "No" << endl;
  40. }
  41. else
  42. cout << "No" << endl;
  43. }
  44. }
  45. return 0;
  46. }





1015. Reversible Primes (20)

原文:http://www.cnblogs.com/zzandliz/p/5023056.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!