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

  • 格式:ppt
  • 大小:540.00 KB
  • 文档页数:86

下载文档原格式

  / 50
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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 in one line, and with one line of output for each line in input. Sample input 1 5 10 20 Sample output 6 30
2012-3-26 10
ex-2源代码:
#include <stdio.h> int main() { int n,i,a,b; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d %d",&a, &b); printf("%d\n",a+b); } }
2012-3-26 11
本类输入解决方案:
C语法: scanf("%d",&n) ; for( i=0 ; i<n ; i++ ) { .... } C++语法: cin >> n; for( i=0 ; i<n ; i++ ) { .... }
2012-3-26 12
输入第三类:
输入不说明有多少个Input Block,但以某个特殊输入为结束标志。 ex-3
2012-3-26
7
本类输入解决方案:
C语法: while(scanf("%d %d",&a, &b) != EOF) { .... } C++语法: while( cin >> a >> b ) { .... }
2012-3-26
8
说明:
1.
wk.baidu.com
Scanf函数返回值就是读出的变量个数,如: scanf( “%d %d”, &a, &b ); 如果a和b都被成功读入整数,那么scanf的返回 值就是2; 如果只有a被成功读入整数,返回值为1; 如果a和b都未被成功读入整数,返回值为0; 如果遇到错误或遇到end of file,返回值为EOF EOF是一个预定义的常量,等于-1。
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
9
2.
2012-3-26
输入第二类:
输入一开始就会说有N个Input Block,下面接着是N个Input Block。 ex-2
Problem 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 consists of a pair 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 2 1 5 10 20 Sample output 6 30
5
输入第一类:
输入不说明有多少个Input Block, 以EOF为结束标志。 参见:ex-1.
2012-3-26
6
ex-1源代码:
#include <stdio.h> int main() { int a,b; while(scanf("%d %d",&a, &b) != EOF) printf("%d\n",a+b); }
ACM程序设计
输入输出格式
2012-3-26
1
ACM题目特点
由于ACM竞赛题目的输入数据和输出 数据一般有多组(不定),并且格式多 种多样,所以,如何处理题目的输入输 出是对大家的一项最基本的要求。这也 是困扰初学者的一大问题。 下面,分类介绍:
2012-3-26
2
一个超级简单的题目(ex-1):
2012-3-26 3
初学者很常见的一种写法:
#include<stdio.h> void main() { int a,b; scanf(“%d %d”,&a,&b); Printf(“%d”,a+b); }
2012-3-26
4
有什么问题呢?
这就是下面需要解决的问题
基本输入输出
2012-3-26
2012-3-26 13
ex-3源代码:
#include <stdio.h> int main() { int a,b;
while(scanf("%d %d",&a, &b) &&(a!=0 && b!=0))