ACM 八大输出格式之C#篇

本文详细介绍了在ACM竞赛中使用C#语言进行输入输出的八种常见情况,包括处理多组输入数据,根据特定结束标志结束输入,以及在输出时如何处理换行和空格等细节。通过实例解析了C#中Console.ReadLine()和Split(), Parse()等方法的运用,以及在不同输入输出场景下的解决方案。" 109592393,10089941,Python编程:元组、可变对象与字典解析,"['Python编程', '数据结构', '字典', '元组']
摘要由CSDN通过智能技术生成

刚好学校OJ支持使用C++++(C#),所以就把OJ水题当成C#的入门途径了...0v0(第一次用C#,今后遇到更简洁的方式会继续更新...)

对应HDOJ的八大输入输出格式题

C#的Console.ReadLine()输入默认为string类型,遇到一行输入可以使用Split()来分割,再使用Parse()进行转换

A+B for Input-Output Practice (I)

Time Limit: 1000 ms  Memory Limit: 65536 KiB
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)循环就可以达到循环输出的效果



A+B for Input-Output Practice (II)

Time Limit: 1000 ms Memory Limit: 65536 KiB
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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值