Advertisement
Guest User

UVa: 10920 - Spiral Tap

a guest
Sep 2nd, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <cstdio>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. ll p;
  7. int sz;
  8.  
  9. int main() {
  10.     //freopen("input.txt", "r", stdin);
  11.     while(scanf("%d %d", &sz, &p), (sz || p)){
  12.         ll increment = 1, current = 1;
  13.         int line, column, x, y;
  14.         x = y = sz / 2;
  15.         while(true){
  16.             if(current == p){
  17.                 line = y; column = x;
  18.                 break;
  19.             }
  20.             if((p > current) && (p <= current + increment)){
  21.                 column = x;
  22.                 line = y + (p - current);
  23.                 break;
  24.             } else {
  25.                 current += increment;
  26.                 y += increment;
  27.             }
  28.             if((p > current) && (p <= current + increment)){
  29.                 column = x - (p - current);
  30.                 line = y;
  31.                 break;
  32.             } else {
  33.                 current += increment;
  34.                 x -= increment;
  35.             }
  36.             increment++;
  37.             if ((p > current) && (p <= current + increment)){
  38.                 line = y - (p - current);
  39.                 column = x;
  40.                 break;
  41.             } else {
  42.                 y -= increment;
  43.                 current += increment;
  44.             }
  45.             if((p > current) && (p <= current + increment)){
  46.                 column = x + (p - current);
  47.                 break;
  48.             } else {
  49.                 current += increment;
  50.                 x += increment;
  51.             }
  52.             increment++;
  53.         }
  54.         printf("Line = %d, column = %d.\n", line + 1, column + 1);
  55.     }
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement