ACM第一讲--输入输出

ACM第一讲--输入输出

输入输出基本语法

scanf函数的基本用法

scanf函数是一个标准库函数,它的函数原型在头文件“stdio.h”中1。其用法为:
scanf("<格式化字符串>",<地址表>);
其中格式化字符串的一般形式为:%[*][输入数据宽度][长度]类型,方括号[]的项表示为任选项。

  1. 类型:
    表示输入数据的类型,其格式符和意义如下表所示。
格式 字符意义
d 输入十进制整数
o 输入八进制整数
x 输入十六进制整数
u 输入无符号十进制整数
f或e 输入实型数(用小数形式或指数形式)
c 输入单个字符
s 输入字符串
  1. 宽度

用十进制整数指定输入的宽度(即字符数)。例如:
scanf("%5d",&a);
输入12345678只把12345赋予变量a,其余部分被截去。又如:
scanf("%4d%4d",&a,&b);
输入12345678将把1234赋予a,而把5678赋予b。

3.长度
长度格式符为l和h,l表示输入长整型数据(如%ld)和双精度浮点数(如%lf)。h表示输入短整型数据。

使用scanf函数还必须注意以下几点:

  • scanf函数中没有精度控制,如:scanf("%5.2f",&a);是非法的。不能企图用此语句输入小数为2位的实数。
  • scanf中要求给出变量地址,如给出变量名则会出错。如scanf("%d",a);是非法的,应改为scnaf("%d",&a);才是合法的。
  • 在输入多个数值数据时,若格式控制串中没有非格式字符作输入数据之间的间隔则可用空格,TAB或回车作间隔。C编译在碰 到空格,TAB,回车或非法数据(如对“%d”输入“12A”时,A即为非法数据)时即认为该数据结束。
  • 在输入字符数据时,若格式控制串中无非格式字符,则认为所有输入的字符均为有效字符

scanf函数的返回值

scanf 函数是有返回值的,它的返回值可以分成三种情况

  • 正整数,表示正确输入参数的个数。例如执行 scanf("%d %d", &a, &b);
    如果用户输入"3 4",可以正确输入,返回2(正确输入了两个变量);
    如果用户输入"3,4",可以正确输入a,无法输入b,返回1(正确输入了一个变量)。
  • 0,表示用户的输入不匹配,无法正确输入任何值。如上例,用户如果输入",3 4",返回0。
  • EOF,这是在stdio.h里面定义的常量(通常值为-1),表示输入流已经结束。在Windows下,用户按下CTRL+Z(会看到一个^Z字符)再按下回车(可能需要重复2次),就表示输入结束;Linux/Unix下使用CTRL+D表示输入结束。

printf函数的基本用法

printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息。在
编写程序时经常会用到此函数。printf()函数的调用格式为:
printf("<格式化字符串>", <参量表>);
其中格式化字符串包括两部分内容: 一部分是正常字符, 这些字符将按原样输出; 另
一部分是格式化规定字符, 以"%"开始, 后跟一个或几个规定字符,用来确定输出内容格式。

例子

ACM中最常见的三种输入格式(以A+B为例)

第一种输入格式,以A+B for Input-Output Practice (II)为例:

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
代码1:

#include<stdio.h>
int main(){
    int i,T,a,b;
    scanf("%d",&T);
    for(i=0;i<T;i++){
        scanf("%d%d",&a,&b);
        printf("%d\n",a+b);
    }
    return 0;
}

代码2

#include<stdio.h>
int main(){
    int T,a,b;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&a,&b);
        printf("%d\n",a+b);
    }
    return 0;
}

第二种输入格式,以A+B for Input-Output Practice (III)为例:

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
代码1:

#include<stdio.h>
int main(){
    int a,b;
    while(scanf("%d%d",&a,&b)){
        if(a==0 && b==0)break;
        printf("%d\n",a+b);
    }
    return 0;
}

代码2

#include<stdio.h>
int main(){
    int a,b;
    while(scanf("%d%d",&a,&b),a||b)
        printf("%d\n",a+b);
    
    return 0;
}

第三输入格式,以A+B for Input-Output Practice (I)为例:

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
代码1:

#include<stdio.h>
int main(){
    int a,b;
    while(scanf("%d%d",&a,&b)==2)
        printf("%d\n",a+b);
    
    return 0;
}

代码2

#include<stdio.h>
int main(){
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)//!=-1亦可
        printf("%d\n",a+b);
    
    return 0;
}

代码3

#include<stdio.h>
int main(){
    int a,b;
    while(~scanf("%d%d",&a,&b))
        printf("%d\n",a+b);
    
    return 0;
}

ACM中最常见的三种输出格式(以A+B为例)

第一种输出格式,以A+B for Input-Output Practice (VIII)为例:

Problem 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.

Output

For 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 Input

3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3

Sample Output

10


15


6

代码1:

#include<stdio.h>
int main(){
    int m,a,i,n,sum;
    scanf("%d",&m);
    while(m--){
        scanf("%d",&n);
        sum=0;
        for(i=0;i<n;i++){
            scanf("%d",&a);
            sum+=a;
        }
        printf("%d\n",sum);
        if(m)printf("\n");
    }
    return 0;    
}

第二种输入格式,以A+B for Input-Output Practice (VII)为例:

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, and followed by a blank line.

Sample Input

1 5
10 20

Sample Output

6

30

代码1:

#include<stdio.h>
int main(){
    int a,b,sum;
    while(scanf("%d%d",&a,&b)!=EOF){
        sum=a+b;
        printf("%d\n\n",sum);
    }
    return 0;
}

第三输入格式,以A+B for Input-Output Practice (VI)为例:

Problem Description

Your task is to calculate the sum of some integers.

Input

Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.

Output

For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.

Sample Input

4 1 2 3 4
5 1 2 3 4 5

Sample Output

10
15
代码1:

#include<stdio.h>
int main(){
    int a,i,n,sum;
    while(scanf("%d",&n)!=EOF){
        sum=0;
        for(i=0;i<n;i++){
            scanf("%d",&a);
            sum+=a;
        }
        printf("%d\n",sum);
    }
    return 0;
}

参考文献

posted @ 2016-04-14 09:59  算法研究  阅读(335)  评论(0编辑  收藏  举报