HDU 1062 字符串翻转(字符串,水)

来源:互联网 发布:淘宝助理初始化未响应 编辑:程序博客网 时间:2024/05/04 03:18

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.

好久没写题目了,今天先找了一道水题试试手。
AC:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;char str[1010];int main(){    char ch;    int p,t;    cin>>t;    getchar(); //输入数字后面scanf字符一定小心    while(t--){        p=0;        while(scanf("%c",&ch)){            if(ch==' '){                str[p]='\0'; //字符串结尾的标志                for(int i=p-1;i>=0;i--) cout<<str[i];                cout<<" ";                p=0;            }            else if(ch=='\n'){                for(int i=p-1;i>=0;i--) cout<<str[i];                cout<<endl;                break;            }            else {                str[p++]=ch;            }        }    }    return 0;}
原创粉丝点击