当前位置:   article > 正文

C#期末复习编程题(老师猜的)

C#期末复习编程题(老师猜的)

1.判断n是奇是偶。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int n = int.Parse(Console.ReadLine());
  13. if (n % 2 == 1) Console.WriteLine("{0}为奇数", n);
  14. else Console.WriteLine("{0}为偶数", n);
  15. Console.ReadKey();
  16. }
  17. }
  18. }

2.求“1-n”之间的偶数和/奇数和。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int n = int.Parse(Console.ReadLine());
  13. int sum0 = 0, sum1 = 0;
  14. for(int i=1;i<=n;i++)
  15. {
  16. if (i % 2 == 1) sum0 += i;
  17. else sum1 += i;
  18. }
  19. Console.WriteLine("奇数和{0},偶数和{1}", sum0, sum1);
  20. Console.ReadKey();
  21. }
  22. }
  23. }

3.判断字符中有几个大写,小写字母,数字,其他字符。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string s=Console.ReadLine();
  13. int da = 0, xiao = 0, shuzi = 0, qita = 0;
  14. for(int i=0;i<s.Length;i++)
  15. {
  16. if (s[i] >= 'A' && s[i] <= 'Z') da++;
  17. else if (s[i] >= 'a' && s[i] <= 'z') xiao++;
  18. else if (s[i] >= '0' && s[i] <= '9') shuzi++;
  19. else qita++;
  20. }
  21. Console.WriteLine("大写字母{0}个,小写字母{1}个,数字{2}个,其他{3}个。", da, xiao, shuzi, qita);
  22. Console.ReadKey();
  23. }
  24. }
  25. }

4.90-100优秀,80-90良好,60-80及格,低于60不及格。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int n = int.Parse(Console.ReadLine());
  13. if (n >= 90 && n <= 100) Console.WriteLine("优秀!");
  14. else if (n >= 80 && n < 90) Console.WriteLine("良好!");
  15. else if (n >= 60 && n < 80) Console.WriteLine("及格!");
  16. else Console.WriteLine("不及格!");
  17. Console.ReadKey();
  18. }
  19. }
  20. }

5.定义两个方法:一个用来求圆的面积,一个用来求矩形的面积。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. public static double solve(int x)
  11. {
  12. return Math.PI * x * x;
  13. }
  14. public static int fun(int x,int y)
  15. {
  16. return x * y;
  17. }
  18. public static void Main(string[] args)
  19. {
  20. int r = int.Parse(Console.ReadLine());
  21. double sum = solve(r);
  22. Console.WriteLine("圆的面积为{0}", sum);
  23. int a= int.Parse(Console.ReadLine());
  24. int b= int.Parse(Console.ReadLine()); ;
  25. int ans = fun(a, b);
  26. Console.WriteLine("矩形的面积为{0}", ans);
  27. Console.ReadKey();
  28. }
  29. }
  30. }

6.输入一个字符串,查看里面有多少个单词?

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. bool flag = false;
  13. string s = Console.ReadLine();
  14. int sum = 0;
  15. for(int i=0;i<s.Length;i++)
  16. {
  17. if (s[i] != ' ')
  18. {
  19. if (flag == false)
  20. {
  21. sum++;
  22. flag = true;
  23. }
  24. else if (s[i - 1] == ' ') sum++;
  25. }
  26. }
  27. Console.WriteLine(sum);
  28. Console.ReadKey();
  29. }
  30. }
  31. }

7.输入若干个成绩,输出平均分。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int n = int.Parse(Console.ReadLine());
  13. int sum = 0;
  14. for(int i=1;i<=n;i++)
  15. {
  16. int x = int.Parse(Console.ReadLine());
  17. sum += x;
  18. }
  19. Console.WriteLine(sum/n);
  20. Console.ReadKey();
  21. }
  22. }
  23. }

8.求1-n之中的完数个数。(一个数如果恰好等于它的因子之和,这个数就称为“完数”)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int n = int.Parse(Console.ReadLine());
  13. int sum = 0;
  14. for(int i=2;i<=n;i++)
  15. {
  16. int flag = 0;
  17. for(int j=1;j<i;j++)
  18. {
  19. if (i % j == 0) flag += j;
  20. }
  21. if (flag == i) Console.WriteLine(i);//如果他想输出个数,这里改成sum++;就行了
  22. }
  23. //Console.WriteLine(sum);//这里就不用输出个数了
  24. Console.ReadKey();
  25. }
  26. }
  27. }

 9.a能否整除b?(处理异常机制)。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. try
  13. {
  14. int a = int.Parse(Console.ReadLine ());
  15. int b = int.Parse(Console.ReadLine ());
  16. if (a % b == 0) Console.WriteLine("YES");
  17. else Console.WriteLine("NO");
  18. }
  19. catch
  20. {
  21. Console.WriteLine("输入异常!");
  22. }
  23. Console.ReadKey();
  24. }
  25. }
  26. }

10.给定一个数组,找到某个数第一次出现的下标。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int[] a = new int[] { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
  13. int n = int.Parse(Console.ReadLine());
  14. int idx = -1;
  15. for(int i=0;i<a.Length;i++)
  16. {
  17. if(a[i]==n)
  18. {
  19. idx = i;
  20. break;
  21. }
  22. }
  23. if (idx == -1) Console.WriteLine("没有出现过");
  24. else Console.WriteLine("下标为:{0}",idx);
  25. Console.ReadKey();
  26. }
  27. }
  28. }

11.判断一个数是否为质数?

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int n = int.Parse(Console.ReadLine());
  13. bool flag = false;
  14. for(int i=2;i<=n/i;i++)
  15. {
  16. if(n%i==0)
  17. {
  18. flag = true;
  19. break;
  20. }
  21. }
  22. if (flag == false&&n!=1) Console.WriteLine("是质数");
  23. else Console.WriteLine("不是质数");
  24. Console.ReadKey();
  25. }
  26. }
  27. }

12.求一个表达式的和:1!+2!+3!+4!+。。。n!=?

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int n = int.Parse(Console.ReadLine());
  13. int sum = 0;
  14. int flag = 1;
  15. int ans = 1;
  16. while(flag<=n)
  17. {
  18. ans *= flag;
  19. sum += ans;
  20. flag++;
  21. }
  22. Console.WriteLine(sum);
  23. Console.ReadKey();
  24. }
  25. }
  26. }

13.(冒泡排序在课本163面   选择排序171)这个是排序的通用方法

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vijurria
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int[] a = new int[] { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
  13. Array.Sort(a);
  14. for (int i = 0; i < a.Length; i++)
  15. Console.Write(a[i] + " ");
  16. Console.ReadKey();
  17. }
  18. }
  19. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/40257
推荐阅读
相关标签
  

闽ICP备14008679号