ACM输入输出介绍 (2).ppt
《ACM输入输出介绍 (2).ppt》由会员分享,可在线阅读,更多相关《ACM输入输出介绍 (2).ppt(86页珍藏版)》请在三一办公上搜索。
1、2023/11/7,1,ACM程序设计,输入输出格式,2023/11/7,2,ACM题目特点,由于ACM竞赛题目的输入数据和输出数据一般有多组(不定),并且格式多种多样,所以,如何处理题目的输入输出是对大家的一项最基本的要求。这也是困扰初学者的一大问题。下面,分类介绍:,2023/11/7,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 by a
2、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 20Sample output630,2023/11/7,4,初学者很常见的一种写法:,#includevoid main()int a,b;scanf(“%d%d”,2023/11/7,5,有什么问
3、题呢?,这就是下面需要解决的问题,基本输入输出,2023/11/7,6,输入第一类:,输入不说明有多少个Input Block,以EOF为结束标志。参见:ex-1.,2023/11/7,7,ex-1源代码:,#include int main()int a,b;while(scanf(%d%d,2023/11/7,8,本类输入解决方案:,C语法:while(scanf(%d%d,&a,&b)!=EOF).C+语法:while(cin a b).,2023/11/7,9,说明:,Scanf函数返回值就是读出的变量个数,如:scanf(“%d%d”,如果a和b都被成功读入整数,那么scanf的返回
4、值就是2;如果只有a被成功读入整数,返回值为1;如果a和b都未被成功读入整数,返回值为0;如果遇到错误或遇到end of file,返回值为EOF EOF是一个预定义的常量,等于-1。,2023/11/7,10,输入第二类:,输入一开始就会说有N个Input Block,下面接着是N个Input Block。ex-2Problem Description Your task is to calculate a+b.Input Input contains an integer N in the first line,and then N lines follow.Each line consi
5、sts of a pair of integers a and b,separated 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 input21 510 20Sample output630,2023/11/7,11,ex-2源代码:,#include
6、int main()int n,i,a,b;scanf(%d,2023/11/7,12,本类输入解决方案:,C语法:scanf(%d,i+).,2023/11/7,13,输入第三类:,输入不说明有多少个Input Block,但以某个特殊输入为结束标志。ex-3Problem 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 li
7、ne.A test case containing 0 0 terminates the input and this test case is not to be processed.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 0Sample output630,2023/11/7,14,ex-3源代码:
8、,#include int main()int a,b;while(scanf(%d%d,上面的程序有什么问题?,杜绝低级错误!,2023/11/7,15,本类输入解决方案:,如果最后一行是以一个0结尾则:C语法:while(scanf(%d,&n)&n!=0).C+语法:while(cin n&n!=0).,2023/11/7,16,输入第四类:,输入是一整行的字符串的参见:POJ_1298 Sample InputSTART NS BFW,JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX END START N BTZQI WFYM
9、JW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ END START IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ END ENDOFINPUT,2023/11/7,17,本类输入解决方案:,C语法:char buf20;gets(buf);C+语法:如果用string buf;来保存:getline(cin,buf);如果用char buf 255;来保存:cin.getline(buf,255);,2023/11/7,18,说明:,scanf(“
10、%s%s”,str1,str2),在多个字符串之间用一个或多个空格分隔;若使用gets函数,应为gets(str1);gets(str2);字符串之间用回车符作分隔。通常情况下,接受短字符用scanf函数,接受长字符用gets函数。而getchar函数每次只接受一个字符,经常c=getchar()这样来使用。,2023/11/7,19,说明:cin.getline的用法,getline 是一个函数,它可以接受用户的输入的字符,直到已达指定个数,或者用户输入了特定的字符。它的函数声明形式(函数原型)如下:istream不用管它的返回类型,来关心它的三个参数:char line:就是一个字符数组,
11、用户输入的内容将存入在该数组内。int size:最多接受几个字符?用户超过size的输入都将不被接受。char endchar:当用户输入endchar指定的字符时,自动结束。默认是回车符。,2023/11/7,20,说明续,结合后两个参数,getline可以方便地实现:用户最多输入指定个数的字符,如果超过,则仅指定个数的前面字符有效,如果没有超过,则用户可以通过回车来结束输入。char name4;cin.getline(name,4,n);由于 endchar 默认已经是 n,所以后面那行也可以写成:cin.getline(name,4);,2023/11/7,21,练习:,以下题目属于
12、哪一类输入?POJ_1423 POJ_1519 更多类型,2023/11/7,22,输出的问题:,一个Input Block对应一个Output Block,Output Block之间空行。ex-4Problem 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
13、 follow in the same line.OutputFor 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 input3 4 1 2 3 4 5 1 2 3 4 5 3 1 2 3 Sample output10156,2023/11/7,23,以下方法什么问题?,C语法:.printf(%dnn,ans);C+语法:.cout ans endl endl;,2
14、023/11/7,24,Ex-4正确源代码,#include int main()int icase,n,i,j,a,sum;scanf(%d,2023/11/7,25,解决办法:,C语法:for(k=0;kcount;k+)while()printf(%dn,result);if(k!=count-1)printf(n);C+语法:类似,输出语句换一下即可。,2023/11/7,26,思考:,思考以下题目的输入格式,2023/11/7,27,初学者常见问题,2023/11/7,28,编译错误,Main函数必须返回int类型(正式比赛)不要在for语句中定义类型_int64不支持,可以用lon
15、g long代替使用了汉语的标点符号itoa不是ANSI函数 能将整数转换为字符串而且与ANSI标准兼容的方法是使用sprintf()函数 int num=100;char str25;sprintf(str,%d,num);另外,拷贝程序容易产生错误,2023/11/7,29,不常规的编程方式,Printf和cout混用的问题以下的程序输出什么?#include#includeint main()int j=0;for(j=0;j5;j+)coutj=;printf(%dn,j);return 0;,2023/11/7,30,什么问题?,一个带缓冲输出(cout)一个不带缓冲输出(print
16、f)Goole你的问题,充分利用网络资源,2023/11/7,31,相互学习进步,总结一些非算法方面的错误;将心得相互交流;避免初级错误是准备竞赛的第一步;不要轻视初级错误。,2023/11/7,32,菜鸟之伤ACM菜鸟的21个经典错误,2023/11/7,33,以HDU1089 AB为例,Sample Input1 510 20Sample Output630,2023/11/7,34,菜鸟之伤(1),#includevoid main()int a,b;scanf(“%d%d”,2023/11/7,35,菜鸟之伤(1),总结:程序不能处理多组数据的问题是最常见的入门问题,只要掌握几种常见的
17、类型,就可以轻松掌握了,具体处理方法曾在第一次课件有详细描述,这里省略了,2023/11/7,36,菜鸟之伤(2),#includevoid main()int a,b;while(scanf(“%d%d”,2023/11/7,37,菜鸟之伤(2),总结:文件结束符EOF的值是-1而不是0,所以while(scanf()!=0)常常会因为死循环而造成TLE,这个必须牢记。说明:不仅仅菜鸟,很多老鸟也常常因为不注意这点而犯错误,而且还常常因为想不到会犯这种低级错误而想不到原因。,2023/11/7,38,菜鸟之伤(3),#includevoid main()int a,b;while(scanf
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ACM输入输出介绍 2 ACM 输入输出 介绍
链接地址:https://www.31ppt.com/p-6501237.html