C# 斐波拉契數列求第n個值
- 斐波拉契數列,求第n個值是多少
有這樣一個數列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,求出第n 位的值
int num1 = 1, num2 = 1, sum = 0;
Console.WriteLine("請輸入一個數:");
int int_Num = Convert.ToInt32(Console.ReadLine());
for (int i = 3; i <= int_Num; i++)
{
sum = num1 + num2;
num1 = num2;
num2 = sum;
}
Console.WriteLine(sum);
- 寫一個日期的窗體程式
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1990; i < 2050; i++)
{
this.comboBox1.Items.Add(i);
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
this.comboBox3.Items.Clear();
int year = Convert.ToInt32(this.comboBox1.Text);
int months = Convert.ToInt32(this.comboBox2.Text);
int day = 31;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
switch (months)
{
case 4:case 6:case 9:case 11:
day = 30;
break;
case 2:
day = 29;
break;
default:
break;
}
}
else
{
switch (months)
{
case 4:case 6:case 9:case 11:
day = 30;
break;
case 2:
day = 28;
break;
}
}
for (int i = 1; i <= day; i++)
{
this.comboBox3.Items.Add(i);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.comboBox2.Text = null;
this.comboBox3.Text = null;
this.comboBox2.Items.Clear();
this.comboBox3.Items.Clear();
for (int i = 1; i < 13; i++)
{
this.comboBox2.Items.Add(i);
}
}