文档库 最新最全的文档下载
当前位置:文档库 › 算法竞赛入门经典第二章习题编程C#

算法竞赛入门经典第二章习题编程C#

string key = "y";
int i = 0;
string str;
while (key == "y")
{
do
{
Console.Write("请选择题号(1-10):");
str = Console.ReadLine();
}
while ((!int.TryParse(str, out i)) || (i <= 0) || (i > 11));
switch (i)
{
case 1: digit(); break;
case 2: daffidil(); break;
case 3: hanxin(); break;
case 4: tringle(); break;
case 5: stat(); break;
case 6: harmony(); break;
case 7: approximation(); break;
case 8: subsequence(); break;
case 9: Decimal();break;
case 10: permutation();break;
case 11: s(); break;
}
Console.Write("是否继续(y/n)?");
key = Console.ReadLine();
}
}
public static int inputInt(string caption)
{
string str;
int n;
do
{
Console.Write("请输入" + caption + "(正整数)");
str = Console.ReadLine();
}
while ((!int.TryParse(str, out n)) || (n <= 0));
return n;
}
public static void digit()
{
Console.WriteLine("习题2-1 位数(digit)");
int n = inputInt("一个不超过1000000000的正整数");
int count = 0;
while (n > 0)
{
n = n / 10;
count++;
}
Console.WriteLine("{0}", count);
}
public static void daffidil()
{
Console.WriteLine("习题2-2 水仙花数(daffidil)");
int a, b, c;
for (int i = 100; i <= 999; i++)
{
a = i % 10; b = i / 10 % 10;
c = i / 100;
if (i == a * a * a + b * b * b + c * c * c)
Console.WriteLine("{0}", i);
}
}
public static void hanxin()
{
Console.WriteLine("习题2-3 韩信点兵(hanxin)");
int a = inputInt("第一个小于3的数");
int b = inputInt("第二个小于5的数");
int c = inputInt("第三个小于7的数");
for (int i = 10; i <= 100; i++)
{
if (i % 3 == a && i % 5 == b && i % 7 == c)
{
Console.WriteLine("{0}", i);
}
}
}
public static void tringle()
{
Console.WriteLine("习题2-4 倒三角形(tringle)");
int n = inputInt("n<=20的正整数");
for (int i = n; i >= 1; i--)
{

for (int k = 0; k < n - i; k++)
{
Console.Write(" ");
}
for (int j = 0; j < 2 * i - 1; j++)
Console.Write("#");
if (n <= 20)
Console.WriteLine();
}
}
public static void stat()
{
Console.WriteLine("习题2-5 统计(stat)");
int n = inputInt("一个正整数");
int m = inputInt("另一个正整数");
int count =0;
for (int i = 1; i <= n; i++)
{
if (i < m)
count++;
}
Console.WriteLine("{0}", count);
}
public static void harmony()
{
Console.WriteLine("习题2-6 调和级数(harmony)");
int n = inputInt("正整数");
double H = 0;
for (int i = 1; i <= n; i++)
{
H = H + 1.0 / i;
}
Console.WriteLine("{0:f3}", H);
}
public static void approximation()
{
Console.WriteLine("习题2-7 近似计算(approximation)");
int flag = 0, i = 1;
double n = 1, sum = 1; while (n >= 0.000001)
{
n = (double)1 / (2 * i + 1); i += 1;
if (1 == flag)
{
sum += n; flag = 0;
}

else
{ sum -= n; flag = 1; }
}
sum *= 4;
Console.WriteLine(sum);
}
public static void subsequence()
{
Console.WriteLine("习题2-8 子序列的和(subsequence)");
int n = inputInt("第一个正整数");
int m = inputInt("第二个正整数");
int t;
double sum = 0;
if (n > m)
{
t = n;
n = m;
m = t;
}
for (int i = n; i <= m; i++)
{
sum = sum + (1.0 / i / i);
}
Console.WriteLine("{0:f5}", sum);
}
public static void Decimal()
{
Console.WriteLine("习题2-9 分数化小数(Decimal)");
int a = inputInt("第一个正整数");
int b = inputInt("第二个正整数");
int c = inputInt("第三个正整数");
Console.WriteLine("{0:f" + c.ToString() + "}", a * (1.0) / b);
}
public static void permutation()
{
Console.WriteLine("习题2-10 排列(permutation)");
for (int a = 1; a <= 9; a++)
{
for (int b = 1; b <= 9; b++)
for (int c = 1; c <= 9; c++)
for (int d = 1; d <= 9; d++)
for (int e =

1; e <= 9; e++)
for (int f = 1; f <= 9; f++)
for (int g = 1; g <= 9; g++)
for (int h = 1; h <= 9; h++)
for (int i = 1; i <= 9; i++)
if (2 * (a * 100 + b * 10 + c) == 1 * (d * 100 + e * 10 + f) && 3 * (a * 100 + b * 10 + c) == 1 * (g * 100 + h * 10 + i) && (a != b) && (a != c) && (a != d) && (a != e) && (a != f)
&& (a != g) && (a != h) && (a != i) && (b != c) && (b != d) && (b != e) && (b != f) && (b != g) && (b != h) && (b != i) && (c != d) && (c != e) && (c != f) && (c != g)
&& (c != h) && (c != i) && (d != e) && (d != f) && (d != g) && (d != h) && (d != i) && (e != f) && (e != g) && (e != h) && (e != i) && (f != g) && (f != h) && (f != i) &&
(g != h) && (g != i) && (h != i))
Console.WriteLine("{0},{1},{2}", a * 100 + b * 10 + c, d * 100 + e * 10 + f, g * 100 + h * 10 + i);
}
}
public static void s()
{

string[] myFile = System.IO.File.ReadAllLines("2.txt");
int num = myFile.Length;
int i = 0;
while (i < num)
{
Console.WriteLine("{0}__{1}", i, myFile[i]);
i++;
}

相关文档
相关文档 最新文档