关闭

常用算法

1784人阅读 评论(0) 收藏 举报

1、求素数

参考:http://www.math.utah.edu/classes/216/assignment-07.html

自然数由素数(质数)与合数组成。素数如 2 3 5 7 11 13 17 19 23 29等等,只能被1整除。
The Sieve of Eratosthenes埃拉托尼筛子是求素数的高效算法。

const int N = 100; //求100以内的素数
int z[N + 1] = {0}; 
//初始化数组以记录是否为素数,多一个是为了下标清晰不至于困扰于 j-1? 这样的问题
//外圈循环:从自然数2开始,对每一个数进行操作,直到N/2。
//内圈循环:从外圈循环因子的倍数开始,对应数组元素置1,直到N。

for (int i = 2; i <= N / 2; i++)
 for (int j = 2 * i; j <= N; j += i)
  z[j] = 1;
int Num = 0; //记录素数个数
for (i = 2; i <= n; i++)
 if (z[j] == 0)
  Num++;

补充:int z[N] = {0};  //初始化数组以记录是否为素数

其实这只是对第一个元素赋0值,对其余元素赋默认值0

改成int z[N] = {1}; 

数组的值将是 1 0 0 0 0 0 0 0 ...

2、最大公约数和最小公倍数

//求最大公约数
void GreatestCommonDivisor ()
{
 while (1)
 {
  int m = 0, n = 0;
  cin >> m >> n;
 
  if (m == 0 || n == 0) return;
  if (m <= 1 || n <= 1)
  {
   cout << "Values should be larger than 1" << endl;
   continue;
  }
  int a = (m > n) ? m : n;
  int b = (m > n) ? n : m;
  int c = 0;
  while ((a % b) != 0)
  {
   c = a % b;
   a = b;
   b = c;
  }
  cout << m << " " << n << " GreatestCommonDivisor = " << b << endl;
 }
}
//求最小公倍数
void LeastCommonMultiply ()
{
 //两数相乘再除以最小公倍数
 while (1)
 {
  int m = 0, n = 0;
  cin >> m >> n;

  if (m == 0 || n == 0) return;
  if (m <= 1 || n <= 1)
  {
   cout << "Values should be larger than 1" << endl;
   continue;
  }
  int a = (m > n) ? m : n;
  int b = (m > n) ? n : m;
  int c = 0;
  while ((a % b) != 0)
  {
   c = a % b;
   a = b;
   b = c;
  }
  cout << m << " " << n << " GreatestCommonDivisor = " << b << endl;
  cout << m << " " << n << "LeastCommonMultiply = " << m * n / b << endl;
 }
}

1、公约数、公倍数
 int m = 0, n = 0 ,r = 0;
 cin >> m >> n;
 if (m < n)
 {
  r = n;
  n = m;
  m = r;
 }
 r = m % n;
 while (r != 0)
 {
  m = n;
  n = r;
  r = m % n;
 }
 cout << n << endl;
 cout << a * b / n << endl;

2、判断素数
bool prime (int m)
{
 int r = sqrt (m);
 for (int i = 2; i <= r; i++)
  if (m % i == 0)
   return false;
 return true;
}

3、求素数
void prime ()
{
 const int N = 100;
 int a[101] = {0};
 for (int i = 2; i <= N / 2; i++)
  for (int j = 2 * i; j <= N; j += i)
   a[j] = 1;
 for (i = 1; i <= N; i++)
  if (a[i] == 0)
   cout << i << '/t';
}

4、歌德巴赫猜想 ( >=6 的偶数可分解成二素数之和)
bool gedebahe (int m)
{
 for (int r = 1; r <= m /2; r++)
  if (prime (r) && prime (m - r))
   return true;
 return false;
}

5、生成随机数
void rands (int* a, int num)
{
 srand ((unsigned int)time (NULL));

 for (int i = 0; i < num; i++)
 {
  a[i] = rand () % 10;
  cout << a[i] << endl;
 }
}

6、选择法排序 (升序)
void select_sort (int* a)
{
 int min = 0;
 for (int i = 0; i < 9; i++) //从左到右的循环因子,用以被比较
  for (int j = i + 1; j < 10; j++)
   if (a[i] > a[j])
   {
    min = a[j]; a[j] = a[i]; a[i] = min;
   }
}

7、冒泡法排序 (升序)
void pop_sort (int* a)
{ for (int i = 0; i < 9; i++)
//从右到左的循环因子,用以计算里圈循环的空位
  for (int j = 0; j < 9 - i; j++)
   if (a[j] > a[j + 1])
   {
    min = a[j]; a[j] = a[j + 1]; a[j + 1] = min;
   }
}

8、有序数组合并 (升序)
void unite (int* a, int numA, int* b, int numB, int* c, int numC)
{
 int i = 0, j = 0, k = 0;
//各自的循环因子,互不干扰
 for (; i < numA && j < numB; k++)
 {
  if (a[i] < b[j])
  {
   c[k] = a[i];
   i++;
  }
  else
  {
   c[k] = b[j];
   j++;
  }
 }
 if (i < numA)
  for (; i < numA; k++, i++)
   c[k] = a[i];
 else
  for (; j < numB; k++, j++)
   c[k] = a[j];
}

9、有序数组中折半查找
int half_find (int* a, int num, const int dst)
{
 int lower = 0, upper = num - 1, mid = (upper + lower) / 2;
 while (dst != a[mid])
 {
  if (dst > a[mid])
   lower = mid + 1;
  else
   upper = mid - 1;
  mid = (upper + lower) / 2;
  if (lower >= upper)
   return -1;
 }
 return mid;
}

10、方阵转置
void reverse_matrix (int a[3][3], int num)
{
 int t = 0;
 for (int i = 0; i < num; i++)
 {
  for (int j = i; j < num; j++)
  {
   t = a[i][j];
   a[i][j] = a[j][i];
   a[j][i] = t;
  }
 }
}

11、迭代法开平方
float iteration (float a)
{
 float b = a / 2;
 int i = 0;
 while (abs (b * b - a) > 0.0001)
 {
  if (b * b > a)
   b -= 0.001f;
  else
   b += 0.001f;
  if (++i > 999999999)
  {
   cout << "NOFOUND/n";
   return -1;
  }
 }
 return b;
}

12、数值转换
void trdec(int idec, int ibase, char* ret)
{
 if (idec < ibase) //数值大于9时,应该用字母来拓展
 {
  ret[0] = '0' + idec;
  ret[1] = '/0';
  return;
 }
 int i = 0, j = 0;
 while (idec / ibase != 0)
 {
  ret[i++] = '0' + idec % ibase;
  idec /= ibase;
 }
 ret[i] = '0' + 1;
 for (; j <= i / 2; j++)
 {
  ret[i + 1] = ret[j];
  ret[j] = ret[i - j];
  ret[i - j] = ret[i + 1];
 }
 ret[i + 1] = '/0';
}

13、统计单词数
int count_words (char* a)
{
 int word = 0, count = 0;
 char* p = a;
 while (*p != '/0')
 {
  if(*p < 'z' && *p > 'A')
  {
   if (0 == word)
   {
    word = 1;
    count++;
   }
  }
  else
   word = 0;
  p++;
 }
 return count;
}

14、穷举法计算,100张5、1、0.5RMB组合成100元
void exhaustion ()
{
 int x = 0, y = 0, z = 0;
 for (; x < 100 / 5; x++)
  for (y = 0; y < 100 / 1; y++)
  {
   z = 100 - x - y;
   if (5*x + 1*y + 0.5*z == 100)
    cout << x << "/t" << y << "/t" << z << endl;
  }
}

15、递归求阶乘
int fabtorial (int a)
{
 if (0 == a)
  return 1;
 else
  return a * fabtorial (a - 1);
}

16、直接插入排序 (升序)
void InsertSort (int* a, int num)
{
 int temp = 0, i = 0, j = 0;
 for (i = 1; i < num; i++)
 {
  temp = a[i];
  //只有大于的时候,才进行移动,边比较查找边移动
  for (j = i - 1; temp < a[j] && j >= 0; j--)
   a[j + 1] = a[j];
  //插入数据
  a[j + 1] = temp;
 }
}

17、折半插入排序 (升序)
void BiInsertionSort (int* a, int num)
{
 int temp = 0, high = 0, low = 0, mid = 0, i = 0, j = 0;
 for (i = 1; i < num; i++)
 {
  temp = a[i]; high = i - 1; low = 0;
  //折半查找插入位置
  while (low <= high)
  {
   mid = (low + high) / 2;
   if (temp < a[mid])
    high = mid - 1;
   else
    low = mid + 1;
  }
  //移动插入点后的数据
  for (j = i - 1; j >= low; j--)
   a[j + 1] = a[j];
  //插入数据
  a[low] = temp;
 }
}

18、定时器
void sleep (int second)
{
 time_t st = 0, et = 0;
 time (&st);
 et = st;
 while (et - st < second)
 {
  st = et;
  time (&et);
 }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:167501次
    • 积分:2111
    • 等级:
    • 排名:第18734名
    • 原创:32篇
    • 转载:38篇
    • 译文:1篇
    • 评论:15条
    文章分类
    最新评论