c# 经典521例(3 )

一球从100米高度自由下落,每次落地后反弹回原高度的一半,再次落下。求它在第十次落地,经过多少米?第十次反跳多高?

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
float sn = 100, hn = sn / 2;
int n;
for (n = 2; n <= 10; n++)
{
sn
= sn + 2 * hn;//第n次落地时共经过的米数
hn = hn / 2; //第n次反跳高度
}
Console.WriteLine(
"the total of road is{0}", sn);
Console.WriteLine(
"the tenth is {0} meter", hn);
Console.ReadKey();
}
}
}

将数组a中{ 3, 7, 9, 11, 0, 6, 7, 5, 4, 2 }整数按相反顺序存放

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[] a = { 3, 7, 9, 11, 0, 6, 7, 5, 4, 2 };
for (int i = 0; i < 10; i++)
{ Console.WriteLine(a[i]); }
Program newpro
= new Program();
newpro.inv(
ref a);
Console.ReadKey();
}
public void inv(ref int[] x)
{
for (int i = 0; i < 5; i++)
{
int temp;
temp
= x[i];
x[i]
= x[9 - i];
x[
9 - i] = temp;
}
Console.WriteLine(
"反序后:");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(x[i]);
}

}
}
}

将一个二维数组行和列的元素互换,存到另一个二维数组中

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] b = new int[3, 2];
int i, j;
Console.WriteLine(
"array a:");
for (i = 0; i <= 1; i++)
{
for (j = 0; j <= 2; j++)
{
Console.WriteLine(a[i, j]);
b[j, i]
= a[i, j];
}
}
Console.WriteLine(
"array b:\n");
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 1; j++)
{ Console.WriteLine(b[i,j]); }
}
Console.ReadLine();

}
}
}

一个3*4的矩阵,要求编程序求出其中之最大的那个元素的值

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int max=0;
int[,] a = { { 1, 2, 3, 4 }, { 9, 8, 7, 6 }, { -10, 10, -5, 2 } };
foreach (int x in a)
{
if (x> max)
{
max
= x;
}
}
Console.WriteLine(
"数组中最大的是{0}",max);
Console.ReadLine();

}
}
}

有5个人坐在一起,问第5个人多少岁?他说比第4个人大2岁。问第4个人多少岁?他说比第3个人大2岁。问第3个人多少岁?他说比第2个人大2岁。问第2个人多少岁?他说比第1个人大2岁。最后问第1个人,他说是10岁。请问的第5个人多大。用递归方法解决该问题。

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Program newpro
= new Program();
Console.WriteLine(
"第5个人{0}岁", newpro.Age(5));
Console.ReadLine();

}

public int Age(int n)
{
if (n <= 1)
return 10;
else
return Age(n - 1)+2;
}
}
}

用递归方法求n!

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Program newpro
= new Program();
Console.Write(
"输入阶数:");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(
"{0}的阶乘为:{1}",n, newpro.fac(n));
Console.ReadLine();
}
public int fac(int n)
{
if (n <=0)
return 1;
else
return n*fac(n - 1);
}
}
}

判断一年是否是闰年?

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.Write(
"输入年份:");
int year = Convert.ToInt32(Console.ReadLine());

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
Console.WriteLine(
"输入的是闰年!");
else
Console.WriteLine(
"输入的不是闰年!");
Console.ReadLine();
}
}
}


猴子吃桃问题。猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘多少桃子。 

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
for (int i = 1; i <= 10; i++)
{ sum
= 2 * sum + 1; }

Console.WriteLine(
"第一天共摘{0}个桃子!",sum);
Console.ReadLine();
}
}
}

1、1、2、3、5、8、13、21、34...... 求第n位数是多少?用递归算法实现

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(
"想求数列的第几项?");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(
"数列的第{0}项是{1}!", n, ShuLie(n));
Console.ReadLine();
}
public static int ShuLie(int i)
{
if (i <= 2)
return 1;
else
return ShuLie(i - 1) + ShuLie(i - 2);

}

}
}

posted @ 2011-06-19 21:53  伟伟LOVE齐齐  阅读(235)  评论(0编辑  收藏  举报