请教C语言一个小问题

C语言 码拜 9年前 (2015-05-11) 791次浏览 0个评论
 

这是一道水题,题目内容不是重点,我是想请教这两份代码的区别,为什么一份就是accept,另一份却是Output Limit Exceeded ,谢谢

题目内容如下:
请教C语言一个小问题

代码一:(提交OJ后accept)
#include<stdio.h>

int main()
{
    char b[21],g[21],x[9],ch;
    int t;

    scanf(“%d”,&t);

    while(t–)
    {
        scanf(“%s%s%s”,b,g,x);

        printf(“%s will survive\n”,g);
    }

    return 0;
}

代码二:(提交后是Output Limit Exceeded)
#include<stdio.h>

int main()
{
    char b[21],g[21],x[9];
    int t;

    scanf(“%d”,&t);

    getchar();

    while(t–)
    {
        gets(b);
        gets(g);
        gets(x);

    printf(“%s will survive\n”,g);
    }

    return 0;
}

20分
scanf的%s是读取到空格、回车、制表符,而gets只会读取到回车。
你用gets的话,三个人的名字会存成一个名字。
20分
使用scanf();
空白字符(空格、回车、制表符)可区分数据
使用gets();
一般回车才可区分数据
gets()
The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character (“”\n””). gets then replaces the newline character with a null character (“”\0″”) before returning the line. In contrast, the fgets function retains the newline character. 
引用 1 楼 Falleyes 的回复:

scanf的%s是读取到空格、回车、制表符,而gets只会读取到回车。
你用gets的话,三个人的名字会存成一个名字。

你好,可我是分开用gets读入三人的名字的啊,我还是不理解你的意思,麻烦再解释一下,谢谢

引用 2 楼 zhangxiangDavaid 的回复:

使用scanf();
空白字符(空格、回车、制表符)可区分数据
使用gets();
一般回车才可区分数据
gets()
The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character (“”\n””). gets then replaces the newline character with a null character (“”\0″”) before returning the line. In contrast, the fgets function retains the newline character. 

你好,可我是分开用gets读入三人的名字的啊,我还是不理解你的意思,麻烦再解释一下,谢谢

引用 4 楼 cainiao_2015 的回复:
Quote: 引用 2 楼 zhangxiangDavaid 的回复:

使用scanf();
空白字符(空格、回车、制表符)可区分数据
使用gets();
一般回车才可区分数据
gets()
The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character (“”\n””). gets then replaces the newline character with a null character (“”\0″”) before returning the line. In contrast, the fgets function retains the newline character. 

你好,可我是分开用gets读入三人的名字的啊,我还是不理解你的意思,麻烦再解释一下,谢谢

明白了,题目没看清啊。。。谢谢了


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明请教C语言一个小问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!