题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1102
这道题跟那个蛇形矩阵有点相似,主要是找规律,分层,而且看是双层数还是单层数
代码:
View Code
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 using namespace std; 5 6 int main() 7 { 8 long n,i,w,m,r,c; 9 while(scanf("%ld",&n)&&n) 10 { 11 m = sqrt(n); 12 if(m%2) 13 { 14 w = n-m*m; 15 if(w == 0) 16 { 17 r = m; 18 c = 1; 19 } 20 else if(w <= m+1) 21 { 22 r = m+1; 23 c = w; 24 } 25 else 26 { 27 r = 2*m+2+m*m-n; 28 c = m+1; 29 } 30 } 31 else 32 { 33 w = n-m*m; 34 if(w == 0) 35 { 36 r = 1; 37 c = m; 38 } 39 else if(w <= m+1) 40 { 41 r = w; 42 c = m+1; 43 } 44 else 45 { 46 r = m+1; 47 c = (m+1)*(m+1)-n+1; 48 } 49 } 50 printf("%ld %ld\n",c,r); 51 } 52 return 0; 53 }