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();
}
}
}