1 / 14

C语言常见输入输出

C语言常见输入输出. 福州大学数计学院 甘文勇. 一、作业常见输入输出. 1、输入一行数据 2、有T组数据输入. 输入一行数据. 输入一个整数,如输入一个数200, 可以直接用scanf("%d", &n); 如果两个整数用空格隔开,可以用scanf("%d%d", &a,&b); 若是输入多行整数,如: 21 34 仍然可以用scanf("%d%d", &a,&b);. scanf会自动过滤换行跟空格,前提是输入的是基本数据类型,或者不包含空格的字符串。 输入情况如下:1,2,可以有两种读入方式 scanf("%d%c%d", &a, &c, &b);

justin
Download Presentation

C语言常见输入输出

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C语言常见输入输出 福州大学数计学院 甘文勇

  2. 一、作业常见输入输出 • 1、输入一行数据 • 2、有T组数据输入

  3. 输入一行数据 • 输入一个整数,如输入一个数200, 可以直接用scanf("%d", &n); 如果两个整数用空格隔开,可以用scanf("%d%d", &a,&b); 若是输入多行整数,如: 21 34 仍然可以用scanf("%d%d", &a,&b);

  4. scanf会自动过滤换行跟空格,前提是输入的是基本数据类型,或者不包含空格的字符串。scanf会自动过滤换行跟空格,前提是输入的是基本数据类型,或者不包含空格的字符串。 • 输入情况如下:1,2,可以有两种读入方式 scanf("%d%c%d", &a, &c, &b); scanf("%d,%d", &a, &b);

  5. 读入一个字符串。 不包含空格。 scanf("%s", str); 包含空格。 gets(str); 使用gets需注意以下几点: 1:必须包含string.h头文件 2:需注意前方读入是否已经处理换行,如下输入 1 Hello world!

  6. 此外字符型读入还有getchar() • 字符型输出有puts(),putchar()等。

  7. 输入有T组数据 • 一般情况下多组输入会先声明输入的组数。如: 3 1 2 3 4 5 6

  8. int t, a, b; • scanf("%d", &t); • while(t--){ scanf("%d%d", &a, &b); //insert your code ............ • }

  9. int t, a, b, i; • scanf("%d", &t); • for(i=1; i<=t; i++){ scanf("%d%d", &a, &b); //insert your code ........... • }

  10. 在这里我解释下你们输出时遇到的一点问题 • 多组读入时数据是一组一组处理的,我们在DOS下运行输出是处理完一组输出一组的,不是所有的都处理完再统一输出,当然,要所有都处理完再统一输出也是可以的,这需要用到数组。

  11. 二、OJ常见输入输出 • 1、读入直到文件结束(EOF) • 2、有T组数据输入。

  12. 读入直到文件结束(EOF) 以下参考FOJ的1000题。 输入数据有多组,读入直到文件结束。 #include<stdio.h> int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF){ printf("%d\n",a+b); } return 0; }

  13. 有T组数据输入 • 与作业中常见的输入输出一样。请参考第7张PPT。

  14. Thank you for listening! From gwy

More Related