整理下《算法笔记》,方便查看。

一、最大公约数&最小公倍数

欧几里得定理:设a,b均为正整数,那么gcd(a,b)=gcd(b,a%b)。

若,定理就先交换a和b。

注意:0和任意正整数a的gcd是a。

//最大公约数
int gcd(int a,int b)
{
return !b ? a : gcd(b,a % b);
}

设最大公约数为res,最小公倍数lcm即为。

二、分数

PAT甲1088是比较经典的分数处理问题,求2个分数的和、差、积、商,输出最简形式。

表示、化简、运算、输出,代码阐释得很清楚。

#include <cstdio>
#include <algorithm> using namespace std; typedef long long ll; ll gcd(ll a,ll b)
{
return !b ? a : gcd(b,a % b);
} struct Fraction{
ll nume,deno;
}; Fraction reduction(Fraction a)
{
if(a.deno < 0)
{
a.deno = -a.deno;
a.nume = -a.nume;
}
if(a.nume == 0)
{
a.deno = 1;
}
else
{
int d = gcd(abs(a.nume),abs(a.deno));
a.nume /= d;
a.deno /= d;
}
return a;
} Fraction add(Fraction a,Fraction b)
{
Fraction res;
res.deno = a.deno * b.deno;
res.nume = a.deno * b.nume + a.nume * b.deno;
return reduction(res);
} Fraction sub(Fraction a,Fraction b)
{
Fraction res;
res.deno = a.deno * b.deno;
res.nume = a.nume * b.deno - a.deno * b.nume;
return reduction(res);
} Fraction times(Fraction a,Fraction b)
{
Fraction res;
res.deno = a.deno * b.deno;
res.nume = a.nume * b.nume;
return reduction(res);
} Fraction divide(Fraction a,Fraction b)
{
Fraction res;
res.deno = a.deno * b.nume;
res.nume = a.nume * b.deno;
return reduction(res);
} void showFrac(Fraction a)
{
a = reduction(a);
if(a.nume < 0)
{
printf("(");
}
if(a.deno == 1)
{
printf("%lld",a.nume);
}
else if(abs(a.nume) > abs(a.deno))
{
printf("%lld %lld/%lld",a.nume / a.deno,abs(a.nume) % a.deno,a.deno);
}
else
{
printf("%lld/%lld",a.nume,a.deno);
}
if(a.nume < 0)
{
printf(")");
}
} int main()
{
Fraction a,b;
scanf("%lld/%lld%lld/%lld",&a.nume,&a.deno,&b.nume,&b.deno); showFrac(a);
printf(" + ");
showFrac(b);
printf(" = ");
showFrac(add(a,b));
printf("\n"); showFrac(a);
printf(" - ");
showFrac(b);
printf(" = ");
showFrac(sub(a,b));
printf("\n"); showFrac(a);
printf(" * ");
showFrac(b);
printf(" = ");
showFrac(times(a,b));
printf("\n"); showFrac(a);
printf(" / ");
showFrac(b);
printf(" = ");
if(b.nume == 0)
{
printf("Inf\n");
}
else
{
showFrac(divide(a,b));
printf("\n");
} return 0;
}

三、素数

1、判断素数

bool isPrime(int a)
{
if(a <= 1) //1不是素数,也不是合数
return false;
int tmp = (int)sqrt(1.0 * a);
for(int i = 2;i <= tmp;i++)
{
if(a % i == 0)
return false;
}
return true;
}

2、打素数表

第一种方法是枚举判断。

const int maxn = 10010;
int prime[maxn],num = 0; void Prime_table()
{
for(int i = 2;i < maxn;i++)
{
if(isPrime(i))
{
prime[num++] = i;
}
}
}

第二种是Eratosthenes筛法,复杂度比枚举更优,代码更短。

const int maxn = 10010;
int prime[maxn],num = 0;
bool p[maxn] = {false}; //i为素数,p[i]为false void Prime_table()
{
for(int i = 2;i < maxn;i++)
{
if(p[i] == false)
{
prime[num++] = i;
for(int j = i + i;j < maxn;j += i)
{
p[j] = true;
}
}
}
}

3、分解质因子

注意:1要特判。

//存储
struct factor{
int x,cnt; //x为质因子,cnt为该质因子个数
}fac[20];
int num = 0;  //记录不同因子个数
//枚举小于等于sqrt(n)内的所有质因子,判断哪个是n的因子
for(int i = 0;prime[i] <= sqrt(n);i++)
{
if(n % prime[i] == 0)
{
fac[num].x = prime[i];
fac[num].cnt = 0;
while(n % prime[i] == 0)
{
fac[num].cnt++;
n /= primep[i];
}
num++;
}
} //如果n仍然大于1,说明n有一个大于sqrt(n)的质因子
if(n != 1)
{
fac[num].x = n;
fac[num++].cnt = 1;
}

Simple Math Problems的更多相关文章

  1. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. FZYZ-2071 A Simple Math Problem IX

    P2071 -- A Simple Math Problem IX 时间限制:1000MS      内存限制:262144KB 状态:Accepted      标签:    数学问题-博弈论    ...

  5. A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  6. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  7. HDU 1757 A Simple Math Problem(矩阵)

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  8. HDU 1757 A Simple Math Problem (矩阵乘法)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. hdu 5974 A Simple Math Problem

    A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

随机推荐

  1. 【摸鱼向】UE4的AI模块探索手记(1)

    前言 之前实现了自主创作的角色导入进UE4并成功控制其进行一系列动作,但目前的样子距离基本的游戏架构还差了一个很大的模块:NPC,而这部分是由电脑来进行自动控制,所以,我有一句话不知当讲不当讲(对,我 ...

  2. 从JDK源码学习HashSet和HashTable

    HashSet Java中的集合(Collection)有三类,一类是List,一类是Queue,再有一类就是Set. 前两个集合内的元素是有序的,元素可以重复:最后一个集合内的元素无序,但元素不可重 ...

  3. 21 static 静态的使用及特点

    /* * static:他是一个关键字,用来修饰成员变量和成员方法 * static特点: * 被所有的对象所共享 * 可以直接使用类名来调用 * 静态所修饰的成员加载优先于对象,随着类的加载而加载 ...

  4. Python zipfile模块学习

    转载自https://www.j4ml.com/t/15270 import zipfile import os from zipfile import ZipFile class ZipManage ...

  5. alg-最长公共子序列

    class Solution { public: std::string LongestCommonSubsequence(const std::string& s1, const std:: ...

  6. C语言小练习之学生信息管理系统

    C语言小练习之学生信息管理系统 main.c文件   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...

  7. 代码质量管理 SonarQube 系列之 安装

    简介 SonarQube 是一个开源的代码质量管理系统. 功能介绍: 15种语言的静态代码分析 Java.JavaScript.C#.TypeScript.Kotlin.Ruby.Go.Scala.F ...

  8. 如何批量修改文件后缀名,python来帮你

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http ...

  9. 接口测试中实际发生的几个问题——python中token传递

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:AFKplayer PS:如有需要Python学习资料的小伙伴可以加点 ...

  10. stand up meeting 1/14/2016

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云  主要对生词本卡片的整体设计做修改:协助主程序完成popup部分 ...