一道关于哥德巴赫的程序,请高手指导
程序代码:
#include <iostream> using namespace std; int num; int prime(int a) { int flag = 1; for(int s = 2; s <= (a / 2) && flag == 1; s++) { if (a % s == 0) flag = 0; } return flag; } void gotbaha(int a) { int a1 = 0, a2 = 0, n = 0; for(a1 = 1; a1 <= (a - 1) / 2; a1++) { a2 = num - a1; if(prime(a1) && prime(a2)) { cout << num << " = " << a1 << " + " << a2 << endl; n = n + 1; } } cout << "共有" << n << " 种解法!" << endl; } int main(void) { cout << "请输入一个大于6的整数:"; cin >> num; if (num < 6) { cout << "您输入的数字不符合要求!" << endl; cout << "请重新输入: "; cin >> num; } else { gotbaha(num); } return 0; }输入很多数都能正常过,但输入255时竟然无解,不知何故,请高手指导
[ 本帖最后由 dxp44444 于 2011-8-26 15:14 编辑 ]