自己写的。题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
#include<stdio.h> int main() { int PrimeNumber[1000] = {1, 2}; int a[100], b[100]; int i, j, NumOfPrimeNumber = 0, IndexOfPrimeNumber = 2, n, min = 1000; for (i = 3; i <= 1000; i++) { for (j = 2; j < i; j++) { if (i % j == 0) { j = i + 1; } } if (j == i) { PrimeNumber[IndexOfPrimeNumber++] = i; } } NumOfPrimeNumber = IndexOfPrimeNumber; scanf("%d", &n); IndexOfPrimeNumber = 0; for (i = 0; i < NumOfPrimeNumber; i++) { for (j = i; j < NumOfPrimeNumber; j++) { if (PrimeNumber[i] + PrimeNumber[j] == n) { a[IndexOfPrimeNumber] = PrimeNumber[i]; b[IndexOfPrimeNumber++] = PrimeNumber[j]; } } } NumOfPrimeNumber = IndexOfPrimeNumber; for (i = 0; i < NumOfPrimeNumber; i++) { if (b[i] - a[i] < min) { min = b[i] - a[i]; } } for (i = 0; i < NumOfPrimeNumber; i++) { if (b[i] - a[i] == min) { printf("%d\n%d\n", a[i], b[i]); } } return 0; }