算法设计与分析书中程序(第11章).docVIP

  • 0
  • 0
  • 约1.31千字
  • 约 2页
  • 2015-08-23 发布于安徽
  • 举报
【程序11-1】 标识重复元素的拉斯维加斯算法 template class T int Repeated Element(T a[],int n,T x) { while(1){ int i=rand()% n;int j=rand()% n; if ((i!=j)(a[i]==a[j])) { x=a[i];return i; } } } 【程序11-2】 伪素数测试 int ModExp(int a,int n) { int y=1,m=n-1,z=a; while(m0){ while (m%2==0){ z=(z*z)%n;m/=2; } m--; y=(y*z)%n; } return y; } bool PseudoPrime(int a,int n) { if(ModExp(a,n)!=1)return false; //n必定是合数 else return true; //n是素数或基于a的伪素数 } 【程序11-3】 合数性检测 bool Witness(int a,int n) { int y=1,m=n-1,z=a; while(m0){ while (m%2==0){ int x=z; z=(z*z)%n; m/=2;

文档评论(0)

1亿VIP精品文档

相关文档