Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我无语了, num&1 == 0的执行顺序竟然是 num&(1==0),害我调试了一个小时!!!!!!!!!!!

Posted by gfedcba at 2009-02-24 01:36:47 on Problem 3006
In Reply To:小日本的题总是那么水淋淋 Posted by:gfedcba at 2009-02-23 23:18:41
#include <iostream>
#include <math.h>
using namespace std;

bool IsPrime(const int num)
{
	int i, j, k;
	k = sqrt(float(num));
	if (num == 1)
	{
		return false;
	}
	if (num == 2)
	{
		return true;
	}
// 	else if (num&1 == 0) // 我无语了, num&1 == 0的执行顺序是 num&(1==0)
	else if ((num&1) == 0)
	{
		return false;
	}
	for (i=3; i<=k; i+=2)
	{
		if (num%i == 0)
		{
			return false;
		}
	}
	return true;
}
int main()
{
	int a,d,n;
	
	while(true)
	{
		cin >> a >> d >> n;
		if (0 == a && 0 == d && 0 == n)
			break;
	
		int count=0;
		int i = 0;

		while (true)
		{
		//  a = a+(i++*d);	
			if (IsPrime(a))
			{
				count++;
				if (count == n)
				{
					break;
				}
			}
			a = a+d;
		}
		cout<<a<<endl;
		
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator