Simple Math Problems
整理下《算法笔记》,方便查看。
一、最大公约数&最小公倍数
欧几里得定理:设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的更多相关文章
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- FZYZ-2071 A Simple Math Problem IX
P2071 -- A Simple Math Problem IX 时间限制:1000MS 内存限制:262144KB 状态:Accepted 标签: 数学问题-博弈论 ...
- A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- HDU 1757 A Simple Math Problem (矩阵乘法)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 5974 A Simple Math Problem
A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
随机推荐
- Scratch 第4课满天星
素材及视频下载 链接:https://pan.baidu.com/s/1qX0T2B_zczcLaCCpiRrsnA提取码:xfp8
- 【php】字符串
1.字符串的定义方式:1.单引号 ''2.双引号 ""3.定界符 <<<注意结束时的使用例:$str = <<<myStr字符串内容myStr; ...
- 数据结构-Python 字典
字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值 key=>value 对用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中 ,格式如下所示 d = ...
- 一、Python3.8的安装
一:什么是Python解释器 解释器(英语:Interpreter),又译为直译器,是一种电脑程序能够把高级编程语言一行一行直接转译运行. 解释器不会一次把整个程序转译出来,只像一位“中间人”,每次运 ...
- jvm入门及理解(四)——运行时数据区(堆+方法区)
一.堆 定义: Heap,通过new关键字创建的对象,都存放在堆内存中. 特点 线程共享,堆中的对象都存在线程安全的问题 垃圾回收,垃圾回收机制重点区域. jvm内存的划分: JVM内存划分为堆内存和 ...
- 【Selenium07篇】python+selenium实现Web自动化:PO模型,PageObject模式!
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第七篇博 ...
- Springboot启动流程简单分析
springboot启动的类为SpringApplication,执行构造函数初始化属性值后进入run方法: 然后返回ConfigurableApplicationContext(spring应用). ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information
Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...
- Python-气象-大气科学-可视化绘图系列(三)—— 地图上自动标注省会名称(demo调整中)(代码+示例)
本文为原创文章 本文链接:https://www.cnblogs.com/zhanling/p/12606990.html # -*- coding: utf-8 -*- ''' Author: He ...
- element动态添加表头的正确姿势
1. 第一步循环 el-table-column <el-table-column v-if="item.show" v-for="(item, index) in ...