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 网络编程(五)---DNS域名系统
1.域名系统定义 DNS计算机域名系统由域名服务器和域名解析器组成.通常输入的是网址就是一个域名. 2.域名查询 查询方式包括: 1)正向查询:由域名查找对应的IP(如:www.baidu.com—& ...
- javascript活动对象的理解——伪单例模式
在自己研究javascript各种设计模式的过程中,偶然写出的一段代码让自己理解的更深刻了,之所以称之为伪单例模式,是因为这段代码造成的结果很想单例模式,但是实际上是活动对象捣乱所造成的误会. 代码很 ...
- 【bzoj1300】大数计算器
题意: 求C(n,m) 如果C(n,m)的位数<=12 那么直接输出 否则按XXX...XXXXXXXXX的形式输出 题解: 这题之前打过 但是昨天又想出一种新的做法 先说下新的做法吧- - _ ...
- HDU-4691 Front compression 后缀数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4691 后缀数组模板题,求出Height数组后,对Height做RMQ,然后直接统计就可以了... // ...
- 使用Genymotion作Android开发模拟器:安装Genymotion、部署Genymotion Vitrue Device、安装Genymotion eclipse插件
偶然听说Genymotion Android模拟器非常强大,到网上了解一番后,决定从AVD又慢又卡中解脱出来,折腾了半天终于部署好了,体验了一下,果然启动快,运行流畅,现在总结一下经验教训,供大家参考 ...
- 解决dwr报错【 Error: java.lang.SecurityException: No class by name: service】
打开包含dwr的网页时后台报错: 警告: Names of known classes are: __System DwrQueryService 十二月 11, 2015 10:24:44 上午 o ...
- HDU5791--Two (DP)
题意:两个数列a,b,求相同的子序列有多少对,内容相同位置不同也算不同. 题解:dp[i][j]表示a数列前i个数个 b数列前j个数 有多少对 递推方程: dp[i][j] = dp[i-1][j-1 ...
- CentOS安装Redis Sentinel HA集群
安装了很多次,但是每次安装还要翻以前的配置,故列文备忘.下文依赖于2.x版本搭建主从节点实现基于sentinel机制的简单Redis HA(相对高可用Redis集群,真正高可用还要等3.0之后版本). ...
- [OC Foundation框架 - 5] NSString的常用方法
NSString *s1 = "; 1.比较 使用 == 号比较的是字符串地址 NSString *s4 = @"abcdefg"; NSStrin ...
- C/C++中的变量作用域
#include <iostream> using namespace std; int i = 1;int j = 2; int main(){ int i = 9; //C/ ...