帮忙写个算法:两个递增数列排序后求第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 = 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; …
------解决方案--------------------
不知道是不是这个意思,止戈老大看看:
C# code
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();
------解决方案--------------------
C# code
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(); //输出结果