51nod 1080:两个数的平方和

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 收藏
 关注
给出一个整数N,将N表示为2个整数i j的平方和(i <= j),如果有多种表示,按照i的递增序输出。
例如:N = 130,130 = 3^2 + 11^2 = 7^2 + 9^2 (注:3 11同11 3算1种)
Input
一个数N(1 <= N <= 10^9)
Output
共K行:每行2个数,i j,表示N = i^2 + j^2(0 <= i <= j)。
如果无法分解为2个数的平方和,则输出No Solution
Input示例
130
Output示例
3 11
7 9

如果是之前,估计还在暴力。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

long long val[31625];

int main()
{
	long long i,j=31624,temp,x;
	int flag=0;
	cin>>x;

	for(i=0,j=31624;i<=j;)
	{
		temp=i*i+j*j;
		if(temp==x)
		{
			cout<<i<<" "<<j<<endl;
			i++;
			j--;
			flag=1;
		}
		else if(temp>x)
		{
			j--;
		}
		else if(temp<x)
		{
			i++;
		}
	}
	if(flag==0)
		cout<<"No Solution"<<endl;
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4899599.html