C# 实验三 判断一个字符、判断三角形、千名学生、a+aa+aaa+aaaa、求数列相加、约瑟夫环
生活随笔
收集整理的這篇文章主要介紹了
C# 实验三 判断一个字符、判断三角形、千名学生、a+aa+aaa+aaaa、求数列相加、约瑟夫环
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
以下為實現代碼
1 判斷一個字符
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;//用戶輸入一個字符,判斷是數字、大寫字母、小寫字母還是其他字符 namespace 第三章作業 {class Program{static void Main(string[] args){string str;label1:Console.WriteLine("請輸入一個字符:");str=Console.ReadLine();char key=(char)0;//判斷格式正確if (str.Length > 1){Console.WriteLine("你輸入的太多了");goto label1;}else if (str.Length == 0){Console.WriteLine("你并沒有輸入");goto label1;}else key = str[0];//判斷asciiif(key>='0'&&key<='9'){Console.WriteLine("數字");}else if (key >= 'a' && key <= 'z'){Console.WriteLine("小寫字母");}else if (key >= 'A' && key <= 'Z'){Console.WriteLine("大寫字母");}else {Console.WriteLine("其他");}goto label1;}} }2 判斷三角形
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace 三角形 {class Program{static void Main(string[] args){double a, b, c;//輸入Console.WriteLine("請輸入一條邊長:");a = double.Parse(Console.ReadLine());Console.WriteLine("請輸入一條邊長:");b = double.Parse(Console.ReadLine());Console.WriteLine("請輸入一條邊長:");c = double.Parse(Console.ReadLine());//判斷if (a <= 0 || b <= 0 || c <= 0){Console.WriteLine("邊長不能為0或負數");}if (a + b > c && a + c > b && b + c > a)//兩邊之和大于第三遍{if (a == b || b == c || a == c){Console.WriteLine("等腰三角形");if (a * a + b * b - c * c <= 0.001 || b * b + c * c - a * a <= 0.001 || a * a + c * c - b * b <= 0.001){Console.WriteLine("直角三角形");}}else if (a * a + b * b == c * c || a * a == b * b + c * c || a * a + c * c == b * b){Console.WriteLine("直角三角形");}else Console.WriteLine("普通三角形");}else Console.WriteLine("不是三角形");}} }3 千名學生
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace _3_千名學生 {class Program{static void Main(string[] args){int total;Console.WriteLine("100~2000之間滿足要求的數有:");for (total = 100; total < 2000; total++){if (total % 5 == 2 && total % 7 == 3 && total % 3 == 1){Console.WriteLine(total);}}}} }4 a+aa+aaa+aaaa
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace _4_a_aa_aaa_aaaa {class Program{static void Main(string[] args){int total = 0;Console.WriteLine("a+aa+aaa+aaaa計算");//輸入重復次數int n;Console.WriteLine("請輸入n");n = int.Parse(Console.ReadLine());//輸入數字int a;Console.WriteLine("請輸入a");a = int.Parse(Console.ReadLine());//相加int i;int j;int curNum = 0;Console.WriteLine("計算結果:");for (i = 0; i < n; i++)//第i個數字{curNum = a;for (j = 0; j < i; j++){curNum *= 10;curNum += a;}total += curNum;Console.Write(curNum);if (i != n - 1) Console.Write(" + ");else if (i == n - 1) Console.Write(" = ");}Console.WriteLine(total);}} }5 求數列相加
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;//測試:n=6 //答案:0.383333333333333333333333 namespace _5_求數列相加 {class Program{static void Main(string[] args){//輸入nint n;Console.WriteLine("請輸入n");n = int.Parse(Console.ReadLine());//n+1項相加int i;double up;double down;int flag = 1;double total = 0;for (i = 0; i <= n; i++){up = (i == 0 ? 1 : i); //分母down = i + 1; //分子total += (up / down) * flag; //總數flag *= -1;}Console.WriteLine("total=" + total);}} }6 約瑟夫環
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace _6_10同學報數 {class Program{static void Main(string[] args){//輸入總數int total;Console.WriteLine("請輸入總人數:");total = int.Parse(Console.ReadLine());bool[] a = new bool[total];//輸入從第幾個開始數int begin;Console.WriteLine("從第幾個人開始:");begin = int.Parse(Console.ReadLine());//數到幾int count;Console.WriteLine("從1數到幾:");count = int.Parse(Console.ReadLine());//全部為truefor (int i = 0; i < total; i++){a[i] = true;}//開始刪除int iter = begin - 1;int countnum = 1;int left = total;while (left != 1) //如果剩一個人,停止{if (a[iter] == false){iter++;iter %= total;continue;}if (countnum == 0) //如果當前人報3,刪除這個人{a[iter] = false;left--;Console.Write(iter + 1 + " ");}iter++; //下一個人iter %= total;countnum++; //下一個人報的數countnum %= count;}//剩下的數字for (int i = 0; i < total; i++){if (a[i] == true){Console.Write("剩下的人是:" + (i + 1) + "\n");break;}}}} }總結
以上是生活随笔為你收集整理的C# 实验三 判断一个字符、判断三角形、千名学生、a+aa+aaa+aaaa、求数列相加、约瑟夫环的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 约瑟夫环 用数组实现
- 下一篇: Logisim 组合电路设计 七段数码管