1,编写一个程序,对输入的4个整数,求出其中的最大值和最小值,并显示出来。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题1_解2
{
class Program
{
static void Main(string[] args)
{
while (true)
{
int num = 0;
//int min1 = 0;
Console.WriteLine("请输入两个整数");
string str1 = Console.ReadLine();
int num1 = Convert.ToInt32(str1);
string str2 = Console.ReadLine();
int num2 = Convert.ToInt32(str2);
Console.WriteLine("再输入0-3之间的数");
string str3 = Console.ReadLine();
int num3 = Convert.ToInt32(str1);
if (num3 == 1)
{
num = num1 + num2;
}
if (num3 == 2)
{
num = num1 - num2;
}
if (num3 == 3)
{
num = num1 * num2;
}
Console.WriteLine(num);
}
Console.ReadKey();
}
}
}
2,让用户输入两个整数,然后再输入0-3之间的一个数,0代表加法,1代表减法,2代表乘法,3代表除法,计算这两个数字的结果
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题1_输出最值
{
class Program
{
static void Main(string[] args)
{
while (true)
{
int max1 = 0;
int min1 = 0;
Console.WriteLine("请输入第1个数");
string str1 = Console.ReadLine();
int num1 = Convert.ToInt32(str1);
Console.WriteLine("请输入第2个数");
string str2 = Console.ReadLine();
int num2 = Convert.ToInt32(str2);
if (num1 > num2)
{
max1 = num1;
min1 = num2;
}
else
{
min1 = num1;
max1 = num2;
}
int max2 = 0;
int min2 = 0;
Console.WriteLine("请输入第3个数");
string str3 = Console.ReadLine();
int num3 = Convert.ToInt32(str3);
Console.WriteLine("请输入第4个数");
string str4 = Console.ReadLine();
int num4 = Convert.ToInt32(str4);
if (num3 > num4)
{
max2 = num3;
min2 = num4;
}
else
{
min2 = num3;
max2 = num4;
}
int max = 0;
int min = 0;
if (max1 > max2)
{
max = max1;
min = max2;
}
else
{
min = max1;
max = max2;
}
Console.WriteLine("最大值:{0}\n最小值:{1}",max, min);
//if (num1 > num2)
//{
// int max1 = num1;
// int min1 = num2;
//}
//if (num1 > num2)
//{
// int max1 = num1;
// int min1 = num2;
//}
}
Console.ReadKey();
}
}
}
3,求出1~1000之间的所有能被7整除的数,并计算和输出每5个的和。
解法1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题3_输出1000___
{
class Program
{
static void Main(string[] args)
{
//求出1~1000之间的所有能被7整除的数,并计算和输出每5个的和。
int num = 1;
int add = 0;
int index = 0;
while (num <= 999)
{
num++;
if (num % 7 == 0)
{
index++;
Console.WriteLine("能够被7整除的数:{0}", num);
add += num;
}
if (index == 5)
{
Console.WriteLine("能够被7整除的数5个数之和:{0}", add);
index = 0;
add = 0;
}
}
Console.ReadKey();
}
}
}
解法2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题3_输出1000___
{
class Program
{
static void Main(string[] args)
{
//求出1~1000之间的所有能被7整除的数,并计算和输出每5个的和。
int add = 0;
int index = 0;
for (int num = 1; num <= 999; num++)
{
if (num % 7 == 0)
{
index++;
Console.WriteLine("能够被7整除的数:{0}", num);
add += num;
}
if (index == 5)
{
Console.WriteLine("能够被7整除的数5个数之和:{0}", add);
index = 0;
add = 0;
}
}
Console.ReadKey();
}
}
}
**4,编写一个控制台程序,分别输出1~100之间的平方、平方根。 **
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题4
{
class Program
{
static void Main(string[] args)
{
//4,编写一个控制台程序,分别输出1~100之间的平方、平方根。
for (int num = 0; num <= 100; num++)
{
int add = num * num;
double gen = Math.Sqrt(num);
Console.WriteLine("平方为:{0} 平方根为:{1}",add, gen);
}
Console.ReadKey();
}
}
}
5,兔子繁殖问题。设有一对新生的兔子,从第三个月开始他们每个月都生一对兔子,新生的兔子从第三个月开始又每个月生一对兔子。按此规律,并假定兔子没有死亡,20个月后共有多少个兔子?要求编写为控制台程序。
using System;
namespace ConsoleApplication
{
class Fibonacci
{
static void Main(string[] args)
{
//打印出斐波那契数列的前50项
double[] feiBos = new double[50];
feiBos[0] = 1;
feiBos[1] = 1;
//feiBos[2] = 2;
for (int i = 2; i < 20; i++)
{
feiBos[i] = feiBos[i - 1] + feiBos[i - 2];
}
// String.Join(分隔符, 需要打印的数组)
Console.WriteLine("feiBo = [" + String.Join(",", feiBos) + "]");
Console.WriteLine("feiBo数组的长度是{0}", feiBos.Length);
Console.ReadKey();
}
}
}
输出:6765只兔子。
6,编程输出1~100中能被3整除但不能被5整除的数,并统计有多少个这样的数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题6
{
class Program
{
//编程输出1 ~100中能被3整除但不能被5整除的数,并统计有多少个这样的数。
static void Main(string[] args)
{
int index = 0;
for (int num = 0; num <= 100;num++) {
if (num % 3 == 0 && num % 5 != 0) {
index++;
Console.WriteLine("能被3整除的数字,不能被5整除的数:{0}", num);
Console.WriteLine("能被3整除的,不能被5整除的数字个数:{0}", index);
}
}
Console.ReadKey();
}
}
}
7,编程输出1000以内的所有素数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题7素数
{
class Program
{
static void Main(string[] args)
{
for (int num=2;num<=1001;num++) {
bool isZhiShu = true;
for (int j = 2; j < num-1; j++)
{
if (num % j == 0)//
{
isZhiShu = false;
break;
}
}
if (isZhiShu)
{
Console.WriteLine(num);
}
}
Console.ReadKey();
}
}
}
8,编程输出九九乘法表。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题10
{
class Program
{
static void Main(string[] args)
{
for (int num1=1; num1 <= 10; num1++)
{
Console.WriteLine("-----------");
for (int num2 = 1; num2 <= 10; num2++)
{
int num = num1 * num2;
Console.WriteLine("{0}*{1}={2}", num1, num2,num);
}
}
Console.ReadKey();
}
}
}
9,编写一个掷筛子100次的程序,并打印出各种点数的出现次数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题11
{
class Program
{
static void Main(string[] args)
{
int num0 = 0, num1 = 0, num2 = 0, num3 = 0,num4 = 0, num5 = 0;
Random random = new Random();
for (int num=0;num<100;num++)
{
int num_random = random.Next(1, 7);
switch (num_random)
{
case 1:
num0++;
break;
case 2:
num1++;
break;
case 3:
num2++;
break;
case 4:
num3++;
break;
case 5:
num4++;
break;
case 6:
num5++;
break;
}
}
Console.WriteLine(num0);
Console.WriteLine(num1);
Console.WriteLine(num2);
Console.WriteLine(num3);
Console.WriteLine(num4);
Console.WriteLine(num5);
Console.ReadKey();
}
}
}
10,一个控制台应用程序,输出1~5的平方值,要求:用for语句实现。用while语句实现。用do-while语句实现。
10.1 for语句实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题12
{
class Program
{
static void Main(string[] args)
{
for (int num = 1; num <= 5; num++)
{
int square = num * num;
Console.WriteLine("{0}^2={1}", num, square);
}
Console.ReadKey();
}
}
}
10.2 用while语句实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题12
{
class Program
{
static void Main(string[] args)
{
int num = 0;
while (num <= 4)
{
num++;
int square = num * num;
Console.WriteLine("{0}^2={1}", num, square);
}
Console.ReadKey();
}
}
}
}
10.3 用do-while语句实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题12
{
class Program
{
static void Main(string[] args)
{
int num = 0;
do
{
num++;
int square = num * num;
Console.WriteLine("{0}^2={1}", num, square);
} while (num <= 4);
Console.ReadKey();
}
}
}
11,一个控制台应用程序,要求用户输入5个大写字母,如果用户输入的信息不满足要求,提示帮助信息并要求重新输入。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题12
{
class Program
{
static void Main(string[] args)
{
//,一个控制台应用程序,要求用户输入5个大写字母,如果用户输入的信息不满足要求,
//提示帮助信息并要求重新输入。
login:
Console.WriteLine("请输入五个大写字母数字:");
string str=Console.ReadLine();
int len = str.Length;
bool isDaXieZiMu = true;
//Console.WriteLine(len);
for (int i=0;i<= len-1; i++)
{
//Console.WriteLine(i);
if (len == 5 &&str[i]>='A' && str[i] <= 'Z') {
Console.WriteLine("当前输入正确");
}
else
{
isDaXieZiMu = false;
break;
}
}
if (isDaXieZiMu == false)
{
Console.WriteLine("当前输入字母不符合要求");
goto login;
}
Console.ReadKey();
}
}
}
12,一个控制台应用程序,要求完成写列功能。
1)接收一个整数n。
2)如果接收的值n为正数,输出1~n间的全部整数。
3)如果接收的值n为负值,用break或者return退出程序。
4)如何n为0的话 转到1继续接收下一个整数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题13
{
class Program
{
static void Main(string[] args)
{
// 12,一个控制台应用程序,要求完成写列功能。
//1)接收一个整数n。
//2)如果接收的值n为正数,输出1~n间的全部整数。
//3)如果接收的值n为负值,用break或者return退出程序。
//4)如何n为0的话 转到1继续接收下一个整数。
Console.WriteLine("请输入一个整数:");
string str = Console.ReadLine();
int n = Convert.ToInt32(str);
for (int num = 1; num <= n;num++)
{
if (n == 0)
{
continue;
}
else if(n >0)
{
Console.WriteLine(num);
}
else
{
break;
}
Console.ReadKey();
}
}
}
}
13,一个控制台应用程序,求1000之内的所有“完数”。所谓“完数”是指一个数恰好等于它的所有因子之和。例如6是完数,因为6=1+2+3。
解法1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 习题14
{
class Program
{
static void Main(string[] args)
{
//一个控制台应用程序,求1000之内的所有“完数”。
//所谓“完数”是指一个数恰好等于它的所有因子之和。
//例如6是完数,因为6 = 1 + 2 + 3。
string output = "";
int i, j = 1, s;
for (i = 2; i <= 1000; i++)
{
s = 0;
for (j = 1; j<i; j++)
{
if (i % j == 0)
{
s = s + j;
}
if (i == s)
{
output = output + " " + i;
break;
}
}
}
Console.WriteLine(output);
Console.ReadKey();
}
}
}
小结:完数就是所有因子的和等同于该数,所以求出所有因子(所有整除的数)的和即可。