帮忙写个算法:两个递增数列排序后求第n个数
已知数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个数是多少?
[解决办法]
你是要代码还是要算法?
[解决办法]
decimal A = 50;
decimal B = 15;
arryA = new ArrayList();
ArrayList arryB = new ArrayList();
bool flag = false;
for (int i = 1; i <= 100; i = i + 2)
{
arryA.Add(i / (A + B));
arryB.Add(i / (A - B));
}
for (int i = 0; i < arryB.Count; i++)
{
for (int j = i; j < arryA.Count; j++)
{
if (!flag)
{
if (Convert.ToDecimal(arryB[i]) - Convert.ToDecimal(arryA[j]) > 0)
{
arryA.Insert(j, arryB[i]);
flag = true;
}
}
}
}
for (int i = 0; i < arryA.Count; i++)
{
Console.WriteLine(arryA[i]);
}
[解决办法]
decimal A = 4; decimal B = 1; int IndexA = 1; int IndexB = 1; ArrayList arry = new ArrayList(); int FindIndex = 3; int n = 0; while (n < FindIndex) { if (IndexA * (A + B) < IndexB * (A - B)) { arry.Add(IndexA * (A + B)); IndexA = IndexA + 2; } else { arry.Add(IndexB * (A - B)); IndexB = IndexB + 2; } n++; } Console.Write(decimal.Parse(arry[FindIndex - 1].ToString()) / ((A - B) * (A + B))); Console.ReadLine();
[解决办法]
double A = Convert.ToDouble(this.textBox1.Text); //输入A数的值 double B = Convert.ToDouble(this.textBox2.Text); //输入B数的值 int N = Convert.ToInt32(this.textBox3.Text); //要求的N个数 List<double> list=new List<double>(); double tempAdd = 0; double tempSub = 0; for (int i = 1, j = 0; j < N; i = i + 2, j++) { tempAdd = i / (A + B); tempSub = i / (A - B); list.Add(tempAdd); list.Add(tempSub); } list.Sort(); //从低道高排序 this.textBox4.Text = list[N].ToString(); //输出结果
[解决办法]
using System;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { Console.WriteLine("A=4 B=1"); for(int i = 1;i < 20;i++) Console.Write(Get(4, 1, i).ToString() + " "); Console.WriteLine(); Console.WriteLine("A=6 B=2"); for(int i = 1;i < 20;i++) Console.Write(Get(6, 2, i).ToString() + " "); Console.WriteLine(); } static int Get(int A, int B, int N) { int a = A - B; int b = A + B; int t, w, q, p; if(a > b) { t = a; a = b; b = t; } p = a; q = b; w = 1; while(w < N) { if(p + a * 2 < q) { p += a * 2; w++; } else if(p == q) { p += a * 2; q += b * 2; w++; } else { p += a * 2; q += b * 2; w += 2; } } if(w == N) return (p); else return (q - b * 2); } }}/*A=4 B=13 5 9 15 15 21 25 27 33 35 39 45 45 51 55 57 63 65 69A=6 B=24 8 12 20 24 28 36 40 44 52 56 60 68 72 76 84 88 92 100*/
[解决办法]
线形滴
so ,实际是就一次插两个数
[解决办法]
decimal A = 40; decimal B = 10; int IndexA = 1; int IndexB = 1; ArrayList arry = new ArrayList(); int FindIndex = 1; //这里是要找到的N int n = 0; int SameCount = 0; while (n < FindIndex) { //两组数都扩大了(A+B)(A-B) //相同就有先排A的 if (IndexA * (A + B) < IndexB * (A - B)) { arry.Add(IndexA * (A + B)); IndexA = IndexA + 2; } else if (IndexB * (A - B) < IndexA * (A + B)) { arry.Add(IndexB * (A - B)); IndexB = IndexB + 2; } else { arry.Add(IndexA * (A + B)); IndexA = IndexA + 2; IndexB = IndexB + 2; } n++; } //输出时两组数都除(A+B)(A-B) Console.Write(decimal.Parse(arry[FindIndex - 1].ToString()) / ((A - B) * (A + B))); Console.ReadLine();