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条连线. 如果对所有点进行涂色(白/黑),判定是否存 ...
随机推荐
- CCF 认证4
题意:求强联通分量 Tarjan算法 #include<iostream> #include<stdio.h> #include<stdlib.h> #includ ...
- HW5.35
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- leetcode@ [263/264] Ugly Numbers & Ugly Number II
https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...
- [iOS基础控件 - 5.2] 查看大图、缩放图片代码(UIScrollView制作)
原图: 900 x 1305 拖曳滚动: 缩放: 主要代码: // // ViewController.m // ImageZoom // // Created by ...
- 转载LINQ TO Entity 在数据库发生更改时更新实体数据模型 .edmx 文件
转载原出处:http://blog.csdn.net/litao2/article/details/8629335 在“模型浏览器”中,右击 .edmx 文件,然后选择“从数据库更新模型”. 模型更新 ...
- Jenkins 实际项目操作
.Abstract { padding: 15px; border: dotted 2px #999; color: #999; font-family: "Microsoft Yahei& ...
- Latex 中宽度的设置和理解
\textwidth, 文本区域的全部宽度 \columnwidth, 文本中一列的宽度,单栏或者多栏的情况下,值是不同的 但是,一旦\textwidth, \columnwidth, \linewi ...
- 黑马程序猿_7K面试题之交通灯系统
交通灯信号模拟系统 一.概述 模拟实现十字路口的交通灯管理系统逻辑,详细需求例如以下:(需求直接来源于老师的文档) ① 异步随机生成依照各个路线行驶的车辆. 比如: 由南向而来去往北向的车辆 ...
- hdu 5459 Jesus Is Here 数学
Jesus Is Here Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- 【JSP】Cookie的使用及保存中文,并用Cookie实现购物车功能
Cookie是服务器存放在客户端的一些数据,比如密码,以及你曾经访问过的一些数据. 设置Cookie //设置cookie Cookie cookie = new Cookie("TOM&q ...