求2009所在的位置
2009-10-25 20:06 Kevin Pan 阅读(181) 评论(0) 编辑 收藏 举报
/*
*1 5 7 19 21
*3 9 17 23
*11 15 25
*13 27
*29
*求2009的位置
*/
*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
7using 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 */
16int _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#include "stdafx.h"
5#include <iostream>
6
7using 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 */
16int _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