Trim()
是一个常用的方法,通常用于字符串处理。它的主要功能是去除字符串两端的空白字符,包括空格、制表符和换行符等
-
在 C# 中
string str = " Hello, World! "; string trimmedStr = str.Trim(); // 结果是 "Hello, World!"
1. A+B问题I
题目描述
你的任务是计算a+b。
输入描述
输入包含一系列的a和b对,通过空格隔开。一对a和b占一行。
输出描述
对于输入的每对a和b,你需要依次输出a、b的和。
如对于输入中的第二对a和b,在输出中它们的和应该也在第二行。
输入示例
3 4
11 40
输出示例
7
51
using System;
class Program
{
static void Main()
{
string line;
// 循环读取每一行,直到输入结束
while ((line = Console.ReadLine()) != null && line.Trim() != "")
{
// 分割每一行的数字
string[] parts = line.Split(' ');
// 转换为整数
int a = int.Parse(parts[0]);
int b = int.Parse(parts[1]);
// 输出 a + b 的和
Console.WriteLine(a + b);
}
}
}
2.A+B问题II
题目描述
计算a+b,但输入方式有所改变。
输入描述
第一行是一个整数N,表示后面会有N行a和b,通过空格隔开。
输出描述
对于输入的每对a和b,你需要在相应的行输出a、b的和。
如第二对a和b,对应的和也输出在第二行。
输入示例
2
2 4
9 21
输出示例
6
30
提示信息
注意,测试数据不仅仅一组。也就是说,会持续输入N以及后面的a和b
using System;
class Program
{
static void Main()
{
string line;
// 循环读取输入,直到没有更多输入
while ((line = Console.ReadLine()) != null && line.Trim() != "")
{
// 读取第一行,获取 N
int N = int.Parse(line);
// 读取后面的 N 行 a 和 b
for (int i = 0; i < N; i++)
{
string[] parts = Console.ReadLine().Split(' ');
int a = int.Parse(parts[0]);
int b = int.Parse(parts[1]);
// 输出 a + b 的和
Console.WriteLine(a + b);
}
}
}
}
3. A+B问题III
题目描述
你的任务依然是计算a+b。
输入描述
输入中每行是一对a和b。其中会有一对是0和0标志着输入结束,且这一对不要计算。
输出描述
对于输入的每对a和b,你需要在相应的行输出a、b的和。
如第二对a和b,他们的和也输出在第二行。
输入示例
2 4
11 19
0 0
输出示例
6
30
using System;
class Program
{
static void Main()
{
string line;
// 循环读取输入
while ((line = Console.ReadLine()) != null && line.Trim() != "")
{
// 分割输入的数字
string[] parts = line.Split(' ');
// 转换为整数
int a = int.Parse(parts[0]);
int b = int.Parse(parts[1]);
// 检查是否为结束标志
if (a == 0 && b == 0)
{
break; // 结束输入
}
// 输出 a + b 的和
Console.WriteLine(a + b);
}
}
}
4.A+B问题IV
题目描述
你的任务是计算若干整数的和。
输入描述
每行的第一个数N,表示本行后面有N个数。
如果N=0时,表示输入结束,且这一行不要计算。
输出描述
对于每一行数据需要在相应的行输出和。
输入示例
4 1 2 3 4
5 1 2 3 4 5
0
输出示例
10
15
using System;
class Program
{
static void Main()
{
string line;
// 循环读取输入
while ((line = Console.ReadLine()) != null && line.Trim() != "")
{
// 分割输入的数字
string[] parts = line.Split(' ');
int N = int.Parse(parts[0]);
// 检查是否为结束标志
if (N == 0)
{
break; // 结束输入
}
// 初始化和
int sum = 0;
// 计算 N 个数的和
for (int i = 1; i <= N; i++)
{
sum += int.Parse(parts[i]);
}
// 输出和
Console.WriteLine(sum);
}
}
}
5.A+B问题VII
题目描述
你的任务是计算两个整数的和。
输入描述
输入包含若干行,每行输入两个整数a和b,由空格分隔。
输出描述
对于每组输入,输出a和b的和,每行输出后接一个空行。
输入示例
2 4
11 19
输出示例
6
30
using System;
class Program
{
static void Main()
{
string line;
while ((line = Console.ReadLine()) != null)
{
// 去掉行首尾的空白,并分割成两个部分
var parts = line.Trim().Split(' ');
int a = int.Parse(parts[0]);
int b = int.Parse(parts[1]);
// 计算和并打印
Console.WriteLine(a + b);
Console.WriteLine(); // 输出空行
}
}
}
6. A+B问题VIII
题目描述
你的任务是计算若干整数的和。
输入描述
输入的第一行为一个整数N,接下来N行每行先输入一个整数M,然后在同一行内输入M个整数。
输出描述
对于每组输入,输出M个数的和,每组输出之间输出一个空行。
输入示例
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
输出示例
10
15
6
提示信息
注意以上样例为一组测试数据,后端判题会有很多组测试数据,也就是会有多个N的输入
例如输入可以是:
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
输出则是
10
15
6
10
15
6
只保证每组数据间是有空行的。但两组数据并没有空行
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
string line;
List<string> results = new List<string>();
// 读取输入
while ((line = Console.ReadLine()) != null)
{
// 第一个输入是组数N
int N = int.Parse(line.Trim());
for (int i = 0; i < N; i++)
{
line = Console.ReadLine().Trim();
string[] numbers = line.Split(' ');
// M是第一个数字
int M = int.Parse(numbers[0]);
int sum = 0;
// 计算M个整数的和
for (int j = 1; j <= M; j++)
{
sum += int.Parse(numbers[j]);
}
results.Add(sum.ToString());
}
// 在每组输出之间添加空行
results.Add("");
}
// 输出结果
if (results.Count > 0)
{
// 删除最后一个空行以符合输出格式
results.RemoveAt(results.Count - 1);
}
Console.WriteLine(string.Join(Environment.NewLine, results));
}
}
7. 平均绩点
题目描述
每门课的成绩分为A、B、C、D、F五个等级,为了计算平均绩点,规定A、B、C、D、F分别代表4分、3分、2分、1分、0分。
输入描述
有多组测试样例。每组输入数据占一行,由一个或多个大写字母组成,字母之间由空格分隔。
输出描述
每组输出结果占一行。如果输入的大写字母都在集合{A,B,C,D,F}中,则输出对应的平均绩点,结果保留两位小数。否则,输出“Unknown”。
输入示例
A B C D F
B F F C C A
D C E F
输出示例
2.00
1.83
Unknown
using System;
class Program
{
static void Main()
{
string input;
while ((input = Console.ReadLine()) != null)
{
string[] grades = input.Split(' ');
double totalPoints = 0;
int validCount = 0;
bool isValid = true;
foreach (string grade in grades)
{
switch (grade)
{
case "A":
totalPoints += 4;
validCount++;
break;
case "B":
totalPoints += 3;
validCount++;
break;
case "C":
totalPoints += 2;
validCount++;
break;
case "D":
totalPoints += 1;
validCount++;
break;
case "F":
totalPoints += 0;
validCount++;
break;
default:
isValid = false;
break;
}
}
if (isValid && validCount > 0)
{
double average = totalPoints / validCount;
Console.WriteLine(average.ToString("F2"));
}
else
{
Console.WriteLine("Unknown");
}
}
}
}
8. 摆平积木
题目描述
小明很喜欢玩积木。一天,他把许多积木块组成了好多高度不同的堆,每一堆都是一个摞一个的形式。然而此时,他又想把这些积木堆变成高度相同的。但是他很懒,他想移动最少的积木块来实现这一目标,你能帮助他吗?
输入描述
输入包含多组测试样例。每组测试样例包含一个正整数n,表示小明已经堆好的积木堆的个数。
接着下一行是n个正整数,表示每一个积木堆的高度h,每块积木高度为1。其中1<=n<=50,1<=h<=100。
测试数据保证积木总数能被积木堆数整除。
当n=0时,输入结束。
输出描述
对于每一组数据,输出将积木堆变成相同高度需要移动的最少积木块的数量。
在每组输出结果的下面都输出一个空行。
输入示例
6
5 2 4 1 7 5
0
输出示例
5
using System;
class Program
{
static void Main()
{
while (true)
{
// 读取堆的数量
int n = int.Parse(Console.ReadLine());
if (n == 0)
{
break; // 输入结束
}
// 读取每个堆的高度
string[] heightsInput = Console.ReadLine().Split();
int[] heights = new int[n];
for (int i = 0; i < n; i++)
{
heights[i] = int.Parse(heightsInput[i]);
}
// 计算总高度和目标高度
int totalHeight = 0;
foreach (int height in heights)
{
totalHeight += height;
}
int targetHeight = totalHeight / n; // 目标高度
// 计算需要移动的积木块数量
int moves = 0;
foreach (int height in heights)
{
if (height > targetHeight)
{
moves += height - targetHeight; // 移动多余的积木
}
}
// 输出结果
Console.WriteLine(moves);
Console.WriteLine(); // 输出空行
}
}
}
9. 奇怪的信
题目描述
有一天, 小明收到一张奇怪的信, 信上要小明计算出给定数各个位上数字为偶数的和。
例如:5548,结果为12,等于 4 + 8 。
小明很苦恼,想请你帮忙解决这个问题。
输入描述
输入数据有多组。每组占一行,只有一个整整数,保证数字在32位整型范围内。
输出描述
对于每组输入数据,输出一行,每组数据下方有一个空行。
输入示例
415326
3262
输出示例
12
10
using System;
public class Program
{
public static void Main()
{
string line;
while ((line = Console.ReadLine()) != null && line.Length > 0)
{
// 计算偶数的和
int sum = 0;
foreach (char digit in line)
{
int number = digit - '0'; // 将字符转换为数字
if (number % 2 == 0) // 判断是否为偶数
{
sum += number; // 加到总和
}
}
Console.WriteLine(sum);
Console.WriteLine(); // 输出空行
}
}
}
10. 运营商活动
题目描述
小明每天的话费是1元,运营商做活动,手机每充值K元就可以获赠1元话费,一开始小明充值M元,问最多可以用多少天? 注意赠送的话费也可以参与到奖励规则中
输入描述
输入包括多个测试实例。每个测试实例包括2个整数M,K(2<=k<=M<=1000)。M=0,K=0代表输入结束。
输出描述
对于每个测试实例输出一个整数,表示M元可以用的天数。
输入示例
2 2
4 3
13 3
0 0
输出示例
3
5
19
提示信息
注意第三组数据「13 3」结果为什么是19呢, 13/3=4,获得4元奖励。 13%3=1,还剩下1元,4+1=5,5元继续参加奖励规则。 5/3=1,获得1元奖励。 5%3=2,剩下2元,1+2=3,3元继续参与奖励规则。 3/3=1,获得1元奖励。 3%3=0,剩下0元,1+0=1。 1元不能参与剩下奖励。所以一共可以使用的天数是 13+4+1+1=19
using System;
public class Program
{
public static void Main()
{
string line;
while ((line = Console.ReadLine()) != null)
{
string[] parts = line.Split();
int M = int.Parse(parts[0]);
int K = int.Parse(parts[1]);
if (M == 0 && K == 0) break;
int totalDays = 0;
int currentBalance = M;
while (currentBalance > 0)
{
totalDays += currentBalance; // 计算可用天数
int bonus = currentBalance / K; // 计算获得的奖励
currentBalance = bonus + (currentBalance % K); // 更新当前余额
}
Console.WriteLine(totalDays);
}
}
}
11. 共同祖先
题目描述
小明发现和小宇有共同祖先!现在小明想知道小宇是他的长辈,晚辈,还是兄弟。
输入描述
输入包含多组测试数据。每组首先输入一个整数N(N<=10),接下来N行,每行输入两个整数a和b,表示a的父亲是b(1<=a,b<=30)。小明的编号为1,小宇的编号为2。
输入数据保证每个人只有一个父亲。
输出描述
对于每组输入,如果小宇是小明的晚辈,则输出“You are my younger”,如果小宇是小明的长辈,则输出“You are my elder”,如果是同辈则输出“You are my brother”。
输入示例
5
1 3
2 4
3 5
4 6
5 6
6
1 3
2 4
3 5
4 6
5 7
6 7
输出示例
You are my elder
You are my brother
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string line;
while ((line = Console.ReadLine()) != null)
{
int n = int.Parse(line);
if (n == 0) break;
Dictionary<int, int> parents = new Dictionary<int, int>();
for (int i = 0; i < n; i++)
{
var input = Console.ReadLine().Split();
int child = int.Parse(input[0]);
int parent = int.Parse(input[1]);
parents[child] = parent; // 记录父子关系
}
int xiaomingGeneration = GetGeneration(parents, 1);
int xiaoyuGeneration = GetGeneration(parents, 2);
if (xiaomingGeneration < xiaoyuGeneration)
{
Console.WriteLine("You are my younger");
}
else if (xiaomingGeneration > xiaoyuGeneration)
{
Console.WriteLine("You are my elder");
}
else
{
Console.WriteLine("You are my brother");
}
}
}
private static int GetGeneration(Dictionary<int, int> parents, int person)
{
int generation = 0;
while (parents.ContainsKey(person))
{
person = parents[person];
generation++;
}
return generation;
}
}
12. 打印数字图形
题目描述
先要求你从键盘输入一个整数n(1<=n<=9),打印出指定的数字图形。
输入描述
输入包含多组测试数据。每组输入一个整数n(1<=n<=9)。
输出描述
对于每组输入,输出指定的数字图形。
注意:每行最后一个数字后没有任何字符。
输入示例
5
输出示例
1
121
12321
1234321
123454321
1234321
12321
121
1
using System;
public class Program
{
public static void Main()
{
string line;
while ((line = Console.ReadLine()) != null)
{
int n = int.Parse(line);
// 打印上半部分
for (int i = 1; i <= n; i++)
{
PrintLine(i, n);
}
// 打印下半部分
for (int i = n - 1; i >= 1; i--)
{
PrintLine(i, n);
}
// 在每组输出后加一个空行(可选)
Console.WriteLine();
}
}
private static void PrintLine(int current, int total)
{
// 打印空格
Console.Write(new string(' ', total - current));
// 打印递增数字
for (int j = 1; j <= current; j++)
{
Console.Write(j);
}
// 打印递减数字
for (int j = current - 1; j >= 1; j--)
{
Console.Write(j);
}
}
}