hihocoder 1186
1186 : Coordinates
描述
Give you two integers P and Q. Let all divisors(因子,约数) of P be X-coordinates. Let all divisors of Q be Y-coordinates(Y坐标).
For example, when P=6 and Q=2, we can get the coordinates (1,1) (1,2) (2,1) (2,2) (3,1) (3,2) (6,1) (6,2).
You should print all possible coordinates in the order which is first sorted by X-coordinate when coincides, sorted by Y-coordinate.
输入
One line with two integers P and Q(1 <= P, Q <= 10000).
输出
The output may contains several lines , each line with two integers Xi and Yi, denoting the coordinates.
- 样例输入
-
6 2
- 样例输出
1 1
1 2
2 1
2 2
3 1
3 2
6 1
6 2
- 题目大意:
- 给定数字P,Q,求出所有P和Q的约数p,q能够组成的不重复数字对
(p,q)
- 解题思路:
- 本题中需要求出P,Q所有约数组成的数字对,因此我们需要先将P,Q两个数字所有的约数分别找出来,再依次组合后输出。求解一个数字P的所有约数,
可以依次枚举1..P分别进行检查是否能够被P整除,也可以降低复杂度只枚举1..sqrt(P),具体实现可以参考如下伪代码: -
// 枚举 1.. P
p_count = 0
For i = 1 .. P
If (P mod i == 0) Then
p_divisors[p_count] = i
p_count = p_count + 1
End If
End For// 枚举 1 .. sqrt(P)
p_count = 0
For i = 1 .. sqrt(P)
If (P mod i == 0) Then
p_divisors[p_count] = i
p_count = p_count + 1
If (P div i > i) Then
// 这个判断语句的作用是为了防止当 P 为平方数时
// 若(P div i >= i)则会将 sqrt(P) 重复加入约数中
p_divisors[p_count] = P div i
p_count = p_count + 1
End If
End If
End For
// 用这种方法得到的约数序列需要进行排序
Sort(p_divisors)
在得到p_divisors
和q_divisors
之后,再通过双重循环,即可将所有的数字对打印出来:
For i = 0 .. p_count - 1
For j = 0 .. q_count - 1
Output(p_divisors[i] + ' ' + q_divisors[j])
End For
End For
到此,本题也就顺理解决。
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std; int p_divisors[];
int q_divisors[]; int main(){
int i, j, p, q, p_count = , q_count = ;
scanf("%d %d", &p, &q); for(i = ; i <= p; i++){
if(p % i == ){
p_divisors[p_count] = i;
p_count++;
}
} for(i = ; i <= q; i++){
if(q % i == ){
q_divisors[q_count] = i;
q_count++;
}
}
/*
for(int i = 1; i <= sqrt(p); i++){
if(p % i == 0){
p_divisors[p_count] = i;
p_count++;
if(p / i > i){
p_divisors[p_count] = p / i;
p_count++;
}
}
}
sort(p_divisors, p_divisors + p_count); for(int i = 1; i <= sqrt(q); i++){
if(q % i == 0){
q_divisors[q_count] = i;
q_count++;
if(q / i > i){
q_divisors[q_count] = q / i;
q_count++;
}
}
}
sort(q_divisors, q_divisors + q_count);*/ for(i = ; i < p_count; i++){
for(j = ; j < q_count; j++){
printf("%d %d\n", p_divisors[i], q_divisors[j]);
}
} return ; }
hihocoder 1186的更多相关文章
- hihocoder -1121-二分图的判定
hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...
- Hihocoder 太阁最新面经算法竞赛18
Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...
- hihoCoder太阁最新面经算法竞赛15
hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...
- 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II
http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...
- 【hihocoder#1413】Rikka with String 后缀自动机 + 差分
搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...
- 【hihoCoder】1148:2月29日
问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...
- 【hihoCoder】1288 : Font Size
题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...
- 【hihoCoder】1082: 然而沼跃鱼早就看穿了一切
题目:http://hihocoder.com/problemset/problem/1082 输入一个字符串,将其中特定的单词替换成另一个单词 代码注意点: 1. getline(istre ...
- 【hihoCoder】1121:二分图一·二分图判定
题目 http://hihocoder.com/problemset/problem/1121 无向图上有N个点,两两之间可以有连线,共有M条连线. 如果对所有点进行涂色(白/黑),判定是否存 ...
随机推荐
- 用Python写的批量文件重命名
有些时候下载图片或其他文件,文件名都怪怪的,可选的办法是下载一个文件批量重命名的软件.当然,如果想自己'DIY'一把的话编个Python脚本最好不过了. 下面的代码实现的对指定类型的文件进行批量重 ...
- uvalive 3890 Most Distant Point from the Sea
题意:求一个凸多边形中一点到边的最大距离. 思路:转换成在多边形内部,到每边距离为d的直线所围成的内多边形是否存在.也就是,二分距离+半平面交. #include<cstdio> #inc ...
- jenkins api调用
在使用jenkins的时候,有时候需要其他外部调用,下面是调用方法,不定期更新 job调用 使用user和password: curl -X POST "jobPath/buildWithP ...
- dataStructure@ Binary Search Tree
#include<iostream> #include<cstdio> #include<cstring> #include<limits> #incl ...
- python 递归函数
在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以看出: f ...
- Cocos2d-x 重写draw方法绘制直线等图形时被遮挡覆盖问题的一种解决方案
最近在学习过程cocos2dx的过程中需要使用到绘制直线的功能,所以我就采用了引擎中 ccDrawLine 方法,然后重写 draw 方法,在该方法中绘制直线. 但是出现了一个问题,那就是绘制的图形被 ...
- 重看Decorator Pattern,联想到Delegate传递及Flags Enum--欢迎拍砖!
话说装饰模式(Decorator)的动机是“动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活.[GOF <设计模式>]”.再次学到该模式,有感 ...
- Oracle- 提示查询结果不可更新,请使用...更新结果。
我们在对Oracle数据库进行操作时,有时会在查询完结果后想要对其中的某些数据进行操作,当我们点击编辑(一个锁标志)是,会提示我们上述问题中的错误:这些查询结果不可更新,请使用ROWI或者SELECT ...
- C#.NET数据库访问类DBHelper
这是一个与C# .NET通用的数据库访问类,包含了工厂模式.事务处理等安全机制. 调用方式: DBHelper db = new DBHelper(); DbCommand cmd = db.GetS ...
- SQLite使用教程5 分离数据库
http://www.runoob.com/sqlite/sqlite-detach-database.html SQLite 分离数据库 SQLite的 DETACH DTABASE 语句是用来把命 ...