1鸽粉、從一個(gè)數(shù)組中選擇最大最小值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請(qǐng)輸入5個(gè)數(shù)字:");
int[] arr = new int[5];
int max = int.MinValue;
int min = int.MaxValue;
for (int i = 0; i < 5; ++i)
{
arr[i] = Convert.ToInt16(Console.ReadLine());
if (max < arr[i])
max = arr[i];
if (min > arr[i])
min = arr[i];
}
Console.WriteLine("最大的數(shù)是:" + max);
Console.WriteLine("最小的數(shù)是:" + min);
Console.ReadKey();
}
}
}
2、九九乘法表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i < 10; ++i)
{
for (int j = 1; j <= i; ++j)
{
if (i * j > 9)
Console.Write("" + i + " * " + j + " = " + i * j + " ");
else
Console.Write("" + i + " * " + j + " = " + i * j + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
3抓艳、分別使用for循環(huán)和while循環(huán)設(shè)計(jì)一個(gè)程序触机,計(jì)算從1加到100的和
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
for (int i = 1; i <= 100; ++i)
{
sum += i;
}
Console.WriteLine("for:1+..+100 = " + sum);
sum = 0;
int j = 1;
while (j <= 100)
{
sum += j;
j++;
}
Console.WriteLine("while:1+..+100 = " + sum);
Console.ReadKey();
}
}
}
4、利用數(shù)學(xué)類提供的平方根方法計(jì)算并輸出1.0,2.0儡首,3.0片任,…,10.0的平方根蔬胯。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 10; ++i)
{
Console.WriteLine(i + "的平方根" + " = " + Math.Sqrt(i));
}
Console.ReadKey();
}
}
}
5对供、隨機(jī)數(shù)方法產(chǎn)生5個(gè)1~10(包括1和10)之間的整數(shù)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
Random ran = new Random();
for (int i = 1; i <= 5; ++i)
{
Console.WriteLine("第" + i + "個(gè)隨機(jī)數(shù)是 " + ran.Next(1, 11));
}
Console.ReadKey();
}
}
}
6氛濒、編程實(shí)現(xiàn):隨機(jī)產(chǎn)生1~20之間的整數(shù)产场,總共生成1000次,統(tǒng)計(jì)其中生成的整數(shù)0舞竿,1京景,2,3骗奖,... …确徙,20的個(gè)數(shù)分別是多少,并輸出統(tǒng)計(jì)結(jié)果(每5個(gè)數(shù)一行)重归。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
Random ran = new Random();
int[] arr = new int[1001];
for (int i = 1; i <= 1000; ++i)
{
arr[ran.Next(1, 21)]++;
}
for (int i = 1; i < 10; ++i)
{
Console.WriteLine(i + " 出現(xiàn)的次數(shù)是: " + arr[i]);
}
for (int i = 10; i <= 20; ++i)
{
Console.WriteLine(i + " 出現(xiàn)的次數(shù)是: " + arr[i]);
}
Console.ReadKey();
}
}
}
7米愿、統(tǒng)計(jì)從鍵盤中輸入字符串中分別有多少個(gè)數(shù)字和字母。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
class Program
{
static void Main(string[] args)
{
int letter = 0, digit = 0;
string s1 = Console.ReadLine();
foreach (char c in s1)
{
if (char.IsLetter(c))
{
letter++;
continue;
}
if (char.IsDigit(c))
{
digit++;
continue;
}
}
Console.WriteLine("字母出現(xiàn)的次數(shù)是: " + letter);
Console.WriteLine("數(shù)字出現(xiàn)的次數(shù)是: " + digit);
Console.ReadKey();
}
}
}
8鼻吮、編寫程序,要求用戶輸入月份號(hào)碼较鼓,然后顯示該月的英文名稱椎木。例如,如果用戶輸入2博烂,程序應(yīng)顯示February香椎。要求月的英文名稱存于數(shù)組中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
string[] s = new string[12];
s[0] = "January"; s[1] = "February"; s[2] = "March"; s[3] = "April";
s[4] = "May"; s[5] = "June"; s[6] = "July"; s[7] = "August";
s[8] = "September"; s[9] = "October"; s[10] = "November"; s[11] = "December";
int i = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(i + "月份" + "是 " + s[i - 1]);
Console.ReadKey();
}
}
}
9禽篱、請(qǐng)輸入一個(gè)代表項(xiàng)數(shù)的正整數(shù)N(N ≤ 100)畜伐,然后輸出1-3+5-7+9-11+……前N項(xiàng)的和。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp9
{
class Program
{
static void Main(string[] args)
{
int i = Convert.ToInt16(Console.ReadLine());
int sum = 0;
int num = 1;
for (int j = 1; j <= i; ++j, num += 2)
{
if (j % 2 == 0)
sum -= num;
else
sum += num;
}
Console.WriteLine("前" + i + "項(xiàng)和是 " + sum);
Console.ReadKey();
}
}
}
10躺率、請(qǐng)編寫兩個(gè)程序玛界,分別通過(guò)if語(yǔ)句和switch語(yǔ)句兩種方式完成,輸入一個(gè)成績(jī)悼吱,將百分制成績(jī)轉(zhuǎn)換成等級(jí)制成績(jī)(A為100~90 ,…, F為不及格)慎框。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp10
{
class Program
{
static void Main(string[] args)
{
int i = Convert.ToInt16(Console.ReadLine());
while (i < 0 || i > 100)
{
Console.WriteLine("數(shù)錯(cuò)了,再輸一遍");
i = Convert.ToInt16(Console.ReadLine());
}
char a = 'F';
if (i >= 90)
a = 'A';
else if (i >= 80)
a = 'B';
else if (i >= 70)
a = 'C';
else if (i >= 60)
a = 'D';
Console.WriteLine(a);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp10_2
{
class Program
{
static void Main(string[] args)
{
int i = Convert.ToInt16(Console.ReadLine());
while (i < 0 || i > 100)
{
Console.WriteLine("數(shù)錯(cuò)了,再輸一遍");
i = Convert.ToInt16(Console.ReadLine());
}
char a = 'F';
switch (i / 10)
{
case 10:
case 9: a = 'A'; break;
case 8: a = 'B'; break;
case 7: a = 'C'; break;
case 6: a = 'D'; break;
}
Console.WriteLine(a);
Console.ReadKey();
}
}
}
11、讀入兩個(gè)正整數(shù)m和n后添,輸出m和n的最小公倍數(shù)笨枯。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp11
{
class Program
{
static void Main(string[] args)
{
int n1 = Convert.ToInt16(Console.ReadLine());
while (n1 <= 0)
{
Console.WriteLine("數(shù)錯(cuò)了,再輸一遍");
n1 = Convert.ToInt16(Console.ReadLine());
}
int n2 = Convert.ToInt16(Console.ReadLine());
while (n2 < 0)
{
Console.WriteLine("數(shù)錯(cuò)了,再輸一遍");
n2 = Convert.ToInt16(Console.ReadLine());
}
int min = Math.Min(n1, n2);
for (int i = Math.Max(n1, n2); i < int.MaxValue; i++)
{
if (i % n1 == 0 && i % n2 == 0)
{
Console.WriteLine("最小公倍數(shù)是 " + i); Console.ReadKey();
return;
}
}
Console.WriteLine("沒(méi)找到最小公倍數(shù)");
Console.ReadKey();
}
}
}
12、判斷一個(gè)整數(shù)n是否為素?cái)?shù)。若是則輸出:n是素?cái)?shù)馅精;否則輸出n不是素?cái)?shù)(注:n是具體的值)严嗜。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp12
{
class Program
{
static void Main(string[] args)
{
int n = Convert.ToInt16(Console.ReadLine());
while (n < 0)
{
Console.WriteLine("請(qǐng)輸入正整數(shù),再輸一遍");
n = Convert.ToInt16(Console.ReadLine());
}
if (n == 0 || n == 1)
{
Console.WriteLine(n + "不是素?cái)?shù)"); Console.ReadKey();
return;
}
int MAX = (int)Math.Sqrt(n);
for (int i = 2; i <= MAX; ++i)
{
if (n % i == 0)
{
Console.WriteLine(n + "不是素?cái)?shù)"); Console.ReadKey();
return;
}
}
Console.WriteLine(n + "是素?cái)?shù)!");
Console.ReadKey();
}
}
}
13、用for循環(huán)計(jì)算s=1!+2!+3!+…+n!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp13
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請(qǐng)輸入一個(gè)正整數(shù):");
int n = Convert.ToInt16(Console.ReadLine());
while (n < 0)
{
Console.WriteLine("請(qǐng)輸入正整數(shù),再輸一遍");
n = Convert.ToInt16(Console.ReadLine());
}
int sum = 0;
int j = 1;
for (int i = 1; i <= n; ++i)
{
j = j * i;
sum += j;
}
Console.WriteLine("s=1!+2!+3!+…+n!的結(jié)果為:"+sum);
Console.ReadKey();
}
}
}
14洲敢、
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp14
{
class Program
{
static void Main(string[] args)
{
double d = 0;
for (int i = 2; i <= 20; ++i)
{
d += Math.Pow(i, i);
}
if (double.MaxValue - 0.01 < d)
Console.WriteLine("越界!!");
Console.WriteLine(d);
Console.ReadKey();
}
}
}
15漫玄、編寫一個(gè)程序,打印出以下圖形: