2001. Scavenger Hunt

2001. Scavenger Hunt

Constraints

Time Limit: 1 secs, Memory Limit: 64 MB

Description

Farmer John has scattered treats for Bessie at special places in the pasture. Since everyone knows that smart cows make tasty milk, FJ has placed the treats at locations that require Bessie to think. He has given her two numbers, P and Q (1 <= P <= 6,000; 1 <= Q <= 6,000), and she has to check every point in the pasture whose x-coordinate is a factor of P and whose y-coordinate is a factor of Q to find her treat. 
Suppose FJ gives Bessie P = 24 and Q = 2. Here are all of their respective factors: 
P = 24 => 1, 2, 3, 4, 6, 8, 12, 24 
Q = 2 => 1, 2 
Bessie would thus check grid locations: (1, 1), (1, 2), (2, 1), (2, 2), (3, 1)... 
Help Bessie by printing all of the points she ought to check.

Input

* Line 1: Two space separated integers: P and Q

Output

* Lines 1..?: A complete list of unique pairs of space-separated integers sorted by the first number and, if tied, the second number: a factor of P followed by a factor of Q

Sample Input

24 2

Sample Output

1 1
1 2
2 1
2 2
3 1
3 2
4 1
4 2
6 1
6 2
8 1
8 2
12 1
12 2
24 1
24 2

Problem Source

2010中山大学新手赛-网络预选赛


这个我的代码用时很长……不知道为什么

我的代码:

#include <iostream>

using namespace std;

int main () {
	int p, q;
	cin>>p>>q;
	int pp[3000] = {0};
	int qq[3000] = {0};
	int j = 1, k = 1;
	for (int i = 1; i <= p; i++)
		if (p % i == 0)
			pp[j++] = i;
	for (int i = 1; i <= q; i++)
		if (q % i == 0)
			qq[k++] = i;
	for (int i = 1; i < j; i++)
		for (int m = 1; m < k; m++)
			cout<<pp[i]<<' '<<qq[m]<<endl;
	
	//system("pause");
	return 0;
}

其他人代码:

链接:http://163yescpk.blog.163.com/blog/static/1418902882010111311229716/

代码:

#include <iostream>

using namespace std;

void divide(int xx , int a[6000])
{
	a[0] = 0;
    for (int i = 1 ; i <= xx ; ++i)
        if (xx % i == 0) a[++a[0]] = i;
}

int main()
{
    int x , y;
    cin >> x >> y;
    int a[6000];
    int b[6000];
    divide(x , a);
    divide(y , b);
    for (int i = 1 ; i <= a[0] ; ++i)
     for (int j = 1 ; j <= b[0] ; ++j)
        cout << a[i] << " " << b[j] << "\n";
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值