国际大学生程序设计大赛(ACM-icpc)输入输出介绍课件

上传人:我*** 文档编号:145713845 上传时间:2020-09-22 格式:PPT 页数:86 大小:309KB
返回 下载 相关 举报
国际大学生程序设计大赛(ACM-icpc)输入输出介绍课件_第1页
第1页 / 共86页
国际大学生程序设计大赛(ACM-icpc)输入输出介绍课件_第2页
第2页 / 共86页
国际大学生程序设计大赛(ACM-icpc)输入输出介绍课件_第3页
第3页 / 共86页
国际大学生程序设计大赛(ACM-icpc)输入输出介绍课件_第4页
第4页 / 共86页
国际大学生程序设计大赛(ACM-icpc)输入输出介绍课件_第5页
第5页 / 共86页
点击查看更多>>
资源描述

《国际大学生程序设计大赛(ACM-icpc)输入输出介绍课件》由会员分享,可在线阅读,更多相关《国际大学生程序设计大赛(ACM-icpc)输入输出介绍课件(86页珍藏版)》请在金锄头文库上搜索。

1、2020/9/22,1,ACM程序设计,输入输出格式,2020/9/22,2,ACM题目特点,由于ACM竞赛题目的输入数据和输出数据一般有多组(不定),并且格式多种多样,所以,如何处理题目的输入输出是对大家的一项最基本的要求。这也是困扰初学者的一大问题。 下面,分类介绍:,2020/9/22,3,一个超级简单的题目(ex-1):,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

2、by a space, one pair of integers per line. OutputFor 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 input1 510 20 Sample output630,2020/9/22,4,初学者很常见的一种写法:,#include void main() int a,b; scanf(“%d %d”, ,

3、2020/9/22,5,有什么问题呢?,这就是下面需要解决的问题,基本输入输出,2020/9/22,6,输入第一类:,输入不说明有多少个Input Block,以EOF为结束标志。 参见:ex-1.,2020/9/22,7,ex-1源代码:,#include int main() int a,b; while(scanf(%d %d, ,2020/9/22,8,本类输入解决方案:,C语法: while(scanf(%d %d, scanf(%d, ,2020/9/22,12,本类输入解决方案:,C语法: scanf(%d, i+ ) . ,2020/9/22,13,输入第三类:,输入不说明有多

4、少个Input Block,但以某个特殊输入为结束标志。 ex-3 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 processe

5、d. OutputFor 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 input1 510 200 0 Sample output630,2020/9/22,14,ex-3源代码:,#include int main() int a,b; while(scanf(%d %d, ,上面的程序有什么问题?,杜绝低级错误!,2020/9/22,15,本类输入

6、解决方案:,如果最后一行是以一个0结尾则: C语法: while(scanf(%d, gets(buf); C+语法: 如果用string buf;来保存: getline( cin , buf ); 如果用char buf 255 ; 来保存: cin.getline( buf, 255 );,2020/9/22,18,说明:,scanf(“ %s%s”,str1,str2),在多个字符串之间用一个或多个空格分隔; 若使用gets函数,应为gets(str1); gets(str2); 字符串之间用回车符作分隔。 通常情况下,接受短字符用scanf函数,接受长字符用gets函数。 而getc

7、har函数每次只接受一个字符,经常c=getchar()这样来使用。,2020/9/22,19,说明:cin.getline的用法,getline 是一个函数,它可以接受用户的输入的字符,直到已达指定个数,或者用户输入了特定的字符。它的函数声明形式(函数原型)如下: istream 不用管它的返回类型,来关心它的三个参数: char line: 就是一个字符数组,用户输入的内容将存入在该数组内。 int size : 最多接受几个字符?用户超过size的输入都将不被接受。 char endchar :当用户输入endchar指定的字符时,自动结束。默认是回车符。,2020/9/22,20,说明

8、续,结合后两个参数,getline可以方便地实现: 用户最多输入指定个数的字符,如果超过,则仅指定个数的前面字符有效,如果没有超过,则用户可以通过回车来结束输入。 char name4; cin.getline(name,4,n); 由于 endchar 默认已经是 n,所以后面那行也可以写成: cin.getline(name,4);,2020/9/22,21,练习:,以下题目属于哪一类输入? POJ_1423 POJ_1519 更多类型,2020/9/22,22,输出的问题:,一个Input Block对应一个Output Block,Output Block之间空行。 ex-4 Prob

9、lem 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. OutputFor each group of input integers you should output their sum in one

10、line, and you must note that there is a blank line between outputs. Sample input3 4 1 2 3 4 5 1 2 3 4 5 3 1 2 3 Sample output10156,2020/9/22,23,以下方法什么问题?,C语法: . printf(%dnn,ans); C+语法: . cout ans endl endl; ,2020/9/22,24,Ex-4正确源代码,#include int main() int icase,n,i,j,a,sum; scanf(%d, ,2020/9/22,25,解决

11、办法:,C语法: for (k=0;kcount;k+) while () printf( %dn,result); if (k!=count-1) printf(n); C+语法: 类似,输出语句换一下即可。,2020/9/22,26,思考:,思考以下题目的输入格式 ,2020/9/22,27,初学者常见问题,2020/9/22,28,编译错误,Main函数必须返回int类型(正式比赛) 不要在for语句中定义类型 _int64不支持,可以用long long代替 使用了汉语的标点符号 itoa不是ANSI函数 能将整数转换为字符串而且与ANSI标准兼容的方法是使用sprintf()函数 i

12、nt num = 100; char str25; sprintf(str, %d , num); 另外,拷贝程序容易产生错误,2020/9/22,29,不常规的编程方式,Printf和cout混用的问题 以下的程序输出什么? #include #include int main() int j=0; for(j=0;j5;j+) coutj=; printf(%dn,j); return 0; ,2020/9/22,30,什么问题?,一个带缓冲输出(cout) 一个不带缓冲输出(printf) Goole你的问题,充分利用网络资源,2020/9/22,31,相互学习进步,总结一些非算法方面的

13、错误; 将心得相互交流; 避免初级错误是准备竞赛的第一步; 不要轻视初级错误。,2020/9/22,32,菜鸟之伤 ACM菜鸟的21个经典错误,2020/9/22,33,以HDU1089 AB为例,Sample Input 1 5 10 20 Sample Output 6 30,2020/9/22,34,菜鸟之伤(1),#include void main() int a,b; scanf(“%d%d”, ,2020/9/22,35,菜鸟之伤(1),总结: 程序不能处理多组数据的问题是最常见的入门问题,只要掌握几种常见的类型,就可以轻松掌握了,具体处理方法曾在第一次课件有详细描述,这里省略了

14、,2020/9/22,36,菜鸟之伤(2),#include void main() int a,b; while(scanf(“%d%d”, ,2020/9/22,37,菜鸟之伤(2),总结:文件结束符EOF的值是-1而不是0,所以while(scanf()!=0)常常会因为死循环而造成TLE,这个必须牢记。 说明:不仅仅菜鸟,很多老鸟也常常因为不注意这点而犯错误,而且还常常因为想不到会犯这种低级错误而想不到原因。,2020/9/22,38,菜鸟之伤(3),#include void main() int a,b; while(scanf(“%d%d”, ,2020/9/22,39,菜鸟之伤

15、(3),总结:while 或者 for循环的条件外面误加了分号,编译不影响,但是结果循环体没有真正得到多次执行; 说明:菜鸟常犯的错误,往往因为编译能通过而不能迅速察觉,尤其比赛中 提醒:当你将scanf();语句加上while循环以处理多组数据问题的时候尤其注意因为之前有分号,很容易忘记去掉!,2020/9/22,40,菜鸟之伤(4),#include void main() int a,b; while(scanf(“%d%d”, ,2020/9/22,41,菜鸟之伤(4),总结: C语言中,赋值符号和判断是否相等的逻辑符号具有完全不同的含义,往往因为我们的习惯问题,在编程中误将判断是否相

16、等的逻辑符号写成赋值符号。同样的,这种失误也会因为不影响编译而影响查错的时间。 说明:菜鸟常犯的错误,但是有过几次教训就会牢记了,呵呵,2020/9/22,42,以HDU1001 Sum Problem为例,Sample Input 1 100 Sample Output 1 5050,2020/9/22,43,菜鸟之伤(5),#include void main() int i,n,s; while(scanf(“%d”, ,2020/9/22,44,菜鸟之伤(5),总结: 忘记变量的初始化是典型的菜鸟问题,不必紧张,多经历几次就牢记了 说明:普通变量的初始化还比较容易查找,而用来保存计算结果的数组的初始化更是容易忘记!,2020/9/22,45,菜鸟之伤(6),#include void main() int i,n,s=0; while(scanf(“%d”, ,2020/9/22,46,菜鸟之伤(6),总结:变量初始化放在循环

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号