Light OJ 1008

找规律。

  首先令n=sqrt(s),上取整。讨论当n为偶数时,若n*n-s<n则x=n,y=n*n-s+1否则x=-n*n+2*n+s-1,y=n;如果n为奇数,交换x,y即可,对称的。

Sample Input
3
8
20
25
Sample Output
Case 1: 2 3
Case 2: 5 4
Case 3: 1 5

 

*****************************************************

 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #include <algorithm>
 5 #include <math.h>
 6 #include <vector>
 7 using namespace std;
 8 typedef long long LL;
 9 const int INF = 0xfffffff;
10 const int maxn = 42000;
11 #define max(a,b)(a>b?a:b)
12 #define min(a,b)(a<b?a:b)
13 #define lson rt<<1
14 #define rson rt<<1|1
15 
16 int main()
17 {
18     int T,k=1;
19     long long s,n,a,x,y;
20 
21     scanf("%d", &T);
22 
23     while(T--)
24     {
25         scanf("%lld", &s);
26 
27         n=ceil(sqrt(double(s)));
28 
29         a=n*n-s;
30         if(a<n)
31         {
32             x=n;
33             y=a+1;
34         }
35         else
36         {
37             x=-n*n+2*n+s-1;
38             y=n;
39         }
40 
41         if(n%2!=0)swap(x,y);
42 
43         printf("Case %d: %lld %lld
",k++,x,y);
44     }
45     return 0;
46 }
原文地址:https://www.cnblogs.com/weiyuan/p/5753530.html