#1186 : Coordinates

题目:
1.p和q都不大,直接遍历求出所有的因数。
2.原计划遍历到p/2或者q/2的,但是发现当p==1或者q==1时,会直接跳出没有遍历,于是直接改为遍历到p或q。

测试用例:1 1

输出应该为:1 1

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Give you two integers P and Q. Let all divisors of P be X-coordinates. Let all divisors of Q be Y-coordinates.

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

AC代码:

/*
题目:
1.p和q都不大,直接遍历求出所有的因数。
2.原计划遍历到p/2或者q/2的,但是发现当p==1或者q==1时,会直接跳出没有遍历,于是直接改为遍历到p或q。

*/
/*

测试用例:

1 1
正确答案:
1 1

*/

#include<string>
#include <iomanip>
#include<fstream>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
//#include<stack>
#include<vector>
#include <algorithm>
using namespace std;

/*
函数名  :main
函数功能:主函数
*/
int main(void)
{

	int p, q;
	cin >> p>>q;
	set<int> pDivisors;
	set<int> qDivisors;
	//求出所有的因数
	for (int i = 1; i <= max(p, q); i++)
	{
		if (p%i == 0)
		{
			pDivisors.insert(i);
			pDivisors.insert(p / i);
		}
		if (q%i == 0)
		{
			qDivisors.insert(i);
			qDivisors.insert(q / i);
		}
	}

	//进行输出
	vector<int> qDiv(qDivisors.size(), 0); 
	int i = 0;
	for (set<int>::iterator ite = qDivisors.begin(); ite != qDivisors.end(); ite++,i++)
	{
		qDiv[i] = *ite;
	}

	for (set<int>::iterator ite = pDivisors.begin(); ite != pDivisors.end(); ite++)
	{
		for (int j = 0; j < qDiv.size(); j++)
		{
			printf("%d %d\n", *ite, qDiv[j]);
		}
	}

	return  0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注