源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

求素数,用vector存储的实现方法

  • 时间:2022-05-31 11:08 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:求素数,用vector存储的实现方法
PS:如有不足之处,还望指正!
[u]复制代码[/u] 代码如下:
// tentotwo.cpp : 定义控制台应用程序的入口点。 #include "stdafx.h" #include <iostream> #include <vector> using namespace std; void GetPrimer(int n, vector<int>& vet) {  for (int i = 2; i <= n; i++)  {   vet.push_back(i);  }  vector<int>::iterator ite = vet.begin();  while (ite != vet.end())  {   vector<int>::iterator tmpite = ite + 1;   while (tmpite != vet.end())   {    if ((*tmpite)%(*ite) == 0)    {     tmpite = vet.erase(tmpite);    }    else    {     tmpite ++;    }   }     ite ++;  }  } int _tmain(int argc, _TCHAR* argv[]) {  vector<int> vet;  GetPrimer(100, vet);  vector<int>::iterator ite = vet.begin();  while (ite != vet.end())  {   cout << *ite << " ";   ite ++;  }  cout << endl;  return 0; }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部