资源预览内容
第1页 / 共47页
第2页 / 共47页
第3页 / 共47页
第4页 / 共47页
第5页 / 共47页
第6页 / 共47页
第7页 / 共47页
第8页 / 共47页
第9页 / 共47页
第10页 / 共47页
亲,该文档总共47页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
ACM程序设计,2019/6/12,2,第一讲,ACM入门 (Introduction to ACM),2019/6/12,3,如何入门呢?,2019/6/12,4,ACM题目特点:,由于ACM竞赛题目的输入数据和输出数据一般有多组(不定),并且格式多种多样,所以,如何处理题目的输入输出是对大家的一项最基本的要求。这也是困扰初学者的一大问题。 下面,分类介绍:,2019/6/12,5,先看一个超级简单的题目:,http:/acm.hdu.edu.cn/showproblem.php?pid=1089 Sample input: 1 5 10 20 Sample output: 6 30,2019/6/12,6,初学者很常见的一种写法:,#include void main() int a,b; scanf(“%d %d”, ,2019/6/12,7,有什么问题呢?,这就是下面需要解决的问题,2019/6/12,8,第二部分,基本输入输出,2019/6/12,9,输入_第一类:,输入不说明有多少个Input Block,以EOF为结束标志。 参见:HDOJ_1089 http:/acm.hdu.edu.cn/showproblem.php?pid=1089,2019/6/12,10,Hdoj_1089源代码:,#include int main() int a,b; while(scanf(“%d %d“, ,2019/6/12,11,本类输入解决方案:,C语法: while(scanf(“%d %d“,&a, &b) != EOF) C+语法: while( cin a b ) ,2019/6/12,12,说明(1):,Scanf函数返回值就是读出的变量个数,如:scanf( “%d %d”, 如果只有一个整数输入,返回值是1,如果有两个整数输入,返回值是2,如果一个都没有,则返回值是-1。 EOF是一个预定义的常量,等于-1。,2019/6/12,13,输入_第二类:,输入一开始就会说有N个Input Block,下面接着是N个Input Block。 参见:HDOJ_1090 http:/acm.hdu.edu.cn/showproblem.php?pid=1090,2019/6/12,14,Hdoj_1090源代码:,#include int main() int n,i,a,b; scanf(“%d“, ,2019/6/12,15,本类输入解决方案:,C语法: scanf(“%d“, i+ ) ,2019/6/12,16,输入_第三类:,输入不说明有多少个Input Block,但以某个特殊输入为结束标志。 参见:HDOJ_1091 http:/acm.hdu.edu.cn/showproblem.php?pid=1091,2019/6/12,17,Hdoj_1091源代码:,#include int main() int a,b; while(scanf(“%d %d“, ,上面的程序有什么问题?,2019/6/12,18,本类输入解决方案:,C语法: while(scanf(“%d“,&n) & n!=0 ) C+语法: while( cin n & n != 0 ) ,2019/6/12,19,输入_第四类:,以上几种情况的组合 http:/acm.hdu.edu.cn/showproblem.php?pid=1092 http:/acm.hdu.edu.cn/showproblem.php?pid=1093 http:/acm.hdu.edu.cn/showproblem.php?pid=1094,2019/6/12,20,输入_第五类:,输入是一整行的字符串的 参见:HDOJ_1048 http:/acm.hdu.edu.cn/showproblem.php?pid=1048,2019/6/12,21,本类输入解决方案:,C语法: char buf20; gets(buf); C+语法: 如果用string buf;来保存: getline( cin , buf ); 如果用char buf 255 ; 来保存: cin.getline( buf, 255 );,2019/6/12,22,说明(5_1):,scanf(“ %s%s”,str1,str2),在多个字符串之间用一个或多个空格分隔; 若使用gets函数,应为gets(str1); gets(str2); 字符串之间用回车符作分隔。 通常情况下,接受短字符用scanf函数,接受长字符用gets函数。 而getchar函数每次只接受一个字符,经常c=getchar()这样来使用。,2019/6/12,23,说明(5_2):cin.getline的用法:,getline 是一个函数,它可以接受用户的输入的字符,直到已达指定个数,或者用户输入了特定的字符。它的函数声明形式(函数原型)如下: istream 不用管它的返回类型,来关心它的三个参数: char line: 就是一个字符数组,用户输入的内容将存入在该数组内。 int size : 最多接受几个字符?用户超过size的输入都将不被接受。 char endchar :当用户输入endchar指定的字符时,自动结束。默认是回车符。,2019/6/12,24,说明(5_2)续,结合后两个参数,getline可以方便地实现: 用户最多输入指定个数的字符,如果超过,则仅指定个数的前面字符有效,如果没有超过,则用户可以通过回车来结束输入。 char name4; cin.getline(name,4,n); 由于 endchar 默认已经是 n,所以后面那行也可以写成: cin.getline(name,4);,2019/6/12,25,思考:,以下题目属于哪一类输入? http:/acm.hdu.edu.cn/showproblem.php?pid=1018 http:/acm.hdu.edu.cn/showproblem.php?pid=1013,2019/6/12,26,输出_第一类:,一个Input Block对应一个Output Block,Output Block之间没有空行。 参见:HDOJ_1089 http:/acm.hziee.edu.cn/showproblem.php?pid=1089,2019/6/12,27,解决方案:,C语法: printf(“%dn“,ans); C+语法: . cout ans endl; ,2019/6/12,28,输出_第二类:,一个Input Block对应一个Output Block,每个Output Block之后都有空行。 参见:HDOJ_1095 http:/acm.hdu.edu.cn/showproblem.php?pid=1095,2019/6/12,29,1095源代码,#include int main() int a,b; while(scanf(“%d %d“, ,2019/6/12,30,解决办法:,C语法: printf(“%dnn“,ans); C+语法: . cout ans endl endl; ,2019/6/12,31,输出_第三类:,一个Input Block对应一个Output Block,Output Block之间有空行。 参见:HDOJ_1096 http:/acm.hdu.edu.cn/showproblem.php?pid=1096,2019/6/12,32,1096源代码,#include int main() int icase,n,i,j,a,sum; scanf(“%d“, ,2019/6/12,33,解决办法:,C语法: for (k=0;kcount;k+) while () printf(“ %dn“,result); if (k!=count-1) printf(“n“); C+语法: 类似,输出语句换一下即可。,2019/6/12,34,思考:,以下题目属于哪一类输出? http:/acm.hdu.edu.cn/showproblem.php?pid=1016 http:/acm.hdu.edu.cn/showproblem.php?pid=1017,2019/6/12,35,附:,初学者常见问题,2019/6/12,36,一、编译错误,Main函数必须返回int类型(正式比赛) 不要在for语句中定义类型 _int64不支持,可以用long long代替 使用了汉语的标点符号 itoa不是ansi函数 能将整数转换为字符串而且与ANSI标准兼容的方法是使用printf()函数 int num = 100; char str25; itoa(num,str,2); printf(“integer = %d string = %sn“, num, str); 另外,拷贝程序容易产生错误,2019/6/12,37,二、C语言处理“混合数据”的问题,http:/acm.hdu.edu.cn/showproblem.php?pid=1170,2019/6/12,38,常见的代码:, scanf(“%dn“, ,2019/6/12,39,思考,上面程序 有什么问题?,2019/6/12,40,四、Printf和cout混用的问题,以下的程序输出什么? #include #include int main() int j=0; for(j=0;j5;j+) cout“j=“; printf(“%dn“,j); return 0; /利用G+编译,2019/6/12,41,为什么?,一个带缓冲输出(cout) 一个不带缓冲输出(printf),2019/6/12,42,五、输入输出原理,Input 1 5 2 6 10 20 111 111 321 123,Output 6 8 30 222 444,2019/6/12,43,相关资料,2019/6/12,44,学习方式,练习-总结-练习-总结- http:/acm.hdu.edu.cn 杭电ACM论坛 google、baidu,2019/6/12,45,2019/6/12,46,课后任务:,1016-1018、1013、1048、1061 1089-1096、1170、2000-2043 88,13,61 练习: 1092、1093、1094、1048、1170,2019/6/12,47,Welcome to HDOJ,Thank You ,
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号