题目1440:Goldbach's Conjecture(哥达巴赫猜想)
题目链接:http://ac.jobdu.com/problem.php?pid=1440
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
// // 1440 Goldbach's Conjecture.cpp // Jobdu // // Created by PengFei_Zheng on 12/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cmath> using namespace std; bool isPrime(long long n){// here n is no less than 4, so we don't need to consider 2、3 long long x = sqrt(n)+1; for(long long i = 2 ; i <= x ; i ++){ if(0 == n % i) return false; } return true; } long long n; int main(){ while(scanf("%lld",&n)!=EOF && n!=0){ int counter = 0; // beacuse n is even, so 2 can never being included // in order to reduce the calculation numbers, just need to reverse to n/2 // therefore start with 3 and i+=2 for(int i = 3 ; i <= n/2 ; i+=2){ if(isPrime(i) && isPrime(n-i)){ counter++; } } printf("%d\n",counter); } return 0; } /************************************************************** Problem: 1440 User: zpfbuaa Language: C++ Result: Accepted Time:40 ms Memory:1532 kb ****************************************************************/
作者: 伊甸一点
出处: http://www.cnblogs.com/zpfbuaa/
本文版权归作者伊甸一点所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接 如有问题, 可邮件(zpflyfe@163.com)咨询.