HDU Text Reverse/1.2

来源:互联网 发布:柱状图制作软件 编辑:程序博客网 时间:2024/06/03 20:50

1.12字符反转:Sample Input3olleh !dlrowm’I morf .udhI ekil .mca

Sample Outputhello world!I’m from hdu.I like acm.好久没有用 STL ,于是想到了string 数组的使用 ,还有reverse(s.begin(),s.end()),发现不行 , 在这个题里 没法控制 流 ;(得看看流的那部分了)。方法1:用char[] 数组 。方法2:用栈做(因为先进后出,和栈非常类似)// 拓宽思路

方法一:

#if 1#includeusing namespace std;int main(){char s[1001], s1[1001];int len, i, j, p;int n ;cin>>n;getchar();while(n--){    gets(s);  len=strlen(s); for(i=0,j=0; i<len; i++) {     if(s[i]!=' ')     s1[j++]=s[i];     else {   for(p=j-1; p>=0; p--)      cout<<s1[p];       cout <<" ";       j=0;  } }     for(p=j-1; p>=0; p--)    cout <<s1[p];    cout << endl; }    }

原创粉丝点击