从学习编程至今,已有六个月左右,总结了下以前学习的知识,并把以前老师发我的五十道题以前刚开始学的时候,没做起的重新做了.这也算是一个及时巩固下(虽然只是五十道编程题,但感觉还是挺有意义的,所以留念下….)
1.古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一
对兔子,假如兔子都不死,问每个月的兔子总数为多少?
int i = 1, j = 1,acount=0;
Console.WriteLine("请输入兔子开始生兔子的第几月");
int n = Convert.ToInt32(Console.ReadLine());
for (int k=0;k< n;k++)
{
acount = i + j;
i=j;
j = acount;;
}
Console.WriteLine(acount);
2.判断101-200之间有多少个素数,并输出所有素数。
int acount=0;
for(int i = 101; i <= 200;i++)
{
for(int j = 2; j <= Math.Sqrt(i); j++)
{
if (i%j==0)
{
++acount;
break;
}
}
}
Console.WriteLine(acount);
3.打印出所有的”水仙花数”,所谓”水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
for(int i = 1; i <= 9; i++)
{
for(int j = 0; j <=9; j++)
{
for (int k=0;k<=9;k++)
{
if(Math.Pow(i,3)+ Math.Pow(j, 3)+ Math.Pow(k, 3)== i * 100 + j * 10 + k)
{
Console.Write(i);
Console.Write(j);
Console.Write(k);
Console.WriteLine();
}
}
}
}
4.将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
int N = Convert.ToInt32(Console.ReadLine());
int y;
for(int k = 2; k <= N; k++)
{
if (N % k == 0)
{
y = N / k;
while (k + 1 <N)
{
if (y % k == 0)
{
y = y /k;
Console.WriteLine(k);
}
k++;
}
break;
}
}
5.海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一
个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中
,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
static void Main ( string[] args )
{
float lastNum = 1;
int count = 0;
while (true)
{
for (int i = 0; i < 4; i++)
{
float temp = lastNum;
lastNum = (5 * lastNum + 1) / 4;
if (!isInt ( lastNum ))
{
i = 0;
lastNum = temp;
lastNum++;
count = 0;
break;
}
else
{
count++;
}
}
if (count == 4)
break;
}
Console.WriteLine ( 5 * lastNum + 1 );
Console.ReadKey ();
}
static bool isInt (float f)
{
int i = (int)f;
if (f.ToString ().Length == i.ToString ().Length)
{
return true;
}
return false;
}
6.一个偶数总能表示为两个素数之和。
static void Main ( string[] args )
{
int a;
while (true)
{
Console.WriteLine ( "请输入一个偶数:" );
a = int.Parse ( Console.ReadLine () );
if (a % 2 != 0)
{
Console.WriteLine ( "输入的不是一个偶数,请重新输入:" );
Console.ReadKey ();
}
else
break;
}
for(int i = 1; i < a; i++)
{
if (a - i <i)
break;
if(isPrime(i)&&isPrime(a-i)&&i<=a-i)
{
Console.WriteLine ( a + "=" + i + "+" + (a - i) );
}
}
Console.ReadKey ();
}
static bool isPrime (int p)
{
for(int i = 2; i < p; i++)
{
if (p % i == 0)
return false;
}
return true;
}
7.有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件”student”中。
struct student
{
public int ID;
public string Name;
public Dictionary<string, int> Score;
}
static void Main ( string[] args )
{
student[] students = new student[5];
float[] avgs = new float[students.Length];
FileStream file = new FileStream ( @"D:\Student.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
file.Close ();
for(int i = 0; i < students.Length; i++)
{
float f = 0;
Console.WriteLine ( "请输入学号:" );
students[i].ID = Convert.ToInt32(Console.ReadLine ());
Console.WriteLine ( "请输入姓名:" );
students[i].Name = Console.ReadLine ();
students[i].Score = new Dictionary<string, int> ();
for (int j = 0; j < 3; j++)
{
Console.WriteLine ( "请输入学科:" );
string name = Console.ReadLine ();
Console.WriteLine ( "请输入对应学科的成绩:" );
int score = Convert.ToInt16(Console.ReadLine());
students[i].Score.Add ( name, score );
}
foreach(var v in students[i].Score)
{
f += v.Value;
}
avgs[i] = f / 3;
using(StreamWriter sw=new StreamWriter( @"D:\Student.txt",true ))
{
sw.WriteLine("学号:"+students[i].ID+"\b姓名:"+students[i].Name+"\b"+students[i].Score.ElementAt(0)+"\b"+students[i].Score.ElementAt ( 1 ) + "\b"+students[i].Score.ElementAt ( 2) + "\b平均成绩为:"+avgs[i]);
sw.Close ();
}
Console.Clear ();
}
}
8.打印出杨辉三角形(要求打印出10行如下图)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
int row = 1;
int[] originArry = new int[row];
int[] nextArry = new int[++row];
originArry[0] = 1;
nextArry[0] = 1;nextArry[1] = 1;
foreach(int i in originArry)
{
Console.WriteLine( i );
}
foreach(int i in nextArry)
{
Console.Write ( i );
}
Console.WriteLine ();
for(int i = 2; i < 10; i++)
{
originArry = new int[nextArry.Length];
originArry = nextArry;
nextArry = new int[++row];
for (int j = 0; j < nextArry.Length-1; j++)
{
if (j == 0 )
{
Console.Write ( 1 );
nextArry[0] = 1;
}
if (j != 0 &&j != nextArry.Length - 1)
{
nextArry[j] = originArry[j]+ originArry[j - 1];
Console.Write ( nextArry[j] );
}
if (j == nextArry.Length - 2)
{
Console.Write ( 1 );
nextArry[nextArry.Length - 1] = 1;
}
}
Console.WriteLine ();
}
Console.ReadKey ();
9.有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
int n = int.Parse ( Console.ReadLine () );
List<int> queue = new List<int> ();
for(int i = 0; i < n; i++)
{
queue.Add(i+1);
}
for(int i = 0; i < queue.Count; i++)
{
int count = 0;
if ((i + 1) % 3 == 0)
{
queue[i] = 0;
}
else
{
queue.Add ( queue[i] );
queue[i] = 0;
}
foreach(int num in queue)
{
if (num == 0)
count++;
}
if (count == queue.Count - 1)
break;
}
Console.WriteLine ( queue[queue.Count-1] );
Console.ReadKey ();
等等…
源文件链接: 链接:http://pan.baidu.com/s/1nvrBtgt 密码:4yms