110,547
社区成员
发帖
与我相关
我的任务
分享
private void btCalc_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtA.Text);
int b = Convert.ToInt32(txtB.Text);
int n = Convert.ToInt32(txtn.Text);
int c1 = a - b;
int c2 = a + b;
int i;
int d1 = 1;
int d2 = 1;
int f1=c1;
int f2=c2;
int r=c1;
txtPaixu.Text = c1.ToString();
if (n > 1)
{
for (i = 2; i <= n; i++)
{
if (f1 == f2)
{
d1 += 2;
d2 += 2;
f1 = c1 * d1;
f2 = c2 * d2;
r = f1;
}
else if (f1 < f2)
{
d1 += 2;
r = f2;
f1 = c1 * d1;
}
else
{
d2 += 2;
r = f1;
f2 = c2 * d2;
}
txtPaixu.Text += "," + r.ToString();
}
}
txtAns.Text = r.ToString();
}
#include <iostream.h>
#define A 3
#define B 1
float an(int n)
{
return (2*n-1.0) / (A+B);
}
float bn(int n)
{
return (2*n-1.0) / (A-B);
}
float cn(int n)
{
int x = 0;
int y = 0;
while( (x+y) < n )
{
y++;
int tmp = (A+B)*y - B;
if( (tmp%(A-B)) == 0 )
{
x = tmp / (A-B);
}
else
{
x = tmp/(A-B) + 1;
}
x--;
}
if( (x+y) == n )
{
return bn(y);
}
else
{
y--;
return an(n-y);
}
}
void main()
{
cout<<"an:"<<endl;
for(int i=1; i<=20; i++)
{
cout<<an(i)<<" ";
}
cout<<endl;
cout<<"bn:"<<endl;
for(int i=1; i<=20; i++)
{
cout<<bn(i)<<" ";
}
cout<<endl;
cout<<"cn:"<<endl;
for(int i=1; i<=20; i++)
{
cout<<cn(i)<<" ";
}
cout<<endl;
}