1,不同基底的反数算法,
2,输入 cin 使用
A 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 -2Sample Output:
Yes Yes No
#pragma warning(disable:4996)#include <stdio.h>#include <iostream>using namespace std;bool isPrime(int i) {if (i < 2)return false;if (i == 2 || i == 3)return true;for (int j = 2; j*j <= i; j++) {if (i%j == 0)return false;}return true;}int main(void) {while (1){int num = 0, radix = 0;cin >> num;//scanf("%d", &num);if (num < 0)break;else {cin >> radix;if (isPrime(num) == true) {int reverse = 0,d=0,temp=0;while (num>=radix){d = num%radix;num = num / radix;reverse = (reverse)*radix+d;}reverse = reverse * radix;reverse += num;if (isPrime(reverse) == true)cout << "Yes" << endl;elsecout << "No" << endl;}elsecout << "No" << endl;}}return 0;}
原文:http://www.cnblogs.com/zzandliz/p/5023056.html