A. 计算两个数的和、差、商、积。

从键盘分别输入两个不为0的整数(前2行输入),分别输出这两个整数的和 差 积 商。

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
using System;
// Written by Sheauhaw Jang
// 2020-09-22 10:50:57
namespace CSharp_Week1_A
{
public class Calculate
{
public static int Get(int a, int b, int type)
{
switch(type)
{
case 0: return a + b;
case 1: return a - b;
case 2: return a * b;
case 3: return a / b;
}
return 0;
}
}
class Program
{
static void Main(string[] args)
{
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < 4; ++i)
{
if (i > 0)
Console.Write(' ');
Console.Write(Calculate.Get(a, b, i));
}
}
}
}

B. 时间转换

输入表示时间的整数,单位是秒。输出*天*小时*分*秒。

如果时间单位前的数值是0,则不输出该时间单位的数值。

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
using System;
// Written by Sheauhaw Jang
// 2020-09-22 11:17:04
namespace CSharp_Week1_B
{
public class TimeManager
{
public static long day = 60 * 60 * 24;
public static long hour = 60 * 60;
public static long minute = 60;
public static long second = 1;
public static long[] units = { day, hour, minute, second };
public static string[] unitsName = { "天", "小时", "分", "秒" };
public static long GetUnitCount(long tVar, int type)
{
if (type != 0)
tVar %= units[type - 1];
return tVar / units[type];
}
public static bool WriteUnitCount(long tVar, int type)
{
long ans = GetUnitCount(tVar, type);
if (ans == 0)
return false;
Console.Write(ans);
Console.Write(unitsName[type]);
return true;
}
public static bool WriteCount(long tVar)
{
bool ans = false;
for (int i = 0; i < units.Length; ++i)
if (WriteUnitCount(tVar, i))
ans = true;
return ans;
}
}
class Program
{
static void Main(string[] args)
{
long tVar = Convert.ToInt64(Console.ReadLine());
TimeManager.WriteCount(tVar);
}
}
}

C. 整数加密

对于一个4位整数(从0000-9999)按如下方式加密:将每位数字加7后对10取余,用余数替换原来的数字;然后将1,3位数字互换;2,4位数字互换。

输入一个4位数,输出加密后的结果。

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
using System;
// Written by Sheauhaw Jang
// 2020-09-22 11:44:47
namespace CSharp_Week1_C
{
public class Encode
{
static int AddModulo(int x, int p) { return x >= p ? x - p : x; }
static void Swap<T>(ref T a, ref T b)
{
T tmp = b;
b = a;
a = tmp;
}
public static int[] DoEncoding(string x)
{
int[] code = new int[x.Length];
for (int i = 0; i < x.Length; ++i)
{
code[i] = x[i] - '0';
code[i] = AddModulo(code[i] + 7, 10);
}
Swap(ref code[0], ref code[2]);
Swap(ref code[1], ref code[3]);
return code;
}
public static void WriteCode(string x)
{
int[] code = DoEncoding(x);
foreach (int digit in code)
Console.Write(digit);
}
}
class Program
{
static void Main(string[] args)
{
string x = Console.ReadLine();
Encode.WriteCode(x);
}
}
}

D. 字符串输入输出

输入一个字符串,按照样例格式输出。

样例:

Alice

Hi Alice,

Welcome to C# 2020!

Best wishes!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
// Written by Sheauhaw Jang
// 2020-09-22 11:58:50
namespace CSharp_Week1_D
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
Console.WriteLine("Hi {0},", s);
Console.WriteLine("Welcome to C# 2020!");
Console.WriteLine();
Console.WriteLine("Best wishes!");
}
}
}

E. 字符大小写转换

输入一个字母,如果它是小写字母则输出它的大写字母,如果它是大写字母输出它的小写字母。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
// Written by Sheauhaw Jang
// 2020-09-22 21:28:37
namespace CSharp_Week1_E
{
class Program
{
static void Main(string[] args)
{
int s = Console.Read();
if (s >= 'a' && s <= 'z')
s = s - 'a' + 'A';
else
s = s - 'A' + 'a';
Console.WriteLine(Convert.ToChar(s));
}
}
}