//方法一:整除 #include <cmath> #include <iostream> using namespace std; bool isprime(long long a) { if (a <= 1) return false; for (long long i = 2; i <= sqrt(a); i++) { if (a % i == 0) return false; } return true; } int main() { long long n; while (cin >> n) { if (isprime(n)) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
//方法二:打表 #include <cstring> #include <iostream> using namespace std; #define MAX 1000000 bool prime[MAX]; void findprime() { prime[0] = false; prime[1] = false; memset(prime, true, sizeof(prime)); for (int i = 2; i < MAX; i++) if (prime[i]) //如果是素数 for (int j = 2 * i; j < MAX; j += i) { prime[j] = false; //筛去所有i的倍数 } } int main() { int n; findprime(); while (cin >> n) { if (prime[n]) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
#include <cmath> #include <iostream> using namespace std; int map1[1000][1000]; int map2[1000][1000]; int main() { int N, M, n, m; int posi, posj, min; while (cin >> N >> M) { posi = posj = 0; min = 0x3fffffff; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) cin >> map1[i][j]; cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> map2[i][j]; for (int i = 0; i <= N - n; i++) { // i,j为小矩阵起始位置 for (int j = 0; j <= M - m; j++) { int temp = 0; for (int p = 0; p < n; p++) { for (int q = 0; q < m; q++) { temp += fabs(map1[i + p][j + q] - map2[p][q]); } } if (min > temp) { posi = i; posj = j; min = temp; } } } cout << min << " (" << posi + 1 << "," << posj + 1 << ")" << endl; } return 0; }