1. 编写一个控制台应用程序,输出1到n的平方值,要求:
1) 用for语句实现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace problem1
{
classProgram
{
static void Main(string[] args)
{
Console.Write("请输入n : ");
string buf = Console.ReadLine();
int n = Convert.ToInt32(buf);
int i;
for (i = 1; i <= n; i++)
{
Console.Write(i * i + " ");
}
Console.WriteLine();
}
}
}
2) 用while语句实现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace problem12
{
classProgram
{
static void Main(string[] args)
{
Console.WriteLine("请输入n : ");
string buf = Console.ReadLine();
int n = Convert.ToInt32(buf);
int i = 1;
while(i <= n)
{
Console.Write(i * i);
if(i != n)
{
Console.Write(" ");
}
else
{
Console.WriteLine();
}
i++;
}
}
}
}
3) 用do-while语句实现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace problem13
{
classProgram
{
static void Main(string[] args)
{
Console.Write("请输入 n : ");
string buf = Console.ReadLine();
int n = Convert.ToInt32(buf);
int i = 1;
if(n < 1)
return;
do
{
Console.Write(i * i);
if (i != n)
Console.Write(" ");
else
Console.WriteLine();
i++;
}while (i <= n);
}
}
}
2. 编写一个控制台应用程序,要求用户输入5个大写字母,如果用户输入的信息不满足要求,提示帮助信息并要求重新输入。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
classProgram
{
static void Main(string[] args)
{
Console.Write("请新输入:");
while(true)
{
string str = Console.ReadLine();
int i;
bool k = true;
if (str.Length != 5)
k = false;
else
for (i = 0; i < 5; i++)
{
if (str[i] < 'A' ||str[i] > 'Z')
k = false;
}
if (!k)
Console.Write("请重新输入:");
else
{
Console.WriteLine("输入正确");
break;
}
}
}
}
}
3. 编写一个控制台应用程序,要求完成下列功能。
1) 接收一个整数n。
2) 如果接收的值n为正数,输出1到n间的全部整数。
3) 如果接收的值为负值,用break或者return退出程序。
4) 转到(1)继续接收下一个整数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace problem3
{
classProgram
{
static void Main(string[] args)
{
Console.Write("请输入n : ");
while (true)
{
string buf = Console.ReadLine();
int n = Convert.ToInt32(buf);
int i;
if (n < 1)
{
Console.Write("请重新输入n : ");
}
else
{
for (i = 1; i <= n; i++)
{
Console.Write(i);
if (i != n)
Console.Write(" ");
else
Console.WriteLine();
}
break;
}
}
}
}
}
4. 编写一个控制台应用程序,求1000之内的所有“完数”。所谓“完数”是指一个数恰好等于它的所有因子之和。例如,6是完数,因为6=1+2+3。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace problem4
{
classProgram
{
static void Main(string[] args)
{
//Console.WriteLine(Math.Pow(Math.Sqrt(2), 2) );
//Console.WriteLine(Math.Sqrt(2));
int i,j,z,sum;
int k = 0;
Console.Write("1 - 1000 的完数有: ");
for (i = 2; i <= 1000; i++ )
{
sum = 1;
for (j = 2; j <= Math.Sqrt(i); j++)
{
if (i % j == 0)
{
z = i / j;
sum += j;
sum += z;
}
}
if((int)Math.Sqrt(i) == Math.Sqrt(i))
sum -= (int)Math.Sqrt(i);
if (sum == i)
{
Console.Write(i);
k++;
if (0 == k % 10)
Console.WriteLine();
else
Console.Write(" ");
}
}
Console.Read();
}
}
}
5. 编写一个控制台应用程序,输入任意的10个整数,输出这10个数中的最大值和最小值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace problem
{
classProgram
{
static void Main(string[] args)
{
int min = 2147483647;
int max = -2147483648;
Console.Write("请输入10个整数: ");
string buf = Console.ReadLine();
string[] str = buf.Split(' ');
int i,k;
for (i = 0; i < 10; i++ )
{
k = Convert.ToInt32(str[i]);
if (k <= min)
min = k;
if (k >= max)
max = k;
}
Console.WriteLine("max : " + max);
Console.WriteLine("min : " + min);
}
}
}
6. 打印出以下的杨辉三角形(要求打印出任意的n行)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace problem6
{
classProgram
{
static void Main(string[] args)
{
Console.Write("请输入n: ");
string buf = Console.ReadLine();
int n = Convert.ToInt32(buf);
int i,j;
int[][] yh = new int[n][];
//生产杨辉三角形
for(i = 0; i < n; i++)
{
yh[i] = new int[i + 1];
yh[i][0] = 1;
yh[i][i] = 1;
for (j = 1; j < i; j++ )
{
yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j];
}
}
//输出杨辉三角形
for (i = 0; i < n; i++)
{
for (j = 0; j <= 1.0 * n / 2 - i + 1; j++)
Console.Write(" ");
for (j = 0; j <= i; j++ )
{
Console.Write(yh[i][j]);
if (j != i)
Console.Write("");
else
Console.WriteLine();
}
}
Console.ReadLine();
}
}
}
7.把一个数组中的值按逆序重新存放。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace problem7
{
class Program
{
static void Main(string[]args)
{
string buf;
string[] val;
int i;
//输入
Console.Write(“请输入n : “);
buf = Console.ReadLine();
int n = Convert.ToInt32(buf);
Console.Write(“请输入{0}个值(用空格分隔): “,n);
buf = Console.ReadLine();
val = buf.Split(‘ ‘);
//处理逆序
for (i = 0; i < n / 2; i++)
{
buf = val[i];
val[i] = val[n - 1 - i];
val[n - 1 - i] = buf;
}
//输出
Console.Write(“逆序为: “);
for (i = 0; i < n; i++)
Console.Write(val[i] + “ “);
Console.WriteLine();
Console.ReadLine();
}
}
}