计算100以内的素数的个数

以下代码是用来计算100以内的素数的个数,请把相应的空填上。


 

struct prime_number_node
{
	int prime_number;
	prime_number_node* next;
};

int calc_prime_number()
{
	prime_number_node* list_head = new prime_number_node();  //声明头结点
	list_head->next = NULL;
	list_head->prime_number = 2;
	prime_number_node* list_tail = list_head;
	for(int number = 3 ; number < 100 ; number++)
	{
		int remainder;
		prime_number_node* cur_node_ptr = list_head;
		while(cur_node_ptr != NULL)        //while遍历所有结点,看有被number整除的没。
		{
			remainder = number%cur_node_ptr->prime_number;
			if(remainder == 0)     //如果整除了,直接接受本次循环,number++,进入到下一次循环
			{
				break;  //1
			}
			else                 
			{
				cur_node_ptr = cur_node_ptr->next;  //2
			}
		}
		if(remainder != 0)     //遍历链表所有结点后,还是不能整除,那它就是素数。
		{
			prime_number_node* new_node_ptr = new prime_number_node(); 
			new_node_ptr->prime_number = number;
			new_node_ptr->next = NULL;
			list_tail->next = new_node_ptr;
			list_tail = list_tail->next;    //3
		}
	}
	int result = 0;
	while(list_head != NULL)
	{
		result++;
		prime_number_node* temp_ptr = list_head;
		list_head = list_head->next;
		delete temp_ptr;    //4        删除结点
	}
	return result;
}

你可能感兴趣的:(计算100以内的素数的个数)