刚好学校OJ支持使用C++++(C#),所以就把OJ水题当成C#的入门途径了...0v0(第一次用C#,今后遇到更简洁的方式会继续更新...)
对应HDOJ的八大输入输出格式题
C#的Console.ReadLine()输入默认为string类型,遇到一行输入可以使用Split()来分割,再使用Parse()进行转换
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Submit
Statistic
Discuss
Problem Description
Your task is to Calculate a + b.
Too easy?! Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample Input
1 5
10 20
Sample Output
6
30
【输入】有多组输入数据,但没有具体的告诉你有多少组,只是让你对应每组输入,应该怎样输出。【输出】有多组输出,对应着每组输入,每组输出占一行。
代码:
using System;
using System.Collections.Generic;
namespace QwQ{
class Program{
static void Main(){
while(true) {
List<int> a =new List<string>(Console.ReadLine().Split()).ConvertAll<int>(i => int.Parse(i));
Console.WriteLine(a[0] + a[1]);
}
Console.ReadKey();
}
}
}
使用List将string转成int
C#中没有EOF,也不需要单独判断EOF,所以直接while(true)循环就可以达到循环输出的效果
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic Discuss
Problem Description
Your task is to Calculate a + b.
Input
Your task is to Calculate a + b.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample Input
2
1 5
10 20
Sample Output
6
30
【输入】先输入一个整数,告诉我们接下来有多少组数据,然后在输入每组数据的具体值。
【输出】有多组输出,对应着每组输入,每组输出占一行。
代码:
using System;
using System.Collections.Generic;
namespace QwQ{
class Program{
static void Main(){
int n = int.Parse(Console.ReadLine());
while(n -- > 0) {
List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i => int.Parse(i));
Console.WriteLine(a[0] + a[1]);
}
Console.ReadKey();
}
}
}
C# 中int类型不能转成bool类型,所以没法像C那样直接while(n--)
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Submit
Statistic
Discuss
Problem Description
Your task is to Calculate a + b.
Input
Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample Input
1 5
10 20
0 0
Sample Output
6
30
【输入】有多组输入数据,没有具体的告诉你有多少组,但是题目却告诉你遇见什么结束。
【输出】有多组输出,没对应一组输入都有相应的输出,结束标记不用管!
代码:
using System;
using System.Collections.Generic;
namespace QwQ{
class Program{
static void Main(){
while(true) {
List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i=>int.Parse(i));
if(a[0] == 0 && a[1] == 0) break;
Console.WriteLine(a[0] + a[1]);
}
Console.ReadKey();
}
}
}只需要在第一种基础上判断一下条件跳出循环
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Submit
Statistic
Discuss
Problem Description
Your task is to Calculate the sum of some integers.
Input
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
Sample Input
4 1 2 3 4
5 1 2 3 4 5
0
Sample Output
10
15
【输入】输入有多组,并且题目告诉你每组输入遇见什么结束,与第三种不同之处在于,每组输入都有相应的细化(输入数量为第每组第一个数字)。
【输出】没有什么变化,只需要对应输出即可。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
namespace QwQ{
class Program{
static void Main(){
while(true) {
List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i=>int.Parse(i));
if(a[0] == 0) break;
Console.WriteLine(a.Aggregate(-a[0],(sum,next)=>sum += next));
}
Console.ReadKey();
}
}
}
因为是读一行,所以不需要在意输入数量
使用了Aggregate累加List中的值,也可以使用循环累加
for(int i = 1;i < a.Count;i ++)
或者sum方法:
Console.WriteLine(a.Sum() - a[0]);
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Submit
Statistic
Discuss
Problem Description
Your task is to calculate the sum of some integers.
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
Sample Input
2
4 1 2 3 4
5 1 2 3 4 5
Sample Output
10
15
【输入】这次的输入实现输入一个整数,告诉我们有多少行,在输入每一行。对于每一行的输入,有划分为第一个数和其他的数,第一个数代表那一组数据一共有多少输入。
【输出】没有变化,只需按照提议即可!
代码:
using System;
using System.Collections.Generic;
using System.Linq;
namespace QwQ{
class Program{
static void Main(){
int n = int.Parse(Console.ReadLine());
while(n -- > 0) {
List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i=>int.Parse(i));
Console.WriteLine(a.Aggregate((sum,next)=>sum += next) - a[0]);
}
Console.ReadKey();
}
}
}
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Submit
Statistic
Discuss
Problem Description
Your task is to calculate the sum of some integers.
Input
Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.
Output
For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.
Sample Input
4 1 2 3 4
5 1 2 3 4 5
Sample Output
10
15
using System;
using System.Collections.Generic;
using System.Linq;
namespace QwQ{
class Program{
static void Main(){
while(true) {
List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i=>int.Parse(i));
Console.WriteLine(a.Aggregate((sum,next)=>sum += next) - a[0]);
}
Console.ReadKey();
}
}
}
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic Discuss
Problem Description
Your task is to Calculate a + b.
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.
Sample Input
1 5
10 20
Sample Output
6
30
【输入】同第一种一样
【输出】输出需要判断是不是最后一组,最后一组不加空行
using System;
using System.Collections.Generic;
using System.Linq;
namespace QwQ{
class Program{
static void Main(){
bool First = true;
while(true) {
List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i=>int.Parse(i));
if(First) First = false;
else Console.WriteLine();
Console.WriteLine(a[0] + a[1]);
}
Console.ReadKey();
}
}
}
使用bool变量判断是不是第一组,只要不是第一组就在前面加上一个空格
Console.Write()和Console.WriteLine()的区别就在于后者在输出之后会换行
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Submit
Statistic
Discuss
Problem Description
Your task is to calculate the sum of some integers
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line
Output
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.
Sample Input
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
Sample Output
10
15
6
【输入】类似于第三种
【输出】这种类型的输出注意的就是换行,这类题目说在输出样例中,每组样例之间有什么什么,所以我们在对应输出的同时要判断一下是否是最后一组输出,如果不是,就 将题目所说的东西输出(一般是换行或空格),如果是,就直接结束。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
namespace QwQ{
class Program{
static void Main(){
int n = int.Parse(Console.ReadLine());
while(n -- > 0) {
List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i=>int.Parse(i));
Console.WriteLine(a.Aggregate((sum,next)=>sum += next) - a[0]);
if(n != 0) Console.WriteLine();
}
Console.ReadKey();
}
}
}
保留小数 :
需要保留小数点后几位时,应使用{0 : F}来保留,使用其他保留方式可能会导致错误
Console.WriteLine("{0:F3}", a);
如下题 :
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Submit
Statistic
Discuss
Problem Description
根据输入的半径值,计算球的体积。
Input
输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
Output
输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
Sample Input
1
1.5
Sample Output
4.189
14.137
Hint
#define PI 3.1415927
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QwQ {
class Program {
static void Main(string[] args) {
while(true) {
Console.WriteLine("{0:F3}", V(double.Parse(Console.ReadLine())));
}
Console.ReadKey();
}
public static double V(double r) {
return 4.0 / 3.0 * PI * r * r * r;
}
public const double PI = 3.1415927;
}
}