using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 扩展练习题
{
class Program
{
static void Main(string[] args)
{
#region(第1题)
请编写一程序,输入x值,输出对应的y值
//int x,y;//定义变量
//Console.WriteLine("请输入你要输入的值:");
//x = Convert.ToInt32(Console.ReadLine());
判断条件是否满足
//if (x<1)
//{
// Console.WriteLine(y=x);
//}
//if (x >=1 && x <10)
//{
// y = 2*x - 1;
// Console.WriteLine(y);
//}
//if (x >= 10)
//{
// y = 3 * x - 11;
// Console.WriteLine(y);
//}
#endregion
#region(第2题)
//Console.WriteLine("输出2000-3000之间的闰年");
//控制输出年份的范围
//int rowcount = 0;//控制个数
//for (int i = 2000; i <=3000; i++)
//{
// //输出闰年的个数)
// if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
// {
// rowcount++;
// Console.Write(i + ",");//输出结果之间相互间隔
// if (rowcount == 5)
// {
// rowcount = 0;
// Console.Write("\n");//换行
// }
// }
//}
#endregion
#region(第3题)
/*3.求:1+1/2+1/3+1/4+.....+1/n ,n从键盘输入。*/
//Console.WriteLine("输入你要输入的数字:");
//int n = Convert.ToInt32(Console.ReadLine());
//double result = 0;
//for (double i = 1; i <= n; i++)
//{
// result += 1 / i; //result = result + i;
//}
//Console.WriteLine(result);
//Console.ReadKey();
#endregion
#region(第4题)
/* 4.求:1-1/2+1/3-1/4+.... ,直到最后一项的绝对值小于10-4为止。*/
//double F = 0, n = 0,Z = 0,sum=0;
//for (double i = 1; i <= 250; i++)
//{
// if (i % 2 == 0)
// {
// n = -i;
// Z = 1 / n + F;
// }
// else
// {
// Z = 1 / i + Z;
// }
// sum = Z + F;
//}
//Console.WriteLine(sum);
//Console.ReadKey();
#endregion
#region(第5题)
/*5.输入两个正整数m和n,求其最大公约数和最小公倍数。*/
//Console.WriteLine("输入第一个数字:");
//int a = Convert.ToInt32(Console.ReadLine());
//Console.WriteLine("输入第二个数字:");
//int b = Convert.ToInt32(Console.ReadLine());
//int c = a > b ? a : b;
//for (int i = b; i > 0; i--)
//{
// if (a % i == 0 && b % i == 0)
// {
// Console.WriteLine("最大公约数为{0}", i);
// break;
// }
//}
//for (int i = c; i <= a * b; i++)
//{
// if (i % a == 0 && i % b == 0)
// {
// Console.WriteLine("最小公倍数为{0}", i);
// break;
// }
//}
//Console.ReadLine();
#endregion
#region(第6题)
/*6.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。*/
//(首先定义变量,然后输入字符串,接着对字符串用循环条件分别处理,分为字母,数字,空格,
//最后条件是否满足输出结果)
//Console.WriteLine("请输入一串字符:");
//string x = Console.ReadLine();
//int a = 0;//用于统计英文字母个数
//int b = 0;//用于统计空格个数
//int c = 0;//用于统计数字个数
//int d = 0;//用于统计其它字符的个数
//do
//{
// char[] arry = x.ToCharArray();//定义数组储存字符串
// for (int i = 0; i < arry.Length; i++)
// {
// if (arry[i] >= 'a' & arry[i] < 'z' || arry[i] >= 'A' && arry[i] < 'Z')//判断字符串中的英文字母
// {
// a++;
// }
// else if (arry[i] >= '0' && arry[i] <= '9')
// {
// c++;
// }
// else if (arry[i] == ' ')
// {
// b++;
// }
// else
// {
// d++;
// }
// x = "";
// }
// Console.WriteLine("字母个数{0},空格个数{1},数字个数{2},其它字符{3}", a, b, c, d);
// Console.ReadKey();//输出停留
//} while (x != "");//必须得输入字符串
#endregion
#region(第7题)
/*7.求Sn=a+aa+aaa+……+aa…a(n个a)之值,其中a是一个数字。
例如:2+22+222+2222+22222(n=5),n由键盘输入。*/
//Console.WriteLine("输入你要计算的数字a");//输入你要输入的a值
//int a = Convert.ToInt32(Console.ReadLine());
//Console.WriteLine("输入你要计算的数字b");
//int n = Convert.ToInt32(Console.ReadLine());
//int result = 0; //进行求和
//string b = " ";
//for (int i = 1; i <= n; i++)
//{
// b = b + "1";
// a *= int.Parse(b);
// result += a;
//}
//Console.WriteLine(result);
//Console.ReadKey();
#endregion#
#region(第8题)
/* 8.求s=1!+2!+3!+……+n!,n由键盘输入。*/
//Console.WriteLine("输入你要计算的数字");
//int n = Convert.ToInt32(Console.ReadLine());
//int result = 0;
//for (int i = 1; i <= n; i++)//控制输出的数字
//{
// int jx = 1;//阶乘初始值
// for (int j = 1; j <= i; j++)
// {
// jx = jx * j;
// }
// result += jx;//阶乘求和
//}
//Console.WriteLine(result);//输出结果
#endregion
#region (第9题)
/*9.打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
例如:153是一个“水仙花数”,因为153=(1*1*1+3*3*3+5*5*5) */
//int i, j, k, n = 100; //(分别表示个位十位百位上的数字)
//do
//{
// i = n / 100;
// j = n % 100 / 10;
// k = n % 10;
// if (i * 100 + j * 10 + k == i * i * i + j * j * j + k * k * k)//判断彼此是否相等
// Console.WriteLine(n);//输出结果
// n++;
//} while (n < 1000);
//Console.ReadKey();
#endregion
#region(第10题)
/*10.一个数如果恰好等于它的因子之和,这个数就称为“完数”。
例如:6的因子为1、2、3,而6=1+2+3,因此6是“完数”。
编程找出1000以内的所有完数,并按下面格式输出其因子。如:6
是一个‘完数’,它的因子是1,2,3*/
//int i, j, sum = 0;
//for (i = 1; i < 1000; i++)//判断小于1000的数字
//{
// sum = 0;//初始的完数的个数
// for (j = 1; j <= i / 2; j++)
// {
// if (i % j == 0)
// {
// sum += j;
// }
// }
// if (sum == i)//等于完数本身
// {
// Console.WriteLine(i + " ");//输出完数的个数
// }
//}
//Console.ReadKey();
#endregion
#region(第11题)
/*11.猴子吃桃问题。猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。
第二天早上将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃前一天剩下的一半零一个。
到第10天早上再想吃时,见只剩一个桃子了。求第一天共摘多少桃子。*/
/*采取逆向思维的方法,从后往前推断*/
//int t, a, b;
//t = 9;
//b = 1;
//while (t > 0)
//{
// a = (b + 1) * 2;/*第一天的桃子数是第2天桃子数加1后的2倍*/
// b = a;
// t--;
//}
//Console.WriteLine("一共摘了{0}个桃子", b);
//使用for语句
//int sum = 1;
//for (int i = 1; i < 10; i++)
//{
// sum = (sum + 1) * 2;
//}
//Console.WriteLine("一共摘了{0}个桃子", sum);
//Console.ReadKey();
#endregion
#region(第12题)
/*12.一个小球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,
求它在第10次落地时,共经过了多少米?第10次反弹多高?*/
//double sum = 100, num = 100;
//for (int i = 2; i <= 10; i++)
//{
// num = (double)(num / 2);
// sum += 2 * num;
//}
//Console.WriteLine("第10次落地时,共经过{0}米,第10次反弹{1}", sum, num);
//Console.ReadKey();
#endregion
#region(第13题)
/*13.打印以下图案:*/
/*图案1*直角三角形*/
//Console.WriteLine("输入你要输入的行数N");
//int N = Convert.ToInt32(Console.ReadLine());
//int wind = Console.WindowWidth / 2;
//for (int i = 0; i < N; i++)//控制输出的行数
//{
// for (int j = 0; j < wind; j++)//控制空格,来输出位置
// {
// Console.Write(" ");
// }
// for (int h = 0; h < i; h++)//控制*
// {
// Console.Write("* ");
// }
// Console.WriteLine();
//}
/*图案2*等腰三角形*/
// Console.WriteLine("输入你要输入的行数N");
//int N = Convert.ToInt32(Console.ReadLine());
//int wind = Console.WindowWidth / 2;
//for (int i = 0; i < N; i++)//控制输出的行数
//{
// for (int j = i; j < wind; j++)//控制空格,来输出位置
// {
// Console.Write(" ");
// }
// for (int h = 0; h < i; h++)//控制*
// {
// Console.Write("*" + " ");
// }
// Console.WriteLine();
//}
//Console.ReadKey();
/*图案3反着输出三角形*/
//Console.WriteLine("输入你要输入的行数N");
//int N = Convert.ToInt32(Console.ReadLine());
//int wind = Console.WindowWidth / 2;
//for (int i = N; i >= 0; i--)
//{
// for (int j = i; j < wind; j++)
// {
// Console.Write(" ");
// }
// for (int j = 0; j < i + 1; j++)
// {
// Console.Write("* ");
// }
// Console.WriteLine();
//}
//Console.ReadKey();
/*图案4*在右边输出三角形*/
// Console.WriteLine("输入你要输入的行数N");
//int N = Convert.ToInt32(Console.ReadLine());
//int wind = Console.WindowWidth/2;
//for (int i = 1; i <=N; i++)
//{
// for (int j = i; j < wind; j++)
// {
// Console.Write("");
// }
// for (int j = 0; j < i; j++)
// {
// Console.Write("*" + " ");
// }
// Console.WriteLine();
//}
//Console.ReadKey();
/*图案5*上下输出三角形*/
// Console.WriteLine("输入你要输入的行数N");
//int N = Convert.ToInt32(Console.ReadLine());
//int wind = Console.WindowWidth / 2;
//for (int i = 0; i <= N; i++)
//{
// for (int j = i; j < wind; j++)
// {
// Console.Write(" ");
// }
// for (int j = 0; j < i + 1; j++)
// {
// Console.Write("* ");
// }
// Console.WriteLine();
//}
//int wind1 = Console.WindowWidth / 2;
//for (int i = N; i >= 0; i--)
//{
// for (int j = i; j < wind; j++)
// {
// Console.Write(" ");
// }
// for (int j = 0; j < i + 1; j++)
// {
// Console.Write("* ");
// }
// Console.WriteLine();
//}
//Console.ReadKey();
#endregion
#region(第14题)
/*14.有一分数序列:2/1+3/2+5/3+8/5+..... 求出这个数列的前20项之和。*/
//double a = 2;//分母
//double b = 1;//分子
//double Sn = 0;//初始结果
//double t;//中间变量
//for (int i = 0; i < 20; i++)
//{
// Sn = Sn + a / b;
// t = a; a = a + b; b = t;
//}
//Console.WriteLine("前20项和为:{0}",Sn);
//Console.ReadKey();
#endregion
#region(第15题)
/*15.输入一行字符,统计其中有多少个单词,单词之间用空格分隔开。*/
//Console.WriteLine("输入字符:");
//string N = Console.ReadLine();
//string[] str = N.Split(' ');
//Console.WriteLine("单词个数{0}", str.Length - 1);
//Console.ReadKey();
#endregion
#region(第16题)
/*16.打印出100之内的所有素数(普通法、筛选法)。*/
//int x = 0;
//for (int i = 1; i <= 100; i++)
//{ x = 0;
// for (int j = 1; j <= i; j++)
// {
// if (i % j == 0)
// {
// x++;
// }
// }
// if (x == 2)
// {
// Console.WriteLine("1到100的素数有:{0}", i);
// }
//}
//Console.ReadKey();
#endregion
#region(第17题)
/*17.求Fibonacci数列的前20项(用普通方法及数组方法)。*/
//普通法
//int x = 0, sum = 1;
//for (int j = 1; j <= 20; j++ )
//{
// sum = x + sum;
// x = sum - x;
// Console.WriteLine( sum);
//}
//数组法 前20项
//int[] a = new int[20];
//for (int i =0; i < 20; i++)
//{
// if (i==1||i==0)
// {
// a[i] = 1;
// }
// for (int j = 2; j < 20; j++)
// {
// a[j] = a[j - 2] + a[j - 1];
// }
// Console.WriteLine(a[i]);
//}
//Console.ReadKey();
#endregion
#region(第18题)
/*18.对10个整数排序(选择法、冒泡法)。*/
//int[] a = new int[10];
//int min = 0;
//for (int i = 0; i<a.Length-1; i++)
//{
// int n=int.Parse(Console.ReadLine());
// //a[i] = n;
// for (int j =0; j <a.Length-i-1; j++)
// {
// if (a[j+1]>a[j])
// {
// min = a[j];
// a[j] = a[j+1];
// a[j+1]=min;
// }
// }
//}
// foreach (var item in a)
// {
// Console.WriteLine(string.Format("{0} ", item));
// }
//一般方法
//Console.Write("这是一个排序,请输入要排序的数:");
//string str = Console.ReadLine();
//string[] array = str.Split(',');
int[] arrayInt = new int[array.Length];
//List<int> arrayInt = new List<int>();
//for (int i = 0; i < array.Length; i++)
//{
// arrayInt.Add(Convert.ToInt32(array[i]));
//}
//arrayInt.Sort();
//for (int i = 0; i < array.Length; i++)
//{
// Console.WriteLine(arrayInt[i]);
//}
Console.ReadKey();
#endregion
#region(第19题)
/*19.有一个已排好序的数组,今输入一个数,要求按原来排序的规律将它插入数组中。*/
//Console.WriteLine("这是一个排序,请输入一段已排序的数:");
//int[] a = new int[10];
//for (int i = 0; i <a.Length; i++)
//{
// a[i] = int.Parse(Console.ReadLine());
//}
//Array.Sort(a);
//int[] result = new int[11];
//Array.Copy(a, result, 10);
//Console.WriteLine("请输入一个数");
//result[10] = int.Parse(Console.ReadLine());
//int min = 0;
//for (int i = 0; i < result.Length - 1; i++)
//{
// //a[i] = n;
// for (int j = 0; j < result.Length - i - 1; j++)
// {
// if (result[j + 1]< result[j])
// {
// min = result[j];
// result[j] = result[j + 1];
// result[j + 1] = min;
// }
// }
//}
//foreach (var item in result)
//{
// Console.WriteLine("重新排序后{0}",item);
//}
#endregion
#region(第20题)
/*将一个数组中的值按逆序重新存放*/
//Console.Write("这是一个排序,一个字符串数组:");
//string str = Console.ReadLine();
//string[] array = str.Split(',');
int[] arrayInt = new int[array.Length];
//List<string > arrayInt = new List<string >();
//for (int i = 0; i < array.Length; i++)
//{
// arrayInt.Add(array[i]);
//}
arrayInt.Sort();
//arrayInt.Reverse();
//for (int i = 0; i < array.Length; i++)
//{
// Console.WriteLine(arrayInt[i]);
//}
#endregion
#region(第21题)
///*21.将一个二维数组行和列元素互换,存到另一个二维数组中。*/
int ["行号","列号"]
用一个中间的一维数组来做中间值
//Console.WriteLine("请输入二维数组的行号,列号");
//int x = int.Parse(Console.ReadLine());
//int y = int.Parse(Console.ReadLine());
//int[,] Frist = new int[x, y];
定义一个一维数组来保存原来二维数组的值
//int[] One = new int[Frist.Length];
循环输入二维数组中的值
//for (int i = 0; i <= x - 1; i++)
//{
// for (int j = 0; j <= y - 1; j++)
// {
// Frist[i, j] = Convert.ToInt32(Console.ReadLine());
// }
//}
循环输出二维数组中的值
//for (int i = 0; i < x; i++)
//{
// for (int j = 0; j < y; j++)
// {
// //将二维数组的值赋值为一维数组
// Console.WriteLine("当前行为{0},当前列为{1},当前项的值{2}", i, j, Frist[i, j]);
// }
//} //将一维数组的值再赋值给一个新的二维数组
//for (int s = 0; s < One.Length; s++)
//{
// One[s] = Frist[s / y, s % y];
//}
//int[,] Second = new int[y, x];
//for (int i = 0; i <= y - 1; i++)
//{
// for (int j = 0; j <= x - 1; j++)
// {
// Second[i, j] = Frist[j, i];
// }
//}
循环输出新的二维数组的值
//for (int i = 0; i < y; i++)
//{
// for (int j = 0; j < x; j++)
// {
// //将二维数组的值赋值为一维数组
// Console.WriteLine("新的当前行为{0},新的当前列为{1},新的当前项的值{2}", i, j, Second[i, j]);
// }
//}
#endregion
#region(第22题)
//int[,] intlist = new int[3, 3];
//Random ran = new Random();
//Console.WriteLine("3*3矩阵内容");
//for (int i = 0; i <= 2; i++)
//{
// for (int j = 0; j <= 2; j++)
// {
// intlist[i, j] = ran.Next(0, 1000);
// Console.Write(intlist[i, j] + " ");
// }
// Console.WriteLine();
//}
//int result = 0;
//int Oblique = 0;//代表\
//int Positive = 0;//代表/
代表\的对角线
//for (int a = 0; a <= 2; a++)
//{
// Oblique += intlist[a, a];
//}
代表/的对角线
//for (int b = 0; b <= 2; b++)
//{
// Positive += intlist[b, 2 - b];
//}
//result = Oblique + Positive;
//Console.WriteLine("两条对角线之和为{0}", result);
//Console.ReadKey();
#endregion
#region(第23题)
/*23.有一个3*4的矩阵,求出其中最大值,以及它所在的行和列。*/
//int[,] intlist = new int[3, 4];
//Random ran = new Random();
//Console.WriteLine("3*4矩阵内容");
//for (int i = 0; i <= 2; i++)
//{
// for (int j = 0; j <= 3; j++)
// {
// intlist[i, j] = ran.Next(0, 1000);
// Console.Write(intlist[i, j] + " ");
// }
// Console.WriteLine();
//}
#endregion
#region(第24题)
/* 24.打印魔方阵,所谓魔方阵是指这样的方阵,它的每一行、第一列和对角线之和均相等。*/
//魔方的中心点是不变的,将一个点的位置赋值给1,然后将二维数组a[i,j]=k(k是循环的变量,循环次数就是3*3)
//8 1 6
//3 5 7
//4 9 2
//Console.WriteLine("请输入一个奇数");
//int n = int.Parse(Console.ReadLine());
//if (n != 0 && n / 2 != 0)
//{
// int[,] mf = MF(n);
// for (int i = 0; i < n; i++)
// {
// for (int j = 0; j < n; j++)
// {
// Console.Write(mf[i, j] + " \t");//\t相当于tab键
// if ((j + 1) % n == 0)
// {
// Console.Write("\n");
// }
// }
// }
//}
#endregion
#region(第25题)
//打印出杨辉三角
//Console.WriteLine("输入你要计算的行数:");
//int N = Convert.ToInt32(Console.ReadLine());
//int[][] arry = new int[N][];
//int i, j;
//for (i = 0; i < N; i++)
//{
// arry[i] = new int[i + 1];//给第i行分配储存空间。
//}
//for (i = 0; i < N; i++)
//{//每行首列和尾列的值均为1;
// arry[i][0] = 1;
// arry[i][i] = 1;
//}
//for (i = 2; i < N; i++)
//{//i=2;意思是i从0开始,当i=2时,相当于是从第3行开始变化。
// for (j = 1; j < i; j++) //j<i,表示不含最后一项。
// {//j=1,意思是从第2项开始,其值等于上一行的前一列和上一行当前列的和。
// arry[i][j] = arry[i - 1][j - 1] + arry[i - 1][j];
// }
//}
//for (i = 0; i < N; i++)
//{//输出各行各列的值。
// Console.WriteLine();//换行符。
// for (j = 0; j <= i; j++)
// {
// Console.Write("{0} ", arry[i][j]);//输出各项值。
// }
//}
//Console.ReadLine();
#endregion
#region(第26题)
/*26.有一篇文章,共有3行文字,每行有80个字符,要求分别统计出其中英文大写字母 、小写字母、数字、空格及其它字符的个数。*/
//Console.WriteLine("请输入三行字符,80个字符");
//string x = Console.ReadLine();
//string [] stringlist=new string[3];//三行
//int a = 0;//用于统计英文字母个数
//int b = 0;//用于统计空格个数
//int c = 0;//用于统计数字个数
//int d = 0;//用于统计其它字符的个数
//int y = 0;//记录行数
//for (y = 0; y < 3; y++)
// for (int j = 0; j <= 80; j++)
// {
// char[] charlist = x.ToCharArray();
// for (int i = 0; i < charlist.Length; i++)
// {
// if (charlist[i] >= 'a' & charlist[i] < 'z' || charlist[i] >= 'A' && charlist[i] < 'Z')
// {
// a++;
// }
// else if (charlist[i] >= '0' && charlist[i] <= '9')
// {
// c++;
// }
// else if (charlist[i] == ' ')
// {
// b++;
// }
// else
// {
// d++;
// }
// x = "";
// //ady++;
// }
// } Console.WriteLine("字母个数{0},空格个数{1},数字个数{2},其它字符{3}", a, b, c, d);
#endregion
#region(第27题)
/*27.编一程序,实现下列字符串库函数功能:*/
//(1)将两个字符串连接起来。
//Console.WriteLine("请输入一个字符串:");
//string str1 = Console.ReadLine();
//Console.WriteLine("请在输入一个字符串:");
//string str2 = Console.ReadLine();
//string str = str1 + str2;
//Console.WriteLine("连接之后字符串:{0}",str);
//Console.ReadKey();
//(2)比较两个字符串。
//Console.WriteLine("请输入一个字符串:");
//string str1 = Console.ReadLine().ToString();
//Console.WriteLine("请在输入一个字符串:");
//string str2 = Console.ReadLine().ToString();
//do
//{
// Console.WriteLine("输出字符串{0}",str1);
// break;
//} while (str1.Length >= str2.Length);
//(3)求一个字符串的长度。
//Console.WriteLine("请输入一个字符串:");
//string str = Console.ReadLine();
// int a = str.Length;
//Console.WriteLine("字符串的的长度是:{0}",a);
//Console.ReadKey();
//(4)将一个字符串复制到另一个字符串中。
//Console.WriteLine("请输入一个字符串:");
//string str1 = Console.ReadLine();
//Console.WriteLine("请在输入一个字符串:");
//string str2 = Console.ReadLine();
//string str = string.Copy(str1+str2);//新的字符串是将str1和str2复制之后相加
//Console.WriteLine("输出新的字符串:{0}",str);//输出新的字符串
//Console.ReadKey();
#endregion
}
//private static int[,] MF(int n)
//{
// //定义一个二维数组
// int[,] arrlist = new int[n, n];
// //将第一行的第二个数字先进行赋值给1
// int i = 0;
// int j = n / 2;
// arrlist[i, j] = 1;//1在一开始就被赋值给了二维数组中的其中一个位置,之后k的循环从2开始
// for (int k = 2; k <= n * n; k++)
// {
// if (i >= 0 && i < n)
// {
// i--;//-1 2 1 3 0
// if (i < 0)
// {
// i = i + n;
// }
// }
// if (j >= 0 && j < n)
// {//1 2,0 1
// j++;
// if (j >= n)
// {
// j = j - n;
// }
// }
// if (arrlist[i, j] != 0)
// {
// i += 2;
// if (i >= n)
// {
// i = i - n;
// }
// j--;
// if (j < 0)
// {
// j = j + n;
// }
// arrlist[i, j] = k;
// }
// else
// {
// arrlist[i, j] = k;
// }
// }
// return arrlist;
//}
}
}
C#扩展练习题
最新推荐文章于 2021-05-18 12:19:13 发布