## 几个简单的算法 原

mskk

``````#include <stdio.h>
#include <math.h>

/*
*约瑟夫环解法
*/
unsigned joseph(unsigned begin, unsigned end, unsigned step)
{
unsigned loop = 0;
unsigned last = 0xFFFFFFFF;
if (begin >= end)
{
return 0xFFFFFFFF;
}

for (loop = begin + 1; loop <= end; loop++)
{
last = (last + step) % loop;
}

return last;
}

/*
*求最大公约数数,假定a > b
*/
unsigned gcd(unsigned a, unsigned b)
{
unsigned c = 0;
while (b != 0)
{
c = b;
b = a % b;
a = c;
}
return a;
}

/*
* 求100以内的素数的个数
*/

unsigned prime(unsigned n)
{
unsigned count = 0;
unsigned loop = 0;
unsigned i = 0;
bool flag = false;
for (loop = 2; loop < n; loop++)
{
flag = false;

if (loop == 2 || loop == 3 || loop == 5 || loop == 7)count++;

if (loop % 2 == 0 || loop % 3 == 0 || loop %5 == 0 || loop % 7 == 0)continue;

for (i = 2; i <= sqrt(loop); i++)
{
if (loop % i == 0){flag == true; break;}
}
if (true != flag)count++;
}

return count;
}

/*
* 求一个无符号整数中1的个数
*/

unsigned calc_1_bit_num(unsigned n)
{
unsigned count = 0;

while(n)
{
n &= n-1;
count++;
}

return count;
}

/*
* 交换两个整数的值
*/
void exchange(unsigned *lhs, unsigned *rhs)
{
*lhs = *lhs ^ *rhs;
*rhs = *lhs ^ *rhs;
*lhs = *lhs ^ *rhs;
}

int main(int argc, char**argv) {
// Prints hello message...
printf("josph problem:\n");
printf("begin:1,end:5,step:3-->%u\n", joseph(1, 5,2));
printf("begin:2,end:6,step:3-->%u\n",joseph(2,6,2));

printf("gcd:%u", gcd(8,7));

printf("prime:%u",prime(100));

unsigned a = 10;
unsigned b = 11;
exchange(&a, &b);
printf("a:%u, b:%u",a, b);
return 0;
}
``````

### 评论(2)

http://blog.csdn.net/chlele0105/article/details/38739919

http://blog.csdn.net/dszgf5717/article/details/23034099
2018软件设计师中级下午答题解题策略~

最近在琢磨软件设计师的下午题目，一开始发现看见流程图，逻辑困难的比较难受。因为流程图怎么说呢，是没学过吧！ 　　总结流程图的几个解体方法，首先看到一大堆的文字不要想着你就能很...

RunWsh
2018/11/02
0
0
Win32游戏制作之---五子棋的简单设计

loving_forever_
2016/07/17
0
0

第一次在博客园写东西，最近在重温算法，在这里记录一下，以便以后查阅及修改。 首先是优化冒泡排序 } } #endregion View Code 其次是计数排序 View Code 快速排序，这里用到了递归 publ...

cl789456
08/01
0
0
sbc(四)应用限流

pexels-photo-306198.jpeg 前言 在一个高并发系统中对流量的把控是非常重要的，当巨大的流量直接请求到我们的服务器上没多久就可能造成接口不可用，不处理的话甚至会造成整个应用不可用。 比...

crossoverJie
2017/08/11
0
0

hailx
01/28
0
0

oracle ORA-39700: database must be opened with UPGRADE option

ORA-01092: ORACLE instance terminated. Disconnection forced ORA-00704: bootstrap process failure ORA-39700: database must be opened with UPGRADE option 进程 ID: 3650 会话 ID: 29......

Tank_shu

2
0

ls_cherish

4
0

go4it

2
0
redis 学习2

2
0
Spring boot 静态资源访问

0. 两个配置 spring.mvc.static-path-patternspring.resources.static-locations 1. application中需要先行的两个配置项 1.1 spring.mvc.static-path-pattern 这个配置项是告诉springboo......

moon888

4
0