标签:
1.求质数 质数也成为素数,质数就是这个数除了1和他本身两个因数以外,没有其他因数的数,叫做质数,和他相反的是合数, 就是除了1和他本身两个因数以外,还友其他因数的数叫做合数。
1 namespace ConsoleApp
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7 long i;
8 while (true)
9 {
10 Console.Write("请输入要计算的质数(0退出):");
11 i = long.Parse(Console.ReadLine());
12 if (i == 0) break;
13 DateTime t1 = DateTime.Now;
14 switch (i)
15 {
16 case 1: Console.WriteLine("1 不是质数!"); break;
17 case 2: Console.WriteLine("2 是质数!"); break;
18 default: cal(i); break;
19 }
20 DateTime t2 = DateTime.Now;
21 Console.WriteLine("时间为:{0} 毫秒\n", (t2 - t1).Ticks / 10000f);
22 }
23 }
24
25 //以下为函数部分
26 static void cal(long x)
27 {
28 long sum = 1;
29 byte row = 1;
30 Console.Write("\n");
31 for (long a = 3; a < x + 1; a++)
32 {
33 bool flag = true;
34 for (long b = 2; b < (a / 2) + 1; b++)
35 {
36 if (a % b != 0) continue;
37 flag = false;
38 break;
39 }
40 if (flag)
41 {
42 if (row == 10) { Console.WriteLine(); row = 0; }
43 if (sum == 1) Console.Write("{0,7}", 2);
44 Console.Write("{0,7}", a);
45 sum++; row++;
46 }
47 }
48 Console.WriteLine("\n\n{0} 以内共有 {1} 个质数\n", x, sum);
49 }
50 }
51 }
2.有一列数1,1,2,3,5,........求第30个数.
1 public static int Foo(int i)
2 {
3 if (i <= 0)
4 return 0;
5 else if (i > 0 && i <= 2)
6 return 1;
7 else
8 return Foo(i - 1) + Foo(i - 2);
9 }
10
3.冒泡排序
4.请编写一个函数,能够计算10以内数的阶乘,尽量采用递归算法。(10!=3628800)。
5 请编程实现此方法。将输入的整型数组,合并转换为逗号分隔的字符串。
例如输入参数为整型数组{9,7,2},那么输出结果为字符串"9,7,2"。
1 private static string Combine(int[] data)
2 {
3 string str = "";
4 foreach (int s in data)
5 {
6 str += s.ToString() + ",";
7 }
8 return str;
9 }
6.产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
1 //产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
2 int[] arr = new int[100];
3 ArrayList myList = new ArrayList();
4 Random rad = new Random();
5 while (myList.Count < 100)
6 {
7 int num = rad.Next(1, 101);
8 if (!myList.Contains(num))
9 {
10 myList.Add(num);
11 }
12 }
13 for (int i = 0; i < 100; i++)
14 {
15 arr[i] = (int)myList[i];
16 }
17 for (int i = 0; i < arr.Length; i++)
18 {
19 Console.Write(arr[i] + ",");
20 }
21 Console.ReadKey();
7.请将字符串"I am a student"按单词逆序输出 如"student a am I"
string S = "I am a student";
char[] C = new char[] { ‘ ‘};
string[] n =S.Split(C);
int length = S.Length;
for (int i =length-1 ; i >=0; i--)
{
Console.Write(n[i]);
if (i != 0)
{
Console.Write(" ");
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceTest4_03
{
classProgram
{
staticvoidMain(string[] args)
{
string[] names = {"Adams","Arthur","Buchanan","Tsbuchis","ShCian","FuchsiaLinda","DecheChen","Lotheer","FindLanciCade", "SorchLand","JiangZheng","MisiiLoda","Gtod","Dfac","Lama","BakCades","Losangle","ZheWQ","GehengDahaLothi","ToryLandey", "DakaLothy","BthLanda","MenNorth","Fith","FoxMain","DontM","Saobba","Del","Sala","Ghero","BhthLaPhda"};
IEnumerable<string> skip = names.Skip(10);
IEnumerable<string> take = names.Take(11);
//取出两个序列中交集部分,按理论应该输出JiangZheng
IEnumerable<string> intersect = skip.Intersect(take);
foreach(varsinintersect)
{
Console.WriteLine(s);
}
Console.ReadKey();
}
}
}
标签:
原文地址:http://www.cnblogs.com/xtxblog/p/4760641.html