csu oj 1344: Special Judge

Description

Given a positive integer n, find two non-negative integers a, b such that a2 + b2 = n.

Input

The first line contains the number of test cases T (1 <= T <= 1000).
For each test case, there is only one line with an integer n (1 <= n <= 109) as defined above.

Output

For each test case, output two integers a, b separated by a single space such that a2 + b2 = n. If there are multiple solutions, anyone will be accepted. If there is no solution, output “-1” (without quotation marks) instead.

Sample Input

4
2
3
4
4

Sample Output

1 1
-1
0 2
2 0

Hint

还记得我们在“A Sample Problem”中说到的OJ的判题形式吗?这个题主要让我们来熟悉一下OJ的另一种名为Special Judge的判题形式。所谓的Special Judge是指OJ将使用一个特定的程序来判断我们提交的程序的输出是不是正确的,而不是单纯地看我们提交的程序的输出是否和标准输出一模一样。
一般使用Special Judge都是因为题目的答案不唯一,更具体一点说的话一般是两种情况:
(1)题目最终要求我们输出一个解决方案,而且这个解决方案可能不唯一。比如这个题目就是这样的,只要求我们输出一组a、b满足a^2 + b^2 = n,而对于不同的n,可能有多组a、b满足这一条件,像n=4时,就有a=2、b=0和a=0、b=2这两组解,这时我们输出任意一组解都可以。
(2)题目最终要求我们输出一个浮点数,而且会告诉我们只要我们的答案和标准答案相差不超过某个较小的数就可以,比如0.000001。这种情况我们只要保证我们的中间运算过程尽可能精确就可以了,并且最后输出答案的时候保留的小数位数足够多就行了,比如如果要求我们的答案和标准答案相差不超过0.01,那么一般我们保留3位小数、4位小数等等都是可以的,而且多保留几位小数也没什么坏处。
为了能让大家更清楚的了解Special Judge的原理,这里我把这个题目的执行Special Judge功能的程序的源代码贴出来了

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
using namespace std;
int main()
{
    int t,n,a,b;
    cin>>t;
    
    while(t--)
    {
        bool flag=false;
        cin>>n;
        for(int a=0;a*a<=n/2;a++)
        {
            b=sqrt(n-a*a);
            if(a*a+b*b==n)
            {
                flag=true;
                break;
            }
        }
        if(flag)
            cout<<a<<" "<<b<<endl;
        else
            cout<<"-1"<<endl;
    
    } 
    return 0;
}
原文地址:https://www.cnblogs.com/hcw110/p/9713163.html