赞
踩
1.判断n是奇是偶。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- if (n % 2 == 1) Console.WriteLine("{0}为奇数", n);
- else Console.WriteLine("{0}为偶数", n);
- Console.ReadKey();
- }
- }
- }

2.求“1-n”之间的偶数和/奇数和。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int sum0 = 0, sum1 = 0;
- for(int i=1;i<=n;i++)
- {
- if (i % 2 == 1) sum0 += i;
- else sum1 += i;
- }
- Console.WriteLine("奇数和{0},偶数和{1}", sum0, sum1);
- Console.ReadKey();
- }
- }
- }

3.判断字符中有几个大写,小写字母,数字,其他字符。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- string s=Console.ReadLine();
- int da = 0, xiao = 0, shuzi = 0, qita = 0;
- for(int i=0;i<s.Length;i++)
- {
- if (s[i] >= 'A' && s[i] <= 'Z') da++;
- else if (s[i] >= 'a' && s[i] <= 'z') xiao++;
- else if (s[i] >= '0' && s[i] <= '9') shuzi++;
- else qita++;
- }
- Console.WriteLine("大写字母{0}个,小写字母{1}个,数字{2}个,其他{3}个。", da, xiao, shuzi, qita);
- Console.ReadKey();
- }
- }
- }

4.90-100优秀,80-90良好,60-80及格,低于60不及格。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- if (n >= 90 && n <= 100) Console.WriteLine("优秀!");
- else if (n >= 80 && n < 90) Console.WriteLine("良好!");
- else if (n >= 60 && n < 80) Console.WriteLine("及格!");
- else Console.WriteLine("不及格!");
- Console.ReadKey();
- }
- }
- }

5.定义两个方法:一个用来求圆的面积,一个用来求矩形的面积。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- public static double solve(int x)
- {
- return Math.PI * x * x;
- }
- public static int fun(int x,int y)
- {
- return x * y;
- }
- public static void Main(string[] args)
- {
- int r = int.Parse(Console.ReadLine());
- double sum = solve(r);
- Console.WriteLine("圆的面积为{0}", sum);
- int a= int.Parse(Console.ReadLine());
- int b= int.Parse(Console.ReadLine()); ;
- int ans = fun(a, b);
- Console.WriteLine("矩形的面积为{0}", ans);
- Console.ReadKey();
- }
- }
- }

6.输入一个字符串,查看里面有多少个单词?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool flag = false;
- string s = Console.ReadLine();
- int sum = 0;
- for(int i=0;i<s.Length;i++)
- {
- if (s[i] != ' ')
- {
- if (flag == false)
- {
- sum++;
- flag = true;
- }
- else if (s[i - 1] == ' ') sum++;
- }
- }
- Console.WriteLine(sum);
- Console.ReadKey();
- }
- }
- }

7.输入若干个成绩,输出平均分。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int sum = 0;
- for(int i=1;i<=n;i++)
- {
- int x = int.Parse(Console.ReadLine());
- sum += x;
- }
- Console.WriteLine(sum/n);
- Console.ReadKey();
- }
- }
- }

8.求1-n之中的完数个数。(一个数如果恰好等于它的因子之和,这个数就称为“完数”)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int sum = 0;
- for(int i=2;i<=n;i++)
- {
- int flag = 0;
- for(int j=1;j<i;j++)
- {
- if (i % j == 0) flag += j;
- }
- if (flag == i) Console.WriteLine(i);//如果他想输出个数,这里改成sum++;就行了
- }
- //Console.WriteLine(sum);//这里就不用输出个数了
- Console.ReadKey();
- }
- }
- }

9.a能否整除b?(处理异常机制)。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- int a = int.Parse(Console.ReadLine ());
- int b = int.Parse(Console.ReadLine ());
- if (a % b == 0) Console.WriteLine("YES");
- else Console.WriteLine("NO");
- }
- catch
- {
- Console.WriteLine("输入异常!");
- }
- Console.ReadKey();
- }
- }
- }

10.给定一个数组,找到某个数第一次出现的下标。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] a = new int[] { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
- int n = int.Parse(Console.ReadLine());
- int idx = -1;
- for(int i=0;i<a.Length;i++)
- {
- if(a[i]==n)
- {
- idx = i;
- break;
- }
- }
- if (idx == -1) Console.WriteLine("没有出现过");
- else Console.WriteLine("下标为:{0}",idx);
- Console.ReadKey();
- }
- }
- }

11.判断一个数是否为质数?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- bool flag = false;
- for(int i=2;i<=n/i;i++)
- {
- if(n%i==0)
- {
- flag = true;
- break;
- }
- }
- if (flag == false&&n!=1) Console.WriteLine("是质数");
- else Console.WriteLine("不是质数");
- Console.ReadKey();
- }
- }
- }

12.求一个表达式的和:1!+2!+3!+4!+。。。n!=?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int sum = 0;
- int flag = 1;
- int ans = 1;
- while(flag<=n)
- {
- ans *= flag;
- sum += ans;
- flag++;
- }
- Console.WriteLine(sum);
- Console.ReadKey();
- }
- }
- }

13.(冒泡排序在课本163面 选择排序171)这个是排序的通用方法
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Vijurria
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] a = new int[] { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
- Array.Sort(a);
- for (int i = 0; i < a.Length; i++)
- Console.Write(a[i] + " ");
- Console.ReadKey();
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。