*1 5 7 19 21
*3 9 17 23
*11 15 25
*13 27
*29
*求2009的位置
*/
1
//
2009Position.cpp : 定义控制台应用程序的入口点。
2
//
3
4
#include
"
stdafx.h
"
5
#include
<
iostream
>
6
7
using
namespace
std;
8
/**/
/*
9
*1 5 7 19 21
10
*3 9 17 23
11
*11 15 25
12
*13 27
13
*29
14
*求2009的位置
15
*/
16
int
_tmain(
int
argc, _TCHAR
*
argv[])
17
{
18
int i = 0;
19
while(cin >> i)
20
{
21
int index = (i+1)/2;
22
int line = 1;
23
int count = 1;
24
while(count < index)
25
{
26
count += line + 1;
27
line ++;
28
}
29
//求出1 + 2 + 。。。 line - 1
30
int lineBeginNumber = (line * (line - 1) /2 + 1) * 2 - 1;//所在行的第一个数的值
31
int x = 1, y = 1;
32
if(line % 2 == 1)//奇数行
33
{
34
x = line - (i - lineBeginNumber)/2;
35
y = line - x + 1;
36
}
37
if(line % 2 == 0)//偶数行
38
{
39
y = line - (i - lineBeginNumber)/2;
40
x = line - y + 1;
41
}
42
cout << "position of " << i << " is in line " << line << ", x: " << x << ", y: " << y << endl;
43
}
44
return 0;
45
}
46
47

2

3

4

5

6

7

8


9

10

11

12

13

14

15

16

17



18

19

20



21

22

23

24

25



26

27

28

29

30

31

32

33



34

35

36

37

38



39

40

41

42

43

44

45

46

47
