A. 孪生素数查找程序

所谓孪生素数指的是间隔为2 的相邻素数,就像孪生兄弟。最小的孪生素数是(3, 5),在100 以内的孪生素数还有(3,5), (5,7), (11,13),(17,19),(29,31),(41,43),(59,61),(71,73) 总计有 8 组。(备注:每组孪生素数之间用英文逗号,分隔)

输入正整数,输出小于等于number的孪生素数的组数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;

namespace CSharp_Week3_A
{
public static class NumberChecker
{
public static bool CheckPrime(int x)
{
if (x < 2)
return false;
for (int i = 2; i * i <= x; ++i)
if (x % i == 0)
return false;
return true;
}
public static int NextNumber(int x) { return x + 2; }
public static bool TwinPrime(int x)
{
return CheckPrime(x) && CheckPrime(NextNumber(x));
}
public static void WriteTwin(int x, ref bool split)
{
if (split)
Console.Write(",");
else
split = true;
Console.Write("({0},{1})", x, NextNumber(x));
}
}
class Program
{
static void Main(string[] args)
{
bool split = false;
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 1; NumberChecker.NextNumber(i) <= n; ++i)
if (NumberChecker.TwinPrime(i))
NumberChecker.WriteTwin(i, ref split);
}
}
}

B. 求a+aa+aaa+aaaa+...+aa...a

求a+aa+aaa+aaaa+...+aa...a(第n项,n个a),其中a是1~9的整数。例如,

a=1,n=3时,式子为1+11+111;

当a=6,n=5时,式子为5+55+555+5555+55555。

格式: 第一行为输入a n 第二行为输出结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;

namespace CSharp_Week3_B
{
public class Calculate
{
int a, n;
long[] save;
long ans = 0;
void update()
{
if (n < 1)
return;
save = new long[n];
save[0] = a;
for (int i = 1; i < n; ++i)
save[i] = save[i - 1] * 10 + a;
foreach (long sig in save)
ans += sig;
}
public Calculate(int va, int vn)
{
a = va;
n = vn;
update();
}
public void SetDigit(int va) { a = va; update(); }
public void SetLength(int vn) { n = vn; update(); }
public long At(int x) { return x >= 0 && x < n ? save[x] : 0; }
public long Get(int len) { return len > 0 && len <= n ? save[len - 1] : 0; }
public long GetSum() { return ans; }
public void WriteSum() { Console.WriteLine(ans); }
}
public static class InputConvert
{
public static int[] GetInputArray()
{
string[] instr = Console.ReadLine().Split(' ');
int[] invar = new int[instr.Length];
for (int i = 0; i<invar.Length; ++i)
invar[i] = Convert.ToInt32(instr[i]);
return invar;
}
}
class Program
{
static void Main(string[] args)
{
int[] an = InputConvert.GetInputArray();
Calculate equ = new Calculate(an[0], an[1]);
equ.WriteSum();
}
}
}

C. 天数计算

输入公元年份和月份,输出该月份的天数。(用switch……case语句)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;

namespace CSharp_Week3_C
{
public static class InputConvert
{
public static int[] GetInputArray(char SplitOperator)
{
string[] instr = Console.ReadLine().Split(SplitOperator);
int[] invar = new int[instr.Length];
for (int i = 0; i < invar.Length; ++i)
invar[i] = Convert.ToInt32(instr[i]);
return invar;
}
}
public static class DateCalculate
{
public static int GetDays(int year, int month)
{
switch(month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
case 2:
if (year % 400 == 0)
return 29;
if (year % 100 == 0)
return 28;
if (year % 4 == 0)
return 29;
return 28;
default:
return 0;
}
}
public static void WriteDays(int year, int month)
{
Console.WriteLine(GetDays(year, month));
}
}
class Program
{
static void Main(string[] args)
{
int[] ym = InputConvert.GetInputArray(',');
DateCalculate.WriteDays(ym[0], ym[1]);
}
}
}

D. 文字祖玛游戏

程序通过用户输入一个字符串(长度不超过30),由A、B、C、D、E五个字母组成,例如:ACBEEBBAD。

用户再输入一个字符,只能是A、B、C、D、E其中之一,然后再输入一个要插入的位置。

程序会将这个字符插入到字符串的指定位置前(第一个字符位置为0,第二个字符位置为1,依此类推),然后消除连续出现的三个相同的字符,直到没有连续三个相同的字符为止。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;

namespace CSharp_Week3_D
{
public class ZumaMap
{
string map;
static int rule = 3;
public ZumaMap() { map = Console.ReadLine(); }
public ZumaMap(string s) { map = s; }
public string GetMap() { return map; }
public void WriteMap() { Console.WriteLine(map); }
public bool AutoRemove()
{
for (int i = 0; i + rule <= map.Length; ++i)
{
bool check = true;
for (int j = 1; j < rule; ++j)
if (map[i + j] != map[i])
{
check = false;
break;
}
if (check)
{
map = map.Remove(i, rule);
return true;
}
}
return false;
}
public void Update() { while (AutoRemove()) ; }
public void Insert(int insloc, char insvar)
{
map = map.Insert(insloc, Convert.ToString(insvar));
Update();
}
}
public static class InputConvert
{
public static string[] GetInputStrings(char SplitOperator)
{
return Console.ReadLine().Split(SplitOperator);
}
public static int[] GetInputArray(char SplitOperator)
{
string[] instr = Console.ReadLine().Split(SplitOperator);
int[] invar = new int[instr.Length];
for (int i = 0; i < invar.Length; ++i)
invar[i] = Convert.ToInt32(instr[i]);
return invar;
}
}
class Program
{
static void Main(string[] args)
{
ZumaMap map = new ZumaMap();
string[] insertinfo = InputConvert.GetInputStrings(' ');
int insloc = Convert.ToInt32(insertinfo[1]);
char insvar = Convert.ToChar(insertinfo[0]);
map.Insert(insloc, insvar);
map.WriteMap();
}
}
}