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条连线. 如果对所有点进行涂色(白/黑),判定是否存 ...
随机推荐
- 基于寄存器的VM
jvm是基于栈的,基于栈的原因是:实现简单,考虑的就是两个地方,局部变量和操作数栈 http://ifeve.com/javacode2bytecode/这几篇文章相当不错. http://redna ...
- NOIP2006 2k进制数
2^k进制数 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换 ...
- 编译并使用Lua语言
Lua是一个小巧的脚本语言,该语言设计的目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. 可扩展性.Lua的扩展性非常卓越,可以通过Lua代码或C代码扩展,很多功能可以通过外部库来扩 ...
- Android知识点
下面这段activity的配置可以防止onCreate方法在锁屏或者屏幕旋转时被调用,转而调用onConfigurationChanged方法,避免onCreate重复调用 <activity ...
- [OC Foundation框架 - 4] NSString的导出
void exportString() { NSString *str = @"Hello, 坑爹"; NSString *path = @"/Users/hello ...
- AVR抗干扰能力一般
在485通信中,为了方便测试,在485 芯片前端加下了插测好测试量,可是用TTL直接通信, 但是,在实际的PCB测试中,9600 的波特率下不小心用手碰了P5插针,会现大量的数量错误, 后来在Tx和R ...
- IAR stm8带库的工程模板
下载:http://pan.baidu.com/share/link?shareid=2243555626&uk=2483252218
- Struts2中DMI(动态方法调用)的错误问题(There is no Action mapped for namespace [/xxx] and action name [xxx!yyy] a)
默认的Struts.xml中是这样的 <constant name="struts.enable.DynamicMethodInvocation" value="f ...
- MySQL索引使用方法和性能优化
在自己的一个项目中,数据比较多,搜索也很频繁,这里找到一个建立索引很不错的文章,推荐下. 关于MySQL索引的好处,如果正确合理设计并且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的 ...
- Postfix配置Q&A
原文地址:http://space.doit.com.cn/51460/viewspace-4943.html 在配置Postfix中遇到的一些问题及相关的解决方法,希望在遇到相同的问题时能起参考的作 ...