题目:
已知数A和数B,且A>B
数列一:第一个数为1/(A+B),增量为2/(A+B),往下递增,即1/(A+B)、3/(A+B)、5/(A+B)……
数列二:第一个数为1/(A-B),增量为2/(A-B),往下递增,即1/(A-B)、3/(A-B)、5/(A-B)……
两个数列放到一起排序,求排序后的第n个数是多少?
答案 :
没测试过
1 using System;
2
3 namespace ConsoleApplication1
4 {
5 class Program
6 {
7 static void Main(string[] args)
8 {
9 Console.WriteLine("A=4 B=1");
10 for(int i = 1;i < 20;i++)
11 Console.Write(Get(4, 1, i).ToString() + "");
12 Console.WriteLine();
13 Console.WriteLine("A=6 B=2");
14 for(int i = 1;i < 20;i++)
15 Console.Write(Get(6, 2, i).ToString() + "");
16 Console.WriteLine();
17 }
18 static int Get(int A, int B, int N)
19 {
20 int a = A - B;
21 int b = A + B;
22 int t, w, q, p;
23 if(a > b)
24 {
25 t = a;
26 a = b;
27 b = t;
28 }
29 p = a;
30 q = b;
31 w = 1;
32 while(w < N)
33 {
34 if(p + a * 2 < q)
35 {
36 p += a * 2;
37 w++;
38 }
39 else if(p == q)
40 {
41 p += a * 2;
42 q += b * 2;
43 w++;
44 }
45 else
46 {
47 p += a * 2;
48 q += b * 2;
49 w += 2;
50 }
51 }
52 if(w == N)
53 return (p);
54 else
55 return (q - b * 2);
56 }
57 }
58 }
59 /*
60 A=4 B=1
61 3 5 9 15 15 21 25 27 33 35 39 45 45 51 55 57 63 65 69
62 A=6 B=2
63 4 8 12 20 24 28 36 40 44 52 56 60 68 72 76 84 88 92 100
64 */