scanf 与 gets HDU1062

scanf("%s", s);

输入时忽略开头的空格与回车,输入字符后, 结尾遇到空格,回车结束;

gets(s);

不忽略开头的空格与回车,遇到回车立即结束;

#include<bits/stdc++.h>
#define FOR(i, x, y) for(int i = x; i <= y; i ++)
#define FORD(i, y, x) for(int i = y; i >= x; i --)
#define mset(x, v) memset(x, v, sizeof(x))
#define maxn 220
using namespace std;
int main()
{
    char s[100];
    int n;
    scanf("%d", &n);
    gets(s);
    cout <<s<<endl;
    return 0;
}

sample:

input

3(回车)

output:

回车被当作s

getchar();

不忽略所有字符(包括回车,空格)


HDU 1062

Text Reverse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23697    Accepted Submission(s): 9134


Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
 

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
 

Output
For each test case, you should output the text which is processed.
 

Sample Input
  
  
3 olleh !dlrow m'I morf .udh I ekil .mca
 

Sample Output
 
 
hello world! I'm from hdu. I like acm.

//PE代码,开始只是认为输出的问题,后来发现其实是这种做法是错误的,后来发现百度知道上也有很多问的,我提供一个错误样例.
// input :I  evol  uoy 
// output:I  love  you(注意每个字母隔了两个空格)
//由于scanf()会自动略过空格所以会将多个空格转化成一个空格
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define FOR(i, x, y) for(int i = x; i <= y; i ++)
#define FORD(i, y, x) for(int i = y; i >= x; i --)
#define mset(x, v) memset(x, v, sizeof(x))
#define maxn 1005
#define ll long long
using namespace std;
char s[maxn];
int main()
{
    int m;
    char c;
    cin >> m;
    while(m --){
        while(scanf("%s%c", s, &c) != -1){
            int len = strlen(s);
            FORD(i, len - 1, 0) printf("%c", s[i]);
            if(c == '\n') break;
            else printf("%c", c);
        }
        printf("\n");
    }
    return 0;
}

 
//AC代码, 注意gets()前面的getchar()
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define FOR(i, x, y) for(int i = x; i <= y; i ++)
#define FORD(i, y, x) for(int i = y; i >= x; i --)
#define mset(x, v) memset(x, v, sizeof(x))
#define maxn 1005
#define ll long long
using namespace std;
char s[maxn];
int main()
{
    int m, p[2], k = 0;
    char c;
    cin >> m;
    getchar();
    while(m --){
        gets(s);
        k = 0;
        int len = strlen(s);
        FOR(i, 0, len - 1){
            if(s[i] == ' ') printf(" ");
            else {
                int j = i;
                while(j + 1 < len && s[j + 1] != ' ') j++;
                FORD(k, j, i) printf("%c", s[k]);
                i = j;
            }
        }
        printf("\n");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值